Skip to content

Commit ffa50f2

Browse files
committed
Added in all of the animation callbacks (start, repeat, update and complete), and user-defined arguments for each of them + callback scope.
1 parent 5095e66 commit ffa50f2

7 files changed

Lines changed: 66 additions & 20 deletions

File tree

v3/src/animation/AnimationManager.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,28 @@ AnimationManager.prototype = {
4444
// {
4545
// frames: [
4646
// { key: textureKey, frame: textureFrame },
47-
// { key: textureKey, frame: textureFrame, duration: float },
48-
// { key: textureKey, frame: textureFrame, onUpdate: function }
47+
// { key: textureKey, frame: textureFrame, duration: float }
4948
// { key: textureKey, frame: textureFrame, visible: boolean }
49+
// { key: textureKey, frame: textureFrame, onUpdate: function }
5050
// ]
5151
// framerate: integer
5252
// duration: float (seconds, optional, ignored if framerate is set)
5353
// skipMissedFrames: boolean
5454
// delay: integer
55-
// repeat: -1 = forever, otherwise integer
55+
// repeat: integer (-1 = forever)
5656
// repeatDelay: integer
5757
// yoyo: boolean
5858
// showOnStart: boolean
5959
// hideOnComplete: boolean
60+
// callbackScope: Object
6061
// onStart: function
62+
// onStartParams: array
6163
// onRepeat: function
64+
// onRepeatParams: array
65+
// onUpdate: function
66+
// onUpdateParams: array
6267
// onComplete: function
68+
// onCompleteParams: array
6369
// transitions: [
6470
// {
6571
// key: string <- key of the animation to blend with,

v3/src/animation/frame/Animation.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,20 @@ var Animation = function (manager, key, config)
6161
// Should sprite.visible = false when the animation finishes?
6262
this.hideOnComplete = GetObjectValue(config, 'hideOnComplete', false);
6363

64-
// Callbacks (swap for Events?)
64+
// Callbacks
65+
this.callbackScope = GetObjectValue(config, 'callbackScope', this);
66+
6567
this.onStart = GetObjectValue(config, 'onStart', false);
68+
this.onStartParams = GetObjectValue(config, 'onStartParams', []);
69+
6670
this.onRepeat = GetObjectValue(config, 'onRepeat', false);
71+
this.onRepeatParams = GetObjectValue(config, 'onRepeatParams', []);
72+
73+
this.onUpdate = GetObjectValue(config, 'onUpdate', false);
74+
this.onUpdateParams = GetObjectValue(config, 'onUpdateParams', []);
75+
6776
this.onComplete = GetObjectValue(config, 'onComplete', false);
68-
this.onStop = GetObjectValue(config, 'onStop', false);
77+
this.onCompleteParams = GetObjectValue(config, 'onCompleteParams', []);
6978
};
7079

7180
Animation.prototype.constructor = Animation;
@@ -92,6 +101,8 @@ Animation.prototype = {
92101
component._repeat = this.repeat;
93102
component._repeatDelay = this.repeatDelay;
94103
component._yoyo = this.yoyo;
104+
component._callbackArgs[1] = this;
105+
component._updateParams = component._callbackArgs.concat(this.onUpdateParams);
95106
}
96107

97108
component.updateFrame(this.frames[startFrame]);
@@ -209,21 +220,21 @@ Animation.prototype = {
209220

210221
component.pendingRepeat = false;
211222

212-
// OnRepeat
223+
if (this.onRepeat)
224+
{
225+
this.onRepeat.apply(this.callbackScope, component._callbackArgs.concat(this.onRepeatParams));
226+
}
213227
}
214228
},
215229

216230
completeAnimation: function (component)
217231
{
218-
component.stop();
219-
220232
if (this.hideOnComplete)
221233
{
222234
component.parent.visible = false;
223235
}
224236

225-
// Events
226-
237+
component.stop(true);
227238
},
228239

229240
setFrame: function (component)

v3/src/animation/frame/Play.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,29 @@ var Play = function (key, startFrame)
44

55
this.load(key, startFrame);
66

7+
var anim = this.currentAnim;
8+
79
// Should give us 9,007,199,254,740,991 safe repeats
810
this.repeatCounter = (this._repeat === -1) ? Number.MAX_SAFE_INTEGER : this._repeat;
911

10-
this.currentAnim.getFirstTick(this);
12+
anim.getFirstTick(this);
1113

1214
this.forward = true;
1315
this.isPlaying = true;
1416
this.pendingRepeat = false;
1517

1618
this.prevTick = this.mainloop.lastFrameTimeMs;
1719

18-
if (currentAnim.showOnStart)
20+
if (anim.showOnStart)
1921
{
2022
this.parent.visible = true;
2123
}
2224

25+
if (anim.onStart)
26+
{
27+
anim.onStart.apply(anim.callbackScope, this._callbackArgs.concat(anim.onStartParams));
28+
}
29+
2330
return this;
2431
};
2532

v3/src/animation/frame/Stop.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
var Stop = function ()
1+
var Stop = function (dispatchCallbacks)
22
{
3+
if (dispatchCallbacks === undefined) { dispatchCallbacks = false; }
4+
35
this.isPlaying = false;
46

7+
var anim = this.currentAnim;
8+
9+
if (dispatchCallbacks && anim.onComplete)
10+
{
11+
anim.onComplete.apply(anim.callbackScope, this._callbackArgs.concat(anim.onCompleteParams));
12+
}
13+
514
return this;
615
};
716

v3/src/animation/frame/UpdateFrame.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,24 @@ var UpdateFrame = function (animationFrame)
77
sprite.texture = animationFrame.frame.texture;
88
sprite.frame = animationFrame.frame;
99

10-
if (animationFrame.setAlpha)
10+
if (this.isPlaying)
1111
{
12-
sprite.alpha = animationFrame.alpha;
13-
}
12+
if (animationFrame.setAlpha)
13+
{
14+
sprite.alpha = animationFrame.alpha;
15+
}
1416

15-
if (animationFrame.onUpdate)
16-
{
17-
animationFrame.onUpdate(sprite, animationFrame);
17+
var anim = this.currentAnim;
18+
19+
if (anim.onUpdate)
20+
{
21+
anim.onUpdate.apply(anim.callbackScope, this._updateParams);
22+
}
23+
24+
if (animationFrame.onUpdate)
25+
{
26+
animationFrame.onUpdate(sprite, animationFrame);
27+
}
1828
}
1929
};
2030

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: 'e6996440-1de8-11e7-bd97-cdf675a0abb1'
2+
build: '6d1ac140-1e01-11e7-abae-c74d411e0c22'
33
};
44
module.exports = CHECKSUM;

v3/src/components/Animation.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ var Animation = new Class({
7272

7373
this._paused = false;
7474
this._wasPlaying = false;
75+
76+
this._callbackArgs = [ parent, null ];
77+
this._updateParams = [];
7578
},
7679

7780
delay: Components.Delay,

0 commit comments

Comments
 (0)