Jump to content

Add animations. HELP.


dinhoinz
 Share

Recommended Posts

Hello, i'm new to programming and i'm using phaser just for a few days.

I'm trying to add a animation to my player, but when i do that, the game crashes.

What i'm doing wrong?

thats my code:

 

window.onload = function() {

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

			    var player;

			    function preload () {
			        game.load.atlasJSONHash('walk', 'walk.png', 'walk.json');
			        game.load.atlasJSONHash('idle', 'idle.png', 'idle.json');
			    }

			    function create () {
			        //var background = game.add.sprite(0, 0, 'cityscene', 'background');

			        player = game.add.sprite(0, 325, 'walk', 'walk_0001.png');
			        player.scale.setTo(0.5,0.5);
			        player.animations.add('walk', Phaser.Animation.generateFrameNames('walk_', 1, 31, '.png', 4), 10, true, false);
			        player.anchor.setTo(.5,.5);
			        player.animations.currentAnim.speed = 30; // Altera velocidade da animação.

			        
			        player.animations.add('idle', Phaser.Animation.generateFrameNames('idle_', 1, 99, '.png', 4), 10, true, false);
			    }

			    function update() {


			         //player.x += 6;
			        if(player.x > 800)
			         {
			             player.x = 0;
			         }
			         else if (player.x < 0){
			         	player.x = 800;
			         }
			        if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
    {
    	player.scale.setTo(-0.5,0.5);
    	player.animations.play('walk');
        player.x -= 4;
    }
    else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
    {
    	player.scale.setTo(0.5,0.5);
        player.x += 4;
        player.animations.play('walk');
    }
    else
    {
        //  Stand still
        //player.animations.play(idle);
    }

    // if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
    // {
    //     player.y -= 4;
    // }
    // else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
    // {
    //     player.y += 4;
    // }
			    }
			};

 

I'm trying to add that "idle" animation.

 

Thanks! :D

(I'm not a native english speaker.)

 

Edit:

 

I can normally put the walk animation or the idle animation, when i put both animations, one of them don't work...

Link to comment
Share on other sites

28 minutes ago, Skeptron said:

Aren't the animations supposed to be in the same file? It would make your life easier anyways.

yeah, but they are supposed to work even if its on different files...

any idea about why isn't working?

Link to comment
Share on other sites

The console is clean.

putting everything in the same file worked!

But with different files still not working.

btw, i'll use in the same file. Thx.

On 13/06/2017 at 11:32 AM, Skeptron said:

Aren't the animations supposed to be in the same file? It would make your life easier anyways.

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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