Changing frames in Phaser

Recommended Posts

Hi guys,


I have this code in create()

lootCrates = this.physics.add.staticGroup();
lootCrates.create(600, 362, 'crate', frame = 1);

this.physics.add.overlap(player, lootCrates, brokenCrate, null, this);

I then have this function for when the player touches the crate: 

function brokenCrate(player, crate) {
            crate.frame = 0;

I simply want to change the frame to the crate being broken.... However, I get this error: "Cannot read property 'source' of undefined"


But earlier I did the exact same thing for a speedPotion where I did speedPotion.disableBody(true, true) and that worked perfectly.


Why is changing frame giving me an error? Thanks!

Share this post

Link to post
Share on other sites

Just to quickly explain why the way you did it before caused an error - 'frame' is a property of Game Objects and it contains a reference to an AnimationFrame object. The renderer uses this to draw the Sprite with. So when you set it to a zero it freaked the renderer out, because it was expecting a Frame object and instead it got a number.

Also, to change a texture you can use setTexture (which takes a frame parameter as well)

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.