phaser/Tests/textures/dynamic texture 1.js

58 lines
2 KiB
JavaScript
Raw Normal View History

2013-07-13 11:38:59 +00:00
/// <reference path="../../Phaser/Game.ts" />
(function () {
2013-08-02 17:32:26 +00:00
var game = new Phaser.Game(this, 'game', 800, 600, init, create, update);
2013-07-13 11:38:59 +00:00
function init() {
2013-08-02 17:32:26 +00:00
game.load.image('ball', 'assets/sprites/shinyball.png');
game.load.start();
2013-07-13 11:38:59 +00:00
}
var wobblyBall;
function create() {
// Create our DynamicTexture
2013-08-02 17:32:26 +00:00
wobblyBall = game.add.dynamicTexture(32, 64);
2013-07-13 11:38:59 +00:00
// And apply it to 100 randomly positioned sprites
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
}
// 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
}
function update() {
wobblyBall.clear();
updateWobblyBall();
}
// 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;
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) {
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;
s++;
}
// Cycle through the wave data - this is what causes the image to "undulate"
var t = waveData.shift();
waveData.push(t);
waveDataCounter++;
if(waveDataCounter == waveData.length) {
waveDataCounter = 0;
}
}
})();