@@ -20,11 +20,15 @@ export class SpriteBase {
2020 frame_names ?: { [ animation : string ] : string [ ] } ;
2121 } ;
2222 } ;
23+ private action_aliases : {
24+ [ alias : string ] : string ;
25+ } ;
2326
2427 constructor ( data , key_name , actions ) {
2528 this . data = data ;
2629 this . key_name = key_name ;
2730 this . actions = { } ;
31+ this . action_aliases = { } ;
2832 for ( let i = 0 ; i < actions . length ; ++ i ) {
2933 this . actions [ actions [ i ] ] = { } ;
3034 }
@@ -35,10 +39,21 @@ export class SpriteBase {
3539 }
3640
3741 setActionAnimations ( action , animations , frame_counts ) {
42+ if ( ! this . actions . hasOwnProperty ( action ) ) {
43+ this . data . logger . log_message ( `Sprite '${ this . key_name } ' has no action '${ action } '.` , msg_types . ERROR ) ;
44+ return ;
45+ }
3846 this . actions [ action ] . animations = new Array ( animations . length ) ;
3947 this . actions [ action ] . frame_counts = new Array ( animations . length ) ;
4048 this . actions [ action ] . frame_names = { } ;
4149 const frame_count_is_array = Array . isArray ( frame_counts ) ;
50+ if ( frame_count_is_array && frame_counts . length !== animations . length ) {
51+ this . data . logger . log_message (
52+ `Frames count and animations size must match. Issue on sprite '${ this . key_name } ' for action action '${ action } '.` ,
53+ msg_types . ERROR
54+ ) ;
55+ return ;
56+ }
4257 for ( let i = 0 ; i < animations . length ; ++ i ) {
4358 const frame_count = frame_count_is_array ? frame_counts [ i ] : frame_counts ;
4459 this . actions [ action ] . animations [ i ] = animations [ i ] ;
@@ -47,6 +62,17 @@ export class SpriteBase {
4762 }
4863
4964 setActionFrameRate ( action , frame_rate ) {
65+ if ( ! this . actions . hasOwnProperty ( action ) ) {
66+ this . data . logger . log_message ( `Sprite '${ this . key_name } ' has no action '${ action } '.` , msg_types . ERROR ) ;
67+ return ;
68+ }
69+ if ( ! Array . isArray ( frame_rate ) && ! ( typeof frame_rate === "number" ) ) {
70+ this . data . logger . log_message (
71+ `Invalid frame rate set for action '${ action } ' for sprite '${ this . key_name } '.` ,
72+ msg_types . ERROR
73+ ) ;
74+ return ;
75+ }
5076 this . actions [ action ] . frame_rate = { } ;
5177 for ( let i = 0 ; i < this . actions [ action ] . animations . length ; ++ i ) {
5278 const animation = this . actions [ action ] . animations [ i ] ;
@@ -60,6 +86,13 @@ export class SpriteBase {
6086 } else {
6187 this_frame_rate = frame_rate ;
6288 }
89+ if ( this_frame_rate === undefined ) {
90+ this . data . logger . log_message (
91+ `Invalid frame rate set for action '${ action } ' for sprite '${ this . key_name } '.` ,
92+ msg_types . ERROR
93+ ) ;
94+ continue ;
95+ }
6396 this . actions [ action ] . frame_rate [ animation ] = this_frame_rate ;
6497 }
6598 }
@@ -79,9 +112,17 @@ export class SpriteBase {
79112 if ( reference_action in this . actions ) {
80113 this . actions [ alias ] = this . actions [ reference_action ] ;
81114 game . cache . setCacheAlias ( this . getSpriteKey ( alias ) , this . getSpriteKey ( reference_action ) , Phaser . Cache . IMAGE ) ;
115+ this . action_aliases [ alias ] = reference_action ;
82116 }
83117 }
84118
119+ getActionRefFromAlias ( alias ) {
120+ if ( alias in this . action_aliases ) {
121+ return this . action_aliases [ alias ] ;
122+ }
123+ return null ;
124+ }
125+
85126 hasAction ( action : string ) {
86127 return action in this . actions ;
87128 }
@@ -96,7 +137,7 @@ export class SpriteBase {
96137 const action_key = this . getSpriteKey ( action ) ;
97138 if ( game . cache . checkImageKey ( action_key ) ) {
98139 this . data . logger . log_message (
99- `Sprite key '<key_name>/<action>' '${ action_key } is already registered in the engine. Please consider renaming it.' ` ,
140+ `Sprite key '<key_name>/<action>' '${ action_key } ' is already registered in the engine. Please consider renaming it.` ,
100141 msg_types . ERROR
101142 ) ;
102143 }
@@ -125,14 +166,24 @@ export class SpriteBase {
125166 for ( let i = 0 ; i < animations . length ; ++ i ) {
126167 const animation = animations [ i ] ;
127168 const frame_rate = this . actions [ action ] . frame_rate [ animation ] ;
128- const anim_key = this . getAnimationKey ( action , animation ) ;
169+ let anim_key = this . getAnimationKey ( action , animation ) ;
129170 sprite . animations . add (
130171 anim_key ,
131172 this . actions [ action ] . frame_names [ animation ] ,
132173 frame_rate ,
133174 Array . isArray ( loop ) ? loop [ i ] : loop ,
134175 false
135176 ) ;
177+ const ref_action = this . getActionRefFromAlias ( action ) ;
178+ if ( ref_action ) {
179+ anim_key = this . getAnimationKey ( ref_action , animation ) ;
180+ }
181+ if ( ! sprite . animations . frameData . getFrameByName ( `${ anim_key } ${ SpriteBase . ACTION_ANIM_SEPARATOR } 00` ) ) {
182+ this . data . logger . log_message (
183+ `Animation '${ anim_key } ' is not valid for action '${ action } ' for sprite '${ this . key_name } '.` ,
184+ msg_types . ERROR
185+ ) ;
186+ }
136187 }
137188 } else {
138189 this . data . logger . log_message ( `Action '${ action } ' not available for '${ this . key_name } '.` ) ;
0 commit comments