Skip to content

Commit e77f5da

Browse files
committed
Fixed the TweenManager and added support to GameObjectFactory, so you can create tweens easily now all hooked in to the internal game clock. Also added the AnimationManager into Sprite, so you can create and play animations directly from sprites nice and easily.
1 parent 936118b commit e77f5da

7 files changed

Lines changed: 55 additions & 26 deletions

File tree

examples/sprite2.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,24 @@
1717
var bunny;
1818

1919
function preload() {
20-
game.load.spritesheet('ms', 'assets/sprites/metalslug_mummy37x45.png', 37, 45);
20+
// 37x45 is the size of each frame
21+
// There are 18 frames in the PNG - you can leave this value blank if the frames fill up the entire PNG, but in this case there are some
22+
// blank frames at the end, so we tell the loader how many to load
23+
game.load.spritesheet('ms', 'assets/sprites/metalslug_mummy37x45.png', 37, 45, 18);
2124
}
2225

2326
function create() {
2427

25-
bunny = new Phaser.Sprite(game, 0, 0, 'ms', 10);
28+
bunny = game.add.sprite(-40, 100, 'ms');
2629

27-
game.world.add(bunny);
30+
bunny.animations.add('walk');
31+
32+
bunny.animations.play('walk', 50, true);
33+
34+
// bunny.scale.x = 8;
35+
// bunny.scale.y = 8;
36+
37+
game.add.tween(bunny).to({ x: game.width }, 10000, Phaser.Easing.Linear.None, true);
2838

2939
}
3040

src/animation/AnimationManager.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,17 @@ Phaser.AnimationManager.prototype = {
6161

6262
if (this._frameData == null)
6363
{
64+
console.warn('No frameData available for Phaser.Animation ' + name);
6465
return;
6566
}
6667

6768
// Create the signals the AnimationManager will emit
68-
if (this._parent.events.onAnimationStart == null)
69-
{
69+
// if (this._parent.events.onAnimationStart == null)
70+
// {
7071
// this._parent.events.onAnimationStart = new Phaser.Signal();
7172
// this._parent.events.onAnimationComplete = new Phaser.Signal();
7273
// this._parent.events.onAnimationLoop = new Phaser.Signal();
73-
}
74+
// }
7475

7576
if (frames == null)
7677
{
@@ -243,7 +244,6 @@ Object.defineProperty(Phaser.AnimationManager.prototype, "frame", {
243244
{
244245
this.currentFrame = this._frameData.getFrame(value);
245246
this._frameIndex = value;
246-
console.log('AM set frame', value, this.currentFrame);
247247
this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
248248
}
249249

src/core/World.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ Phaser.World.prototype = {
2121

2222
add: function (gameobject) {
2323
this._container.addChild(gameobject);
24+
return gameobject;
2425
},
2526

2627
addAt: function (gameobject, index) {
2728
this._container.addChildAt(gameobject, index);
29+
return gameobject;
2830
},
2931

3032
getAt: function (index) {
@@ -33,6 +35,7 @@ Phaser.World.prototype = {
3335

3436
remove: function (gameobject) {
3537
this._container.removeChild(gameobject);
38+
return gameobject;
3639
},
3740

3841
/**

src/gameobjects/GameObjectFactory.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
Phaser.GameObjectFactory = function (game) {
22

33
this.game = game;
4-
this._world = this.game.world;
4+
this.world = this.game.world;
55

66
};
77

88
Phaser.GameObjectFactory.prototype = {
99

10-
_world: null,
1110
game: null,
11+
world: null,
1212

1313
/**
1414
* Create a new Sprite with specific position and sprite sheet key.
@@ -24,8 +24,23 @@ Phaser.GameObjectFactory.prototype = {
2424
if (typeof key === "undefined") { key = ''; }
2525
if (typeof frame === "undefined") { frame = null; }
2626

27-
// return this._world.group.add(new Phaser.Sprite(this.game, x, y, key, frame));
27+
return this.world.add(new Phaser.Sprite(this.game, x, y, key, frame));
2828

2929
},
3030

31+
/**
32+
* Create a tween object for a specific object. The object can be any JavaScript object or Phaser object such as Sprite.
33+
*
34+
* @param obj {object} Object the tween will be run on.
35+
* @param [localReference] {bool} If true the tween will be stored in the object.tween property so long as it exists. If already set it'll be over-written.
36+
* @return {Phaser.Tween} The newly created tween object.
37+
*/
38+
tween: function (obj, localReference) {
39+
40+
if (typeof localReference === "undefined") { localReference = false; }
41+
42+
return this.game.tweens.create(obj, localReference);
43+
44+
}
45+
3146
};

src/gameobjects/Sprite.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,14 @@ Phaser.Sprite = function (game, x, y, key, frame) {
4747
*/
4848
this.texture = PIXI.TextureCache[key];
4949

50+
if (this.game.cache.isSpriteSheet(key))
51+
{
52+
this.animations.loadFrameData(this.game.cache.getFrameData(key));
53+
}
54+
5055
if (frame !== null)
5156
{
52-
if (typeof frame == 'string')
57+
if (typeof frame === 'string')
5358
{
5459
this.frameName = frame;
5560
}

src/tween/Tween.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ Phaser.Tween = function (object, game) {
4141
this._valuesStart[ field ] = parseFloat(object[field], 10);
4242
}
4343

44-
// this.onStart = new Phaser.Signal();
45-
// this.onUpdate = new Phaser.Signal();
44+
this.onStart = new Phaser.Signal();
4645
this.onComplete = new Phaser.Signal();
4746

4847
this.isRunning = false;
@@ -93,12 +92,14 @@ Phaser.Tween.prototype = {
9392

9493
start: function ( time ) {
9594

96-
//TWEEN.add( this );
97-
9895
if (this.game === null || this._object === null) {
9996
return;
10097
}
10198

99+
this._manager.add(this);
100+
101+
this.onStart.dispatch(this._object);
102+
102103
this.isRunning = true;
103104

104105
this._onStartCallbackFired = false;
@@ -139,11 +140,7 @@ Phaser.Tween.prototype = {
139140

140141
stop: function () {
141142

142-
//TWEEN.remove( this );
143-
if (this._manager !== null) {
144-
this._manager.remove(this);
145-
}
146-
143+
this._manager.remove(this);
147144
this.isRunning = false;
148145

149146
return this;
@@ -171,7 +168,6 @@ Phaser.Tween.prototype = {
171168

172169
},
173170

174-
175171
easing: function ( easing ) {
176172

177173
this._easingFunction = easing;

src/tween/TweenManager.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Phaser.TweenManager.prototype = {
2626
*/
2727
getAll: function () {
2828

29-
return _tweens;
29+
return this._tweens;
3030

3131
},
3232

@@ -35,7 +35,7 @@ Phaser.TweenManager.prototype = {
3535
*/
3636
removeAll: function () {
3737

38-
_tweens = [];
38+
this._tweens = [];
3939

4040
},
4141

@@ -47,7 +47,7 @@ Phaser.TweenManager.prototype = {
4747
*/
4848
add: function ( tween ) {
4949

50-
_tweens.push( tween );
50+
this._tweens.push( tween );
5151

5252
},
5353

@@ -81,11 +81,11 @@ Phaser.TweenManager.prototype = {
8181
*/
8282
remove: function ( tween ) {
8383

84-
var i = _tweens.indexOf( tween );
84+
var i = this._tweens.indexOf( tween );
8585

8686
if ( i !== -1 ) {
8787

88-
_tweens.splice( i, 1 );
88+
this._tweens.splice( i, 1 );
8989

9090
}
9191

0 commit comments

Comments
 (0)