@@ -24,33 +24,57 @@ var Animation = function (manager, key, config)
2424 // onComplete: function,
2525
2626 // Extract all the frame data into the frames array
27-
2827 this . frames = GetFrames ( manager . textureManager , GetObjectValue ( config , 'frames' , [ ] ) ) ;
2928
30- this . framerate = GetObjectValue ( config , 'framerate' , 24 ) ;
29+ // Scale the time (make it go faster / slower) >>> move to Animation Component?
30+ this . timeScale = 1 ;
31+
32+ // The frame rate of playback in frames per second (default 24 if duration is null)
33+ this . framerate = GetObjectValue ( config , 'framerate' , null ) ;
3134
35+ // How long the animation should play for. If framerate is set it overrides this value
36+ // otherwise framerate is derived from duration
3237 this . duration = GetObjectValue ( config , 'duration' , null ) ;
3338
34- if ( this . duration === null )
39+ if ( this . duration === null && this . framerate === null )
3540 {
36- this . duration = this . framerate * this . frames . length ;
41+ this . framerate = 24 ;
42+ this . duration = this . framerate / this . frames . length ;
43+ }
44+ else if ( this . duration && this . framerate === null )
45+ {
46+ // Duration given but no framerate, so set the framerate based on duration
47+ // I.e. 12 frames in the animation, duration = 4 (4000 ms)
48+ // So framerate is 12 / 4 = 3 fps
49+ this . framerate = this . frames . length / this . duration ;
3750 }
3851 else
3952 {
40- // Duration controls framerate
53+ // No duration, so derive from the framerate
54+ // I.e. 15 frames in the animation, framerate = 30 fps
55+ // So duration is 15 / 30 = 0.5 (half a second)
56+ this . duration = this . frames . length / this . framerate ;
4157 }
4258
59+ // ms per frame (without including frame specific modifiers)
60+ this . msPerFrame = 1000 / this . framerate ;
61+
62+ // Skip frames if the time lags, or always advanced anyway?
4363 this . skipMissedFrames = GetObjectValue ( config , 'skipMissedFrames' , true ) ;
4464
4565 // Delay before starting playback (in seconds)
4666 this . delay = GetObjectValue ( config , 'delay' , 0 ) ;
4767
68+ // Number of times to repeat the animation (-1 for infinity)
4869 this . repeat = GetObjectValue ( config , 'repeat' , 0 ) ;
4970
71+ // Delay before the repeat starts (in seconds)
5072 this . repeatDelay = GetObjectValue ( config , 'repeatDelay' , 0 ) ;
5173
74+ // Should the animation yoyo? (reverse back down to the start) before repeating?
5275 this . yoyo = GetObjectValue ( config , 'yoyo' , false ) ;
5376
77+ // Callbacks (swap for Events?)
5478 this . onStart = GetObjectValue ( config , 'onStart' , false ) ;
5579 this . onRepeat = GetObjectValue ( config , 'onRepeat' , false ) ;
5680 this . onComplete = GetObjectValue ( config , 'onComplete' , false ) ;
@@ -77,13 +101,41 @@ Animation.prototype = {
77101 return ( index < this . frames . length ) ;
78102 } ,
79103
80- setFrame : function ( timestamp , child )
104+ getNextTick : function ( component )
105+ {
106+ // When is the next update due?
107+ var frame = component . currentFrame ;
108+
109+ component . nextTick = this . msPerFrame + frame . duration ;
110+ } ,
111+
112+ setFrame : function ( component )
81113 {
114+ // Example data:
115+ // timestamp = 2356.534000020474
116+ // frameDelta = 17.632333353807383 (diff since last timestamp)
117+
82118 // Work out which frame should be set next on the child, and set it
83119
120+ var frame = component . currentFrame ;
121+
122+ var diff = component . accumulator - component . nextTick ;
123+
124+ if ( frame . nextFrame )
125+ {
126+ component . currentFrame = frame . nextFrame ;
127+
128+ component . updateFrame ( ) ;
84129
130+ component . accumulator = diff ;
85131
86- return child ;
132+ this . getNextTick ( component ) ;
133+ }
134+ else
135+ {
136+ // We're at the end of the animation
137+ component . stop ( ) ;
138+ }
87139 } ,
88140
89141 destroy : function ( )
0 commit comments