Skip to content

Commit 20ea562

Browse files
committed
create no longer throws a warning, returns existing animation. Also added exists method.
1 parent 1a407bc commit 20ea562

1 file changed

Lines changed: 40 additions & 9 deletions

File tree

src/animations/AnimationManager.js

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,32 +154,63 @@ var AnimationManager = new Class({
154154
return this;
155155
},
156156

157+
/**
158+
* Checks to see if the given key is already in use within the Animation Manager or not.
159+
*
160+
* Animations are global. Keys created in one scene can be used from any other Scene in your game. They are not Scene specific.
161+
*
162+
* @method Phaser.Animations.AnimationManager#exists
163+
* @since 3.16.0
164+
*
165+
* @param {string} key - The key of the Animation to check.
166+
*
167+
* @return {boolean} `true` if the Animation already exists in the Animation Manager, or `false` if the key is available.
168+
*/
169+
exists: function (key)
170+
{
171+
return this.anims.has(key);
172+
},
173+
157174
/**
158175
* Creates a new Animation and adds it to the Animation Manager.
176+
*
177+
* Animations are global. Once created, you can use them in any Scene in your game. They are not Scene specific.
178+
*
179+
* If an invalid key is given this method will return `false`.
180+
*
181+
* If you pass the key of an animation that already exists in the Animation Manager, that animation will be returned.
182+
*
183+
* A brand new animation is only created if the key is valid and not already in use.
184+
*
185+
* If you wish to re-use an existing key, call `AnimationManager.remove` first, then this method.
159186
*
160187
* @method Phaser.Animations.AnimationManager#create
161188
* @fires AddAnimationEvent
162189
* @since 3.0.0
163190
*
164191
* @param {AnimationConfig} config - The configuration settings for the Animation.
165192
*
166-
* @return {Phaser.Animations.Animation} The Animation that was created.
193+
* @return {(Phaser.Animations.Animation|false)} The Animation that was created, or `false` is the key is already in use.
167194
*/
168195
create: function (config)
169196
{
170197
var key = config.key;
171198

172-
if (!key || this.anims.has(key))
173-
{
174-
console.warn('Invalid Animation Key, or Key already in use: ' + key);
175-
return;
176-
}
199+
var anim = false;
177200

178-
var anim = new Animation(this, key, config);
201+
if (key)
202+
{
203+
anim = this.get(key);
179204

180-
this.anims.set(key, anim);
205+
if (!anim)
206+
{
207+
anim = new Animation(this, key, config);
181208

182-
this.emit('add', key, anim);
209+
this.anims.set(key, anim);
210+
211+
this.emit('add', key, anim);
212+
}
213+
}
183214

184215
return anim;
185216
},

0 commit comments

Comments
 (0)