From e77f5da2cd8ab2390aacdd6c968d8a4f0ef51080 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Fri, 30 Aug 2013 17:09:43 +0100 Subject: [PATCH] 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. --- examples/sprite2.php | 16 +++++++++++++--- src/animation/AnimationManager.js | 8 ++++---- src/core/World.js | 3 +++ src/gameobjects/GameObjectFactory.js | 21 ++++++++++++++++++--- src/gameobjects/Sprite.js | 7 ++++++- src/tween/Tween.js | 16 ++++++---------- src/tween/TweenManager.js | 10 +++++----- 7 files changed, 55 insertions(+), 26 deletions(-) diff --git a/examples/sprite2.php b/examples/sprite2.php index 16e19c64..f9dffc4d 100644 --- a/examples/sprite2.php +++ b/examples/sprite2.php @@ -17,14 +17,24 @@ var bunny; function preload() { - game.load.spritesheet('ms', 'assets/sprites/metalslug_mummy37x45.png', 37, 45); + // 37x45 is the size of each frame + // 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 + // blank frames at the end, so we tell the loader how many to load + game.load.spritesheet('ms', 'assets/sprites/metalslug_mummy37x45.png', 37, 45, 18); } function create() { - bunny = new Phaser.Sprite(game, 0, 0, 'ms', 10); + bunny = game.add.sprite(-40, 100, 'ms'); - game.world.add(bunny); + bunny.animations.add('walk'); + + bunny.animations.play('walk', 50, true); + + // bunny.scale.x = 8; + // bunny.scale.y = 8; + + game.add.tween(bunny).to({ x: game.width }, 10000, Phaser.Easing.Linear.None, true); } diff --git a/src/animation/AnimationManager.js b/src/animation/AnimationManager.js index ea02923d..0abe0eb1 100644 --- a/src/animation/AnimationManager.js +++ b/src/animation/AnimationManager.js @@ -61,16 +61,17 @@ Phaser.AnimationManager.prototype = { if (this._frameData == null) { + console.warn('No frameData available for Phaser.Animation ' + name); return; } // Create the signals the AnimationManager will emit - if (this._parent.events.onAnimationStart == null) - { + // if (this._parent.events.onAnimationStart == null) + // { // this._parent.events.onAnimationStart = new Phaser.Signal(); // this._parent.events.onAnimationComplete = new Phaser.Signal(); // this._parent.events.onAnimationLoop = new Phaser.Signal(); - } + // } if (frames == null) { @@ -243,7 +244,6 @@ Object.defineProperty(Phaser.AnimationManager.prototype, "frame", { { this.currentFrame = this._frameData.getFrame(value); this._frameIndex = value; - console.log('AM set frame', value, this.currentFrame); this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]); } diff --git a/src/core/World.js b/src/core/World.js index 312bd9c8..4171910f 100644 --- a/src/core/World.js +++ b/src/core/World.js @@ -21,10 +21,12 @@ Phaser.World.prototype = { add: function (gameobject) { this._container.addChild(gameobject); + return gameobject; }, addAt: function (gameobject, index) { this._container.addChildAt(gameobject, index); + return gameobject; }, getAt: function (index) { @@ -33,6 +35,7 @@ Phaser.World.prototype = { remove: function (gameobject) { this._container.removeChild(gameobject); + return gameobject; }, /** diff --git a/src/gameobjects/GameObjectFactory.js b/src/gameobjects/GameObjectFactory.js index 253c24ce..b58b3342 100644 --- a/src/gameobjects/GameObjectFactory.js +++ b/src/gameobjects/GameObjectFactory.js @@ -1,14 +1,14 @@ Phaser.GameObjectFactory = function (game) { this.game = game; - this._world = this.game.world; + this.world = this.game.world; }; Phaser.GameObjectFactory.prototype = { - _world: null, game: null, + world: null, /** * Create a new Sprite with specific position and sprite sheet key. @@ -24,8 +24,23 @@ Phaser.GameObjectFactory.prototype = { if (typeof key === "undefined") { key = ''; } if (typeof frame === "undefined") { frame = null; } - // return this._world.group.add(new Phaser.Sprite(this.game, x, y, key, frame)); + return this.world.add(new Phaser.Sprite(this.game, x, y, key, frame)); }, + /** + * Create a tween object for a specific object. The object can be any JavaScript object or Phaser object such as Sprite. + * + * @param obj {object} Object the tween will be run on. + * @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. + * @return {Phaser.Tween} The newly created tween object. + */ + tween: function (obj, localReference) { + + if (typeof localReference === "undefined") { localReference = false; } + + return this.game.tweens.create(obj, localReference); + + } + }; \ No newline at end of file diff --git a/src/gameobjects/Sprite.js b/src/gameobjects/Sprite.js index 738f20b2..792f4342 100644 --- a/src/gameobjects/Sprite.js +++ b/src/gameobjects/Sprite.js @@ -47,9 +47,14 @@ Phaser.Sprite = function (game, x, y, key, frame) { */ this.texture = PIXI.TextureCache[key]; + if (this.game.cache.isSpriteSheet(key)) + { + this.animations.loadFrameData(this.game.cache.getFrameData(key)); + } + if (frame !== null) { - if (typeof frame == 'string') + if (typeof frame === 'string') { this.frameName = frame; } diff --git a/src/tween/Tween.js b/src/tween/Tween.js index 8c58ced5..65c57e1d 100644 --- a/src/tween/Tween.js +++ b/src/tween/Tween.js @@ -41,8 +41,7 @@ Phaser.Tween = function (object, game) { this._valuesStart[ field ] = parseFloat(object[field], 10); } - // this.onStart = new Phaser.Signal(); - // this.onUpdate = new Phaser.Signal(); + this.onStart = new Phaser.Signal(); this.onComplete = new Phaser.Signal(); this.isRunning = false; @@ -93,12 +92,14 @@ Phaser.Tween.prototype = { start: function ( time ) { - //TWEEN.add( this ); - if (this.game === null || this._object === null) { return; } + this._manager.add(this); + + this.onStart.dispatch(this._object); + this.isRunning = true; this._onStartCallbackFired = false; @@ -139,11 +140,7 @@ Phaser.Tween.prototype = { stop: function () { - //TWEEN.remove( this ); - if (this._manager !== null) { - this._manager.remove(this); - } - + this._manager.remove(this); this.isRunning = false; return this; @@ -171,7 +168,6 @@ Phaser.Tween.prototype = { }, - easing: function ( easing ) { this._easingFunction = easing; diff --git a/src/tween/TweenManager.js b/src/tween/TweenManager.js index ad437c26..20a558a0 100644 --- a/src/tween/TweenManager.js +++ b/src/tween/TweenManager.js @@ -26,7 +26,7 @@ Phaser.TweenManager.prototype = { */ getAll: function () { - return _tweens; + return this._tweens; }, @@ -35,7 +35,7 @@ Phaser.TweenManager.prototype = { */ removeAll: function () { - _tweens = []; + this._tweens = []; }, @@ -47,7 +47,7 @@ Phaser.TweenManager.prototype = { */ add: function ( tween ) { - _tweens.push( tween ); + this._tweens.push( tween ); }, @@ -81,11 +81,11 @@ Phaser.TweenManager.prototype = { */ remove: function ( tween ) { - var i = _tweens.indexOf( tween ); + var i = this._tweens.indexOf( tween ); if ( i !== -1 ) { - _tweens.splice( i, 1 ); + this._tweens.splice( i, 1 ); }