NickCastello

Getting Uncaught TypeError: Cannot read property 'index' of undefined on phaser animation

Recommended Posts

I keep having a problem with animations in my code. I try playing an animation and i always get the same message:

"Uncaught TypeError: Cannot read property 'index' of undefined"

and it always points to the same line of my code which is:

this.player.animations.play('left');

Im not sure what is going wrong with it

Heres my player sprite part of my create function:

this.cursors = this.game.input.keyboard.createCursorKeys();

this.player = this.game.add.sprite(10, 10, 'player');
this.game.physics.arcade.enable(this.player);
this.player.scale.setTo(.4,.4);


//the camera will follow the player in the world
this.game.camera.follow(this.player);


this.player.animations.add('left', [0, 1, 2, 3, 4, 5, 6], 10, true, false);
this.player.animations.add('right', [11, 12, 13, 14, 15, 16, 17], 10, true, false);

My temporary update function:

update: function() {

  if (this.cursors.left.isDown)
{
    //  Move to the left
    this.player.body.velocity.x = -150;

    this.player.animations.play('left');
}
else if (this.cursors.right.isDown)
{
    //  Move to the right
    this.player.body.velocity.x = 150;

    this.player.animations.play('right');
}
else
{
    //  Stand still
    this.player.animations.stop();

    this.player.frame = 4;
}

//  Allow the player to jump if they are touching the ground.
if (this.cursors.up.isDown && player.body.touching.down)
{
    this.player.body.velocity.y = -350;
}

}

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.