From f31d56584d982bc839308d81f49ee1242c5f33d4 Mon Sep 17 00:00:00 2001 From: powerfear Date: Fri, 20 Sep 2013 19:14:50 -0400 Subject: [PATCH 1/2] make tweens chainable with .to Allow successive .to calls to chain tweens, and an example using it. --- examples/tweens/chained tweens.php | 47 ++++++++++++++++++++++++++++++ src/tween/Tween.js | 30 ++++++++++++++----- 2 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 examples/tweens/chained tweens.php diff --git a/examples/tweens/chained tweens.php b/examples/tweens/chained tweens.php new file mode 100644 index 00000000..2b4ce086 --- /dev/null +++ b/examples/tweens/chained tweens.php @@ -0,0 +1,47 @@ + + + + + \ No newline at end of file diff --git a/src/tween/Tween.js b/src/tween/Tween.js index 45b29c6e..e4986732 100644 --- a/src/tween/Tween.js +++ b/src/tween/Tween.js @@ -35,6 +35,7 @@ Phaser.Tween = function (object, game) { this._onCompleteCallback = null; this._pausedTime = 0; + this._parent = null; this.pendingDelete = false; @@ -71,26 +72,39 @@ Phaser.Tween.prototype = { repeat = repeat || 0; yoyo = yoyo || false; - this._repeat = repeat; - this._duration = duration; - this._valuesEnd = properties; + var self; + if (this._parent) + { + self = this._manager.create(this._object); + self._parent = this; + this.chain(self); + } + else + { + self = this; + self._parent = self; + } + + self._repeat = repeat; + self._duration = duration; + self._valuesEnd = properties; if (ease !== null) { - this._easingFunction = ease; + self._easingFunction = ease; } if (delay > 0) { - this._delayTime = delay; + self._delayTime = delay; } - this._yoyo = yoyo; + self._yoyo = yoyo; if (autoStart) { - return this.start(); + return self.start(); } else { - return this; + return self; } }, From c36543a2614977dc97b7adefe27e9653948f2598 Mon Sep 17 00:00:00 2001 From: powerfear Date: Fri, 20 Sep 2013 21:47:58 -0400 Subject: [PATCH 2/2] make tweens chain loopable --- examples/tweens/chained tweens.php | 7 ++----- src/tween/Tween.js | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/examples/tweens/chained tweens.php b/examples/tweens/chained tweens.php index 2b4ce086..cb77634f 100644 --- a/examples/tweens/chained tweens.php +++ b/examples/tweens/chained tweens.php @@ -10,7 +10,6 @@ var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render }); var p; - var p2; function preload() { @@ -27,13 +26,11 @@ game.add.tween(p).to({ x: 700 }, 1000, Phaser.Easing.Linear.None, true) .to({ y: 300 }, 1000, Phaser.Easing.Linear.None) .to({ x: 0 }, 1000, Phaser.Easing.Linear.None) - .to({ y: 0 }, 1000, Phaser.Easing.Linear.None); + .to({ y: 0 }, 1000, Phaser.Easing.Linear.None) + .loop(); } function update() { - - // p.y = game.math.min(100, game.input.y); - } function render() { diff --git a/src/tween/Tween.js b/src/tween/Tween.js index e4986732..e1161377 100644 --- a/src/tween/Tween.js +++ b/src/tween/Tween.js @@ -76,7 +76,7 @@ Phaser.Tween.prototype = { if (this._parent) { self = this._manager.create(this._object); - self._parent = this; + self._parent = this._parent; this.chain(self); } else @@ -206,6 +206,23 @@ Phaser.Tween.prototype = { }, + /** + * Loop a chain of tweens + * + * Usage: + * game.add.tween(p).to({ x: 700 }, 1000, Phaser.Easing.Linear.None, true) + * .to({ y: 300 }, 1000, Phaser.Easing.Linear.None) + * .to({ x: 0 }, 1000, Phaser.Easing.Linear.None) + * .to({ y: 0 }, 1000, Phaser.Easing.Linear.None) + * .loop(); + * + * @return {Tween} Itself. + */ + loop: function() { + if (this._parent) this.chain(this._parent); + return this; + }, + onStartCallback: function ( callback ) { this._onStartCallback = callback;