Jump to content

My player will not move


akwa6950
 Share

Recommended Posts

Every time i put the create cursor in it gives this eror: Uncaught TypeError: Cannot read property 'index' of undefined
    at c.Animation.updateCurrentFrame (phaser.min.js:19)
    at c.Animation.play (phaser.min.js:19)
    at c.AnimationManager.play (phaser.min.js:19)

This is the code i'm using:


var game = new Phaser.Game(1200, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });

function preload() {
    game.load.image('sky', 'asset/sky.png');
    game.load.image('ground', 'asset/ground.png');
    game.load.image('burger', 'asset/burger.png');
    game.load.spritesheet('dude', 'asset/dude.png');
}

var player;
var platforms;
var cursors;


function create() {

    game.physics.startSystem(Phaser.Physics.ARCADE);

  
    game.add.sprite(0, 0, 'sky');

  
    platforms = game.add.group();

 
    platforms.enableBody = true;


    var ground = platforms.create(0, game.world.height - 50, 'ground');

    
    ground.scale.setTo(5, 5);


    ground.body.immovable = true;

 
    var ledge = platforms.create(700, 400, 'ground');

    ledge.body.immovable = true;

    ledge = platforms.create(-100, 250, 'ground');

    ledge.body.immovable = true;


    player = game.add.sprite(32, game.world.height - 150, 'dude');

    game.physics.arcade.enable(player);

    player.body.bounce.y = 0.2;
    player.body.gravity.y = 300;
    player.body.collideWorldBounds = true;

    player.animations.add('left', [0, 1, 2, 3], 10, true);
    player.animations.add('right', [5, 6, 7, 8], 10, true);

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

}

function update() {

    game.physics.arcade.collide(player, platforms);


    player.body.velocity.x = 0;

    if (cursors.left.isDown)
    {
        
        player.body.velocity.x = -150;

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

        player.animations.play('right');
    }
    else
    {
    
        player.animations.stop();

        player.frame = 4;
    }
    
    if (cursors.up.isDown && player.body.touching.down)
    {
        player.body.velocity.y = -350;
    }
 }

Link to comment
Share on other sites

Tip: use non-minified Phaser for dev to get more descriptive errors.

Looks like your animations are failing to play.  Have a look at 

   player.animations.add('left', [0, 1, 2, 3], 10, true);
    player.animations.add('right', [5, 6, 7, 8], 10, true);

And see if those indexes are configured correctly (you have enough frames in the image, and so forth.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...