Jump to content

Animation is played only when the key is pressed continuously


Deino
 Share

Recommended Posts

This is the code from my partially complete game, I ran into this problem with the animation part 

var game = new Phaser.Game(1350,600,Phaser.AUTO,'gameDiv');var mainState = {	preload: function()	{		game.load.spritesheet('dude','assets/dude.png',32,48);		game.load.image('wh','assets/wallHorizontal.png');        game.load.image('ground','assets/Ground.png');        	},	create: function()	{				game.stage.backgroundColor = '#DB7093';		this.dude = game.add.sprite(80,482,'dude');		this.wh = game.add.sprite(10,10,'wh');        this.ground = game.add.sprite(0,530,'ground');        this.ground2 = game.add.sprite(476,530,'ground');        this.ground3 = game.add.sprite(952,530,'ground');   		this.physics.enable(this.wh,Phaser.Physics.ARCADE);		this.wh.body.immovable = true;		this.physics.enable(this.dude,Phaser.Physics.ARCADE);		this.facing = 'left';		this.cursors = this.input.keyboard.createCursorKeys();        this.physics.enable(this.ground,Phaser.Physics.ARCADE);        this.ground.body.immovable = true;        this.dude.animations.add('left', [0, 1, 2, 3], 10, true);              this.dude.animations.add('right', [5, 6, 7, 8], 10, true);	},	update: function()	{     game.physics.arcade.collide(this.dude,this.wh,null,null,this);     this.dude.body.velocity.x = 0;           if(this.cursors.left.isDown)    {        this.dude.body.velocity.x = -300;        if(this.facing != 'left')    {        this.dude.animations.play('left');        this.facing = 'left';          }    }    else if(this.cursors.right.isDown)     {     this.dude.body.velocity.x = 300;     if(this.facing != 'right')         {         this.dude.animations.play('right');         this.facing = 'right';             }     }      else    {        if (this.facing != 'idle')        {            this.dude.animations.stop();            if (this.facing == 'left')            {                this.dude.frame = 0;            }            else            {                this.dude.frame = 5;            }            facing = 'idle';        }    }             if(this.dude.inWorld == false)     {     	game.state.start('main');     }     	}};game.state.add('main',mainState);game.state.start('main');
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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