Texture atlas : anim issue with generateFrameNumbers

Hi there,

I created a texture atlas with different characters sprites for my game. One of the character has two sprites for walking, named “player_walk1.png” and “player_walk2.png”.

Here’s my code for loading the texture atlas (in preload) :
this.load.atlas('my_sprites','assets/my_sprites.png', 'assets/my_sprites.json');

Her’es my code for adding the player :
this.player = this.add.sprite(200, 470, 'my_sprites','player_stop.png');

And here’s my animation
this.anims.create({ key: 'left', frames: this.anims.generateFrameNumbers('my_sprites', {prefix:'player_walk', start: 1, end: 2 }), frameRate: 10, repeat: -1 });

When I call this animation with my cursors in the update, I got this error message in the console

Uncaught TypeError: Cannot read property ‘frame’ of undefined

I think I’m miswriting the this.anims.generateFrameNumbers. Any help for a quick fix or maybe a link to a good example? :slight_smile:

Many thanks for your help!

Based on the docs, generateFrameNumbers does not take prefix as an option, but the GenerateFrameNames does.

Docs:

One point for you!
It doesn’t looks to solve the issue though.Same error message :frowning:

Maybe try to add

suffix: '.png'

As a suffix. THANK YOU YANNICK!

Here’s the valid code :

this.anims.create({
    key: 'right',
    frames: this.anims.generateFrameNames('my_sprites', {prefix:'player_walk', start: 1, end: 2, suffix:'.png' }),
    frameRate: 10,
    repeat: -1
});

Niice. Glad I could help :smile:

By the way, I do have a side question. If it looks more complex, I’ll create a specific post.

What if I want to create an animation (going left) with the same sprites flipped? Should I add them reversed in the Texture atlas or is there an easy way to reuse them?

You can use the same animation and just flip the player.

this.player.setFlipX(true OR false)

Awesome, thanks Yannick! :slight_smile:

You’re welcome!

Please set the topic as solved and leave a like :blush:

Your perfect noob interlocutor doesn’t find the button! :smiley:

Set the :ballot_box_with_check: on the post that solved your question.
The :heart: likes a post.

:wink:

:man_student:

1 Like

I spent much more time on this error than I’d like to admit, so I hope I will at least help some lost dev that ends up here. The problem was that I had frame instead of frames in animation config.