1- Phaser . Animation = {
1+ /**
2+ * Animation
3+ *
4+ * An Animation instance contains a single animation and the controls to play it.
5+ * It is created by the AnimationManager and belongs to Game Objects such as Sprite.
6+ *
7+ * @package Phaser.Animation
8+ * @author Richard Davey <rich@photonstorm.com>
9+ * @copyright 2013 Photon Storm Ltd.
10+ * @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
11+ *
12+ * @param parent {Sprite} Owner sprite of this animation.
13+ * @param frameData {FrameData} The FrameData object contains animation data.
14+ * @param name {string} Unique name of this animation.
15+ * @param frames {number[]/string[]} An array of numbers or strings indicating what frames to play in what order.
16+ * @param delay {number} Time between frames in ms.
17+ * @param looped {bool} Whether or not the animation is looped or just plays once.
18+ */
19+ Phaser . Animation = function ( game , parent , frameData , name , frames , delay , looped ) {
20+
21+ this . game = game ;
22+ this . _parent = parent ;
23+ this . _frames = frames ;
24+ this . _frameData = frameData ;
25+ this . name = name ;
26+ this . delay = 1000 / delay ;
27+ this . looped = looped ;
28+ this . isFinished = false ;
29+ this . isPlaying = false ;
30+ this . _frameIndex = 0 ;
31+ this . currentFrame = this . _frameData . getFrame ( this . _frames [ this . _frameIndex ] ) ;
232
3- } ;
33+ } ;
34+
35+ Phaser . Animation . prototype = {
36+
37+ /**
38+ * Play this animation.
39+ * @param frameRate {number} FrameRate you want to specify instead of using default.
40+ * @param loop {bool} Whether or not the animation is looped or just plays once.
41+ */
42+ play : function ( frameRate , loop ) {
43+
44+ if ( typeof frameRate === "undefined" ) { frameRate = null ; }
45+ if ( typeof loop === "undefined" ) { loop = null ; }
46+
47+ if ( frameRate !== null )
48+ {
49+ this . delay = 1000 / frameRate ;
50+ }
51+
52+ if ( loop !== null )
53+ {
54+ // If they set a new loop value then use it, otherwise use the default set on creation
55+ this . looped = loop ;
56+ }
57+
58+ this . isPlaying = true ;
59+ this . isFinished = false ;
60+
61+ this . _timeLastFrame = this . game . time . now ;
62+ this . _timeNextFrame = this . game . time . now + this . delay ;
63+
64+ this . _frameIndex = 0 ;
65+
66+ this . currentFrame = this . _frameData . getFrame ( this . _frames [ this . _frameIndex ] ) ;
67+ this . _parent . setTexture ( PIXI . TextureCache [ this . currentFrame . uuid ] ) ;
68+ // this._parent.events.onAnimationStart.dispatch(this._parent, this);
69+
70+ return this ;
71+
72+ } ,
73+
74+ /**
75+ * Play this animation from the first frame.
76+ */
77+ restart : function ( ) {
78+
79+ this . isPlaying = true ;
80+ this . isFinished = false ;
81+
82+ this . _timeLastFrame = this . game . time . now ;
83+ this . _timeNextFrame = this . game . time . now + this . delay ;
84+
85+ this . _frameIndex = 0 ;
86+
87+ this . currentFrame = this . _frameData . getFrame ( this . _frames [ this . _frameIndex ] ) ;
88+
89+ } ,
90+
91+ /**
92+ * Stop playing animation and set it finished.
93+ */
94+ stop : function ( ) {
95+
96+ this . isPlaying = false ;
97+ this . isFinished = true ;
98+
99+ } ,
100+
101+ /**
102+ * Update animation frames.
103+ */
104+ update : function ( ) {
105+
106+ if ( this . isPlaying == true && this . game . time . now >= this . _timeNextFrame )
107+ {
108+ this . _frameIndex ++ ;
109+
110+ if ( this . _frameIndex == this . _frames . length )
111+ {
112+ if ( this . looped )
113+ {
114+ this . _frameIndex = 0 ;
115+ this . currentFrame = this . _frameData . getFrame ( this . _frames [ this . _frameIndex ] ) ;
116+ // this._parent.events.onAnimationLoop.dispatch(this._parent, this);
117+ }
118+ else
119+ {
120+ this . onComplete ( ) ;
121+ }
122+ }
123+ else
124+ {
125+ this . currentFrame = this . _frameData . getFrame ( this . _frames [ this . _frameIndex ] ) ;
126+ this . _parent . setTexture ( PIXI . TextureCache [ this . currentFrame . uuid ] ) ;
127+ }
128+
129+ this . _timeLastFrame = this . game . time . now ;
130+ this . _timeNextFrame = this . game . time . now + this . delay ;
131+
132+ return true ;
133+ }
134+
135+ return false ;
136+
137+ } ,
138+
139+ /**
140+ * Clean up animation memory.
141+ */
142+ destroy : function ( ) {
143+
144+ this . game = null ;
145+ this . _parent = null ;
146+ this . _frames = null ;
147+ this . _frameData = null ;
148+ this . currentFrame = null ;
149+ this . isPlaying = false ;
150+
151+ } ,
152+
153+ /**
154+ * Animation complete callback method.
155+ */
156+ onComplete : function ( ) {
157+
158+ this . isPlaying = false ;
159+ this . isFinished = true ;
160+ // this._parent.events.onAnimationComplete.dispatch(this._parent, this);
161+
162+ }
163+
164+ } ;
165+
166+ Object . defineProperty ( Phaser . Animation . prototype , "frameTotal" , {
167+
168+ get : function ( ) {
169+ return this . _frames . length ;
170+ } ,
171+
172+ enumerable : true ,
173+ configurable : true
174+
175+ } ) ;
176+
177+ Object . defineProperty ( Phaser . Animation . prototype , "frame" , {
178+
179+ get : function ( ) {
180+
181+ if ( this . currentFrame !== null )
182+ {
183+ return this . currentFrame . index ;
184+ }
185+ else
186+ {
187+ return this . _frameIndex ;
188+ }
189+
190+ } ,
191+
192+ set : function ( value ) {
193+
194+ this . currentFrame = this . _frameData . getFrame ( value ) ;
195+
196+ if ( this . currentFrame !== null )
197+ {
198+ this . _frameIndex = value ;
199+ this . _parent . setTexture ( PIXI . TextureCache [ this . currentFrame . uuid ] ) ;
200+ }
201+
202+ } ,
203+
204+ enumerable : true ,
205+ configurable : true
206+
207+ } ) ;
0 commit comments