phaser/Tests/textures/dynamic texture 1.js

64 lines
1.8 KiB
JavaScript
Raw Normal View History

2013-07-13 11:38:59 +00:00
/// <reference path="../../Phaser/Game.ts" />
(function () {
var game = new Phaser.Game(this, 'game', 800, 600, preload, create, update);
function preload() {
2013-08-02 17:32:26 +00:00
game.load.image('ball', 'assets/sprites/shinyball.png');
2013-07-13 11:38:59 +00:00
}
2013-07-13 11:38:59 +00:00
var wobblyBall;
2013-07-13 11:38:59 +00:00
function create() {
// Create our DynamicTexture
2013-08-02 17:32:26 +00:00
wobblyBall = game.add.dynamicTexture(32, 64);
for (var i = 0; i < 100; i++) {
2013-08-02 17:32:26 +00:00
var temp = game.add.sprite(game.world.randomX, game.world.randomY);
temp.texture.loadDynamicTexture(wobblyBall);
2013-07-13 11:38:59 +00:00
}
2013-07-13 11:38:59 +00:00
// Populate the wave with some data
2013-08-02 17:32:26 +00:00
waveData = game.math.sinCosGenerator(32, 8, 8, 2);
2013-07-13 11:38:59 +00:00
}
2013-07-13 11:38:59 +00:00
function update() {
wobblyBall.clear();
2013-07-13 11:38:59 +00:00
updateWobblyBall();
}
2013-07-13 11:38:59 +00:00
// This creates a simple sine-wave effect running through our DynamicTexture.
// This is then duplicated across all sprites using it, meaning we only have to calculate it once.
var waveSize = 8;
var wavePixelChunk = 2;
var waveData;
var waveDataCounter;
2013-07-13 11:38:59 +00:00
function updateWobblyBall() {
var s = 0;
var copyRect = { x: 0, y: 0, w: wavePixelChunk, h: 32 };
var copyPoint = { x: 0, y: 0 };
for (var x = 0; x < 32; x += wavePixelChunk) {
2013-07-13 11:38:59 +00:00
copyPoint.x = x;
copyPoint.y = waveSize + (waveSize / 2) + waveData[s];
2013-08-02 17:32:26 +00:00
wobblyBall.context.drawImage(game.cache.getImage('ball'), copyRect.x, copyRect.y, copyRect.w, copyRect.h, copyPoint.x, copyPoint.y, copyRect.w, copyRect.h);
2013-07-13 11:38:59 +00:00
copyRect.x += wavePixelChunk;
2013-07-13 11:38:59 +00:00
s++;
}
2013-07-13 11:38:59 +00:00
// Cycle through the wave data - this is what causes the image to "undulate"
var t = waveData.shift();
waveData.push(t);
2013-07-13 11:38:59 +00:00
waveDataCounter++;
if (waveDataCounter == waveData.length) {
2013-07-13 11:38:59 +00:00
waveDataCounter = 0;
}
}
})();