Jump to content

I am new to Phaser. I need help on my animations


SamuelObregon
 Share

Recommended Posts

This is the code.

 

 

<html>

<head>

<style type="text/css">

 

  body {

            margin: 0;

            /*background:url(./assets/backpage.png);*/

        }

</style>

<script type="text/javascript" src="js/phaser.min.js"> </script>

 

 

<script type="text/javascript">

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

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

 

function preload() {

  // I have loaded my assests here

game.load.image('explosions', './assets/explosions.png');

game.load.image('platform', './assets/platform.png');

game.load.image('forest', './assets/forest.png');

game.load.spritesheet('person', './assets/player_sprite.png', 32, 48);

}

 

var platform;

 

function create() {

 

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

// game.add.sprite(0, 0, 'platform');

//game.add.sprite(0,0, 'person');

 

platform = game.add.group();

platform.enableBody = true;

var ground = platform.create(0, game.world.height - 64, 'platform');

    ground.scale.setTo(2,2);

    ground.body.immovable = true;

var ledge = platform.create(-150,250,'platform');

  ledge.body.immovable = true;

 

 

 

// player movement

 var player;

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

 

 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, 4], 10, true);

 player.animations.add('right', [6, 7, 8, 9], 10, true);

 

 

// moving the player

 

  player.body.velocity.x = 0;

 

    if (cursors.left.isDown)

    {

        //  Move to the left

        player.body.velocity.x = -150;

 

        player.animations.play('left');

    }

    else if (cursors.right.isDown)

    {

        //  Move to the right

        player.body.velocity.x = 150;

 

        player.animations.play('right');

    }

    else

    {

        //  Stand still

        player.animations.stop();

 

        player.frame = 4;

    }

 

    //  Allow the player to stay in the ground.

    if (cursors.up.isDown && player.body.touching.down)

    {

        player.body.velocity.y = -350;

    }

 

}

 

function update() {

// Im still unsure about this

 

}

</script>

 

 

</head>

<body>

 

</body>

</body>

</html>

 

 

 

 

 

 

My character is stable and wont move at all.  

Link to comment
Share on other sites

Okay I just did the switch but now I get a black screen.

 

// moving the player
 
  player.body.velocity.x = 0;
 
    if (cursors.left.isDown)
    {
        //  Move to the left
        player.body.velocity.x = -150;
 
        player.animations.play('left');
    }
    else if (cursors.right.isDown)
    {
        //  Move to the right
        player.body.velocity.x = 150;
 
        player.animations.play('right');
    }
    else
    {
        //  Stand still
        player.animations.stop();
 
        player.frame = 4;
    }
 
    //  Allow the player to stay in the ground.
    if (cursors.up.isDown && player.body.touching.down)
    {
        player.body.velocity.y = -350;
    }
 
 
the following above is what I moved into the update
 
 
 
This is also an error that I keep getting.
 
 
Uncaught TypeError: Cannot read property 'left' of undefined
 
update @ part1.html:83b.StateManager.update @ phaser.min.js:6b.Game.update @ phaser.min.js:8b.RequestAnimationFrame.updateRAF @ phaser.min.js:11b.RequestAnimationFrame.start.window.requestAnimationFrame.forceSetTimeOut._onLoo
Link to comment
Share on other sites

The console will point out the line in your code where the error happened. 

The code is trying to access cursors.left.isDown but cursors was never defined in your code. Try adding cursors = game.input.keyboard.createCursorKeys(); in the create section.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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