Jump to content

How to avoid the sprite's animation looping when hold the key??


khleug35
 Share

Recommended Posts

Here is my full code of the example. 

https://jsfiddle.net/d7p7upso/4/

First, I prepared two image, One for main character , one for On Screen control Button

1.crouch.png.b1bc3488dcb6f3db50c7d303403b55c8.png

2.HitBoxButton.png.b5cd2235ccdf3980ce8200f2cfdb181c.png

 

I hope I can do that When User click and hold on the "DOWN" button or Green Button 

The sprite will hold crouch image like this

crouch2.png.1ee7532e5325118944063b380b4f1879.png

But unlucky...... The sprite is looping the sprite

Anyone have idea how to make the spite complete his animation and hold it in last frame???

Thank you very much , I am sorry for my pool english



var game = new Phaser.Game(500, 300, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update});

var crouchFlag = false;


function preload() {

    game.stage.backgroundColor = '#85b5e1';


    game.load.spritesheet('player', 'assets/crouch.png',45,65);
    
    game.load.image('button', 'assets/HitBoxButton.png'); 
}

function create() {

    player = game.add.sprite(100, 100, 'player');
    player.customParams = {};
    cursors = game.input.keyboard.createCursorKeys();
    crouchAnim = player.animations.add('crouch', [0,  1, 2, 3], 10, false);
    crouchAnim.onComplete.add(function () {
          crouchFlag = false; 
        });
    
   Button1 = game.add.button(game.width - 200, game.height - 120, 'button');  
    
    Button1.scale.setTo(0.5);
    
    Button1.events.onInputDown.add(function(){
    player.customParams.isCrouch = true;
    }, this);
    Button1.events.onInputOver.add(function(){
    player.customParams.isCrouch = false;
    }, this);
    Button1.events.onInputUp.add(function(){
    player.customParams.isCrouch = false;
    }, this);
    Button1.events.onInputOut.add(function(){
    player.customParams.isCrouch = false;
    }, this);
}

function update () {
  if (cursors.down.isDown || player.customParams.isCrouch)
        {
     crouchFlag = true;       
        } else{
            
        }   
    
console.log(crouchFlag);    
 if (crouchFlag) {    
   player.animations.play('crouch');
 } else{
     player.frame = 4
 }
}

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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