phaser/examples/wip/tween generate.js

77 lines
2.2 KiB
JavaScript

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });
function preload() {
game.load.image('wasp', 'assets/sprites/wasp.png');
game.load.image('sky', 'assets/skies/sky5.png');
}
var bugs;
var index = 0;
var data;
function create() {
game.add.image(0, 0, 'sky');
// image = game.add.image(0, 0, 'wasp');
// We don't want it to actually run, so we use game.make.tween instead of game.add.tween
var tweenData = { x: 0, y: 0 };
// Here we'll tween the values held in the tweenData object to x: 500, y: 300
tween = game.make.tween(tweenData).to( { x: 100, y: 500 }, 2000, Phaser.Easing.Sinusoidal.InOut);
// Set the tween to yoyo so it loops smoothly
tween.yoyo(true);
// We have 3 interpolation methods available: linearInterpolation (the default), bezierInterpolation and catmullRomInterpolation.
// tween.interpolation(game.math.bezierInterpolation);
tween.interpolation(game.math.catmullRomInterpolation);
// Generates the tween data at a rate of 60 frames per second.
// This is useful if you've got a lot of objects all using the same tween, just at different coordinates.
// It saves having to calculate the same tween across the properties of all objects involved in the motion.
// Instead you can pre-calculate it in advance and trade that in for a bit of memory to store it in an array.
data = tween.generateData(60);
// Now create some sprites to shown the tween data in action
bugs = game.add.group();
bugs.create(100, 0, 'wasp');
bugs.create(300, 100, 'wasp');
bugs.create(600, 30, 'wasp');
}
function update() {
// A simple data playback.
// Each element of the array contains an object that includes whatever properties were tweened
// In this case the x and y properties
bugs.getAt(0).x = data[index].x;
bugs.getAt(0).y += data[index].y;
bugs.getAt(1).x += data[index].x;
bugs.getAt(1).y += data[index].y;
bugs.getAt(2).x += data[index].x;
bugs.getAt(2).y += data[index].y;
// image.x = data[index].x;
// image.y = data[index].y;
index++;
if (index === data.length)
{
index = 0;
}
}