1- /// <reference path="Cache.ts" />
21/// <reference path="Game.ts" />
3- /// <reference path="Sprite.ts" />
2+ /// <reference path="gameobjects/ Sprite.ts" />
43/// <reference path="system/animation/Animation.ts" />
54/// <reference path="system/animation/AnimationLoader.ts" />
65/// <reference path="system/animation/Frame.ts" />
76/// <reference path="system/animation/FrameData.ts" />
87
9- class Animations {
8+ module Phaser {
109
11- constructor ( game : Game , parent : Sprite ) {
10+ export class Animations {
1211
13- this . _game = game ;
14- this . _parent = parent ;
15- this . _anims = { } ;
12+ constructor ( game : Game , parent : Sprite ) {
1613
17- }
14+ this . _game = game ;
15+ this . _parent = parent ;
16+ this . _anims = { } ;
1817
19- private _game : Game ;
20- private _parent : Sprite ;
18+ }
2119
22- private _anims : { } ;
23- private _frameIndex : number ;
24- private _frameData : FrameData = null ;
20+ private _game : Game ;
21+ private _parent : Sprite ;
2522
26- public currentAnim : Animation ;
27- public currentFrame : Frame = null ;
23+ private _anims : { } ;
24+ private _frameIndex : number ;
25+ private _frameData : FrameData = null ;
2826
29- public loadFrameData ( frameData : FrameData ) {
27+ public currentAnim : Animation ;
28+ public currentFrame : Frame = null ;
3029
31- this . _frameData = frameData ;
30+ public loadFrameData ( frameData : FrameData ) {
3231
33- this . frame = 0 ;
32+ this . _frameData = frameData ;
3433
35- }
36-
37- public add ( name : string , frames :number [ ] = null , frameRate : number = 60 , loop : bool = false ) {
34+ this . frame = 0 ;
3835
39- if ( this . _frameData == null )
40- {
41- return ;
4236 }
4337
44- if ( frames == null )
45- {
46- frames = this . _frameData . getFrameIndexes ( ) ;
47- }
48- else
49- {
50- if ( this . validateFrames ( frames ) == false )
38+ public add ( name : string , frames : any [ ] = null , frameRate : number = 60 , loop : bool = false , useNumericIndex : bool = true ) {
39+
40+ if ( this . _frameData == null )
5141 {
5242 return ;
5343 }
44+
45+ if ( frames == null )
46+ {
47+ frames = this . _frameData . getFrameIndexes ( ) ;
48+ }
49+ else
50+ {
51+ if ( this . validateFrames ( frames , useNumericIndex ) == false )
52+ {
53+ return ;
54+ }
55+ }
56+
57+ if ( useNumericIndex == false )
58+ {
59+ frames = this . _frameData . getFrameIndexesByName ( frames ) ;
60+ }
61+
62+ this . _anims [ name ] = new Animation ( this . _game , this . _parent , this . _frameData , name , frames , frameRate , loop ) ;
63+
64+ this . currentAnim = this . _anims [ name ] ;
65+
5466 }
5567
56- this . _anims [ name ] = new Animation ( this . _game , this . _parent , this . _frameData , name , frames , frameRate , loop ) ;
68+ private validateFrames ( frames : any [ ] , useNumericIndex : bool ) : bool {
5769
58- this . currentAnim = this . _anims [ name ] ;
70+ for ( var i = 0 ; i < frames . length ; i ++ )
71+ {
72+ if ( useNumericIndex == true )
73+ {
74+ if ( frames [ i ] > this . _frameData . total )
75+ {
76+ return false ;
77+ }
78+ }
79+ else
80+ {
81+ if ( this . _frameData . checkFrameName ( frames [ i ] ) == false )
82+ {
83+ return false ;
84+ }
85+ }
86+ }
5987
60- }
88+ return true ;
6189
62- private validateFrames ( frames : number [ ] ) : bool {
90+ }
6391
64- var result = true ;
92+ public play ( name : string , frameRate ?: number = null , loop ?: bool ) {
6593
66- for ( var i = 0 ; i < frames . length ; i ++ )
67- {
68- if ( frames [ i ] > this . _frameData . total )
94+ if ( this . _anims [ name ] )
6995 {
70- return false ;
96+ this . currentAnim = this . _anims [ name ] ;
97+ this . currentAnim . play ( frameRate , loop ) ;
7198 }
99+
72100 }
73101
74- }
102+ public stop ( name : string ) {
75103
76- public play ( name : string , frameRate ?: number = null , loop ?: bool ) {
104+ if ( this . _anims [ name ] )
105+ {
106+ this . currentAnim = this . _anims [ name ] ;
107+ this . currentAnim . stop ( ) ;
108+ }
77109
78- if ( this . _anims [ name ] )
79- {
80- this . currentAnim = this . _anims [ name ] ;
81- this . currentAnim . play ( frameRate , loop ) ;
82110 }
83-
84- }
85111
86- public stop ( name : string ) {
112+ public update ( ) {
113+
114+ if ( this . currentAnim && this . currentAnim . update ( ) == true )
115+ {
116+ this . currentFrame = this . currentAnim . currentFrame ;
117+ this . _parent . bounds . width = this . currentFrame . width ;
118+ this . _parent . bounds . height = this . currentFrame . height ;
119+ }
87120
88- if ( this . _anims [ name ] )
89- {
90- this . currentAnim = this . _anims [ name ] ;
91- this . currentAnim . stop ( ) ;
92121 }
93-
94- }
95122
96- public update ( ) {
123+ public get frameData ( ) : FrameData {
124+ return this . _frameData ;
125+ }
97126
98- if ( this . currentAnim && this . currentAnim . update ( ) == true )
99- {
100- this . currentFrame = this . currentAnim . currentFrame ;
101- this . _parent . bounds . width = this . currentFrame . width ;
102- this . _parent . bounds . height = this . currentFrame . height ;
127+ public get frameTotal ( ) : number {
128+ return this . _frameData . total ;
103129 }
104130
105- }
131+ public get frame ( ) : number {
132+ return this . _frameIndex ;
133+ }
106134
107- public get frameTotal ( ) : number {
108- return this . _frameData . total ;
109- }
135+ public set frame ( value : number ) {
110136
111- public get frame ( ) : number {
112- return this . _frameIndex ;
113- }
137+ this . currentFrame = this . _frameData . getFrame ( value ) ;
114138
115- public set frame ( value : number ) {
139+ if ( this . currentFrame !== null )
140+ {
141+ this . _parent . bounds . width = this . currentFrame . width ;
142+ this . _parent . bounds . height = this . currentFrame . height ;
143+ this . _frameIndex = value ;
144+ }
116145
117- this . currentFrame = this . _frameData . getFrame ( value ) ;
146+ }
118147
119- if ( this . currentFrame !== null )
120- {
121- this . _parent . bounds . width = this . currentFrame . width ;
122- this . _parent . bounds . height = this . currentFrame . height ;
123- this . _frameIndex = value ;
148+ public get frameName ( ) : string {
149+ return this . currentFrame . name ;
124150 }
125151
126- }
127-
128- public get frameName ( ) : string {
129- return this . currentFrame . name ;
130- }
152+ public set frameName ( value : string ) {
131153
132- public set frameName ( value : string ) {
154+ this . currentFrame = this . _frameData . getFrameByName ( value ) ;
133155
134- this . currentFrame = this . _frameData . getFrameByName ( value ) ;
156+ if ( this . currentFrame !== null )
157+ {
158+ this . _parent . bounds . width = this . currentFrame . width ;
159+ this . _parent . bounds . height = this . currentFrame . height ;
160+ this . _frameIndex = this . currentFrame . index ;
161+ }
135162
136- if ( this . currentFrame !== null )
137- {
138- this . _parent . bounds . width = this . currentFrame . width ;
139- this . _parent . bounds . height = this . currentFrame . height ;
140- this . _frameIndex = this . currentFrame . index ;
141163 }
142164
143165 }
144166
145- }
167+ }
0 commit comments