1+ var Class = require ( '../../utils/Class' ) ;
12var GetValue = require ( '../../utils/object/GetValue' ) ;
23var GetFrames = require ( './GetFrames' ) ;
34
@@ -8,97 +9,97 @@ var GetFrames = require('./GetFrames');
89// Game Objects have the Animation Component, which are like playheads to global Animations (these objects)
910// So multiple Game Objects can have playheads all pointing to this one Animation instance
1011
11- var Animation = function ( manager , key , config )
12- {
13- this . manager = manager ;
12+ var Animation = new Class ( {
1413
15- this . key = key ;
14+ initialize :
1615
17- // A frame based animation (as opposed to a bone based animation)
18- this . type = 'frame' ;
16+ function Animation ( manager , key , config )
17+ {
18+ this . manager = manager ;
1919
20- // Extract all the frame data into the frames array
21- this . frames = GetFrames ( manager . textureManager , GetValue ( config , 'frames' , [ ] ) ) ;
20+ this . key = key ;
2221
23- // The frame rate of playback in frames per second (default 24 if duration is null )
24- this . frameRate = GetValue ( config , 'framerate' , null ) ;
22+ // A frame based animation (as opposed to a bone based animation )
23+ this . type = 'frame' ;
2524
26- // How long the animation should play for. If frameRate is set it overrides this value
27- // otherwise frameRate is derived from duration
28- this . duration = GetValue ( config , 'duration' , null ) ;
25+ // Extract all the frame data into the frames array
26+ this . frames = GetFrames ( manager . textureManager , GetValue ( config , 'frames' , [ ] ) ) ;
2927
30- if ( this . duration === null && this . frameRate === null )
31- {
32- // No duration or frameRate given, use default frameRate of 24fps
33- this . frameRate = 24 ;
34- this . duration = this . frameRate / this . frames . length ;
35- }
36- else if ( this . duration && this . frameRate === null )
37- {
38- // Duration given but no frameRate, so set the frameRate based on duration
39- // I.e. 12 frames in the animation, duration = 4 (4000 ms)
40- // So frameRate is 12 / 4 = 3 fps
41- this . frameRate = this . frames . length / this . duration ;
42- }
43- else
44- {
45- // frameRate given, derive duration from it (even if duration also specified)
46- // I.e. 15 frames in the animation, frameRate = 30 fps
47- // So duration is 15 / 30 = 0.5 (half a second)
48- this . duration = this . frames . length / this . frameRate ;
49- }
28+ // The frame rate of playback in frames per second (default 24 if duration is null)
29+ this . frameRate = GetValue ( config , 'framerate' , null ) ;
5030
51- // ms per frame (without including frame specific modifiers)
52- this . msPerFrame = 1000 / this . frameRate ;
31+ // How long the animation should play for. If frameRate is set it overrides this value
32+ // otherwise frameRate is derived from duration
33+ this . duration = GetValue ( config , 'duration' , null ) ;
5334
54- // Skip frames if the time lags, or always advanced anyway?
55- this . skipMissedFrames = GetValue ( config , 'skipMissedFrames' , true ) ;
35+ if ( this . duration === null && this . frameRate === null )
36+ {
37+ // No duration or frameRate given, use default frameRate of 24fps
38+ this . frameRate = 24 ;
39+ this . duration = this . frameRate / this . frames . length ;
40+ }
41+ else if ( this . duration && this . frameRate === null )
42+ {
43+ // Duration given but no frameRate, so set the frameRate based on duration
44+ // I.e. 12 frames in the animation, duration = 4 (4000 ms)
45+ // So frameRate is 12 / 4 = 3 fps
46+ this . frameRate = this . frames . length / this . duration ;
47+ }
48+ else
49+ {
50+ // frameRate given, derive duration from it (even if duration also specified)
51+ // I.e. 15 frames in the animation, frameRate = 30 fps
52+ // So duration is 15 / 30 = 0.5 (half a second)
53+ this . duration = this . frames . length / this . frameRate ;
54+ }
5655
57- // Delay before starting playback (in seconds )
58- this . delay = GetValue ( config , 'delay' , 0 ) ;
56+ // ms per frame (without including frame specific modifiers )
57+ this . msPerFrame = 1000 / this . frameRate ;
5958
60- // Number of times to repeat the animation (-1 for infinity)
61- this . repeat = GetValue ( config , 'repeat ' , 0 ) ;
59+ // Skip frames if the time lags, or always advanced anyway?
60+ this . skipMissedFrames = GetValue ( config , 'skipMissedFrames ' , true ) ;
6261
63- // Delay before the repeat starts (in seconds)
64- this . repeatDelay = GetValue ( config , 'repeatDelay ' , 0 ) ;
62+ // Delay before starting playback (in seconds)
63+ this . delay = GetValue ( config , 'delay ' , 0 ) ;
6564
66- // Should the animation yoyo? (reverse back down to the start) before repeating?
67- this . yoyo = GetValue ( config , 'yoyo ' , false ) ;
65+ // Number of times to repeat the animation (-1 for infinity)
66+ this . repeat = GetValue ( config , 'repeat ' , 0 ) ;
6867
69- // Should sprite.visible = true when the animation starts to play?
70- this . showOnStart = GetValue ( config , 'showOnStart ' , false ) ;
68+ // Delay before the repeat starts (in seconds)
69+ this . repeatDelay = GetValue ( config , 'repeatDelay ' , 0 ) ;
7170
72- // Should sprite.visible = false when the animation finishes ?
73- this . hideOnComplete = GetValue ( config , 'hideOnComplete ' , false ) ;
71+ // Should the animation yoyo? (reverse back down to the start) before repeating ?
72+ this . yoyo = GetValue ( config , 'yoyo ' , false ) ;
7473
75- // Callbacks
76- this . callbackScope = GetValue ( config , 'callbackScope ' , this ) ;
74+ // Should sprite.visible = true when the animation starts to play?
75+ this . showOnStart = GetValue ( config , 'showOnStart ' , false ) ;
7776
78- this . onStart = GetValue ( config , 'onStart' , false ) ;
79- this . onStartParams = GetValue ( config , 'onStartParams ' , [ ] ) ;
77+ // Should sprite.visible = false when the animation finishes?
78+ this . hideOnComplete = GetValue ( config , 'hideOnComplete ' , false ) ;
8079
81- this . onRepeat = GetValue ( config , 'onRepeat' , false ) ;
82- this . onRepeatParams = GetValue ( config , 'onRepeatParams ' , [ ] ) ;
80+ // Callbacks
81+ this . callbackScope = GetValue ( config , 'callbackScope ' , this ) ;
8382
84- // Called for EVERY frame of the animation.
85- // See AnimationFrame.onUpdate for a frame specific callback.
86- this . onUpdate = GetValue ( config , 'onUpdate' , false ) ;
87- this . onUpdateParams = GetValue ( config , 'onUpdateParams' , [ ] ) ;
83+ this . onStart = GetValue ( config , 'onStart' , false ) ;
84+ this . onStartParams = GetValue ( config , 'onStartParams' , [ ] ) ;
8885
89- this . onComplete = GetValue ( config , 'onComplete ' , false ) ;
90- this . onCompleteParams = GetValue ( config , 'onCompleteParams ' , [ ] ) ;
86+ this . onRepeat = GetValue ( config , 'onRepeat ' , false ) ;
87+ this . onRepeatParams = GetValue ( config , 'onRepeatParams ' , [ ] ) ;
9188
92- // Global pause, effects all Game Objects using this Animation instance
93- this . paused = false ;
89+ // Called for EVERY frame of the animation.
90+ // See AnimationFrame.onUpdate for a frame specific callback.
91+ this . onUpdate = GetValue ( config , 'onUpdate' , false ) ;
92+ this . onUpdateParams = GetValue ( config , 'onUpdateParams' , [ ] ) ;
9493
95- this . manager . events . on ( 'PAUSE_ALL_ANIMATION_EVENT' , this . pause . bind ( this ) ) ;
96- this . manager . events . on ( 'RESUME_ALL_ANIMATION_EVENT' , this . resume . bind ( this ) ) ;
97- } ;
94+ this . onComplete = GetValue ( config , 'onComplete' , false ) ;
95+ this . onCompleteParams = GetValue ( config , 'onCompleteParams' , [ ] ) ;
9896
99- Animation . prototype . constructor = Animation ;
97+ // Global pause, effects all Game Objects using this Animation instance
98+ this . paused = false ;
10099
101- Animation . prototype = {
100+ this . manager . events . on ( 'PAUSE_ALL_ANIMATION_EVENT' , this . pause . bind ( this ) ) ;
101+ this . manager . events . on ( 'RESUME_ALL_ANIMATION_EVENT' , this . resume . bind ( this ) ) ;
102+ } ,
102103
103104 addFrame : require ( './AddFrame' ) ,
104105 addFrameAt : require ( './AddFrameAt' ) ,
@@ -131,6 +132,7 @@ Animation.prototype = {
131132 {
132133
133134 }
134- } ;
135+
136+ } ) ;
135137
136138module . exports = Animation ;
0 commit comments