Jump to content

[Solved]How to get the TextureFrame number of a playing animation


khleug35
 Share

Recommended Posts

Hello EveryOne :lol:

 How to get the TextureFrame number on Animation?? Thanks you very much

  this.sheet = new game.SpriteSheet('panda.png', 150 ,111);
  this.sprite = new game.Animation(this.sheet.textures);
  this.sprite.addAnim('attack', [43,44,45,46,47,48], { speed: 12, loop: false });


if(this.sprite.xxxxxxxxx.textureframe == 46){  // 
  // dosomething.......
}

 

My solution is that, but I don't know Is it the best way to solve the problem?? Thanks

  if(this.sprite.currentAnim == this.sprite.anims['attack']){
      for(var i =0; i < this.sprite.anims.attack.textures.length;i++){
       if(i ===4){  // [43,44,45,46,47,48]  = [0,1,2,3,4,5,6] due to I want to get frame 46, so i type 4. 
              //doSomething
            }
        }
      }

 

It is very easy to get the TextureFrame in Phaser 3, How about Panda2, Thank you

//Phaser 3
if (this.sprite.anims.currentFrame.textureFrame === 46){
  // dosomething
}

 

Link to comment
Share on other sites

@Wolfsbane

Thank you for reply,

The following is my simple project

I try to  use chrome console debug to find the answer

but still no idea, thx :)

game.module(
    'game.main'
)
.body(function() {
game.addAsset('player.png');

game.createScene('Main', {
    init: function() {
        this.sheet = new game.SpriteSheet('player.png', 149 ,237);
        this.anim = new game.Animation(this.sheet.textures);
        this.anim.addAnim('run', [3, 4, 5, 6, 7, 8, 9]);
        this.anim.play('run');
        this.anim.addTo(this.stage);
        
      console.log(this.anim);
    }
});
});

I can't upload the 'player.png' to this forum. so plz kindly download the 'player.png' to this link , thx

 https://www.panda2.io/content/examples/media/player.png

 

Link to comment
Share on other sites

On 7/20/2019 at 6:33 PM, khleug35 said:

My solution is that, but I don't know Is it the best way to solve the problem?? Thanks


  if(this.sprite.currentAnim == this.sprite.anims['attack']){
      for(var i =0; i < this.sprite.anims.attack.textures.length;i++){
       if(i ===4){  // [43,44,45,46,47,48]  = [0,1,2,3,4,5,6] due to I want to get frame 46, so i type 4. 
              //doSomething
            }
        }
      }

 

No need to do this at least: anim has an attribute currentFrame. So you can simply do:

if (this.anim.currentFrame === 4){ //currentFrame will start from 0-size of animation.
  // dosomething
}

 

I took a look, and we really don't seem to have the textureFrame==46 style..  It's convenient, but if you're going to hard-code a number in there, I much prefer just hard coding to the 0,1,2,3 convention. 

Think of this: You know you're animation framing is going to be 0=first frame, 1=second frame. Now: If you update your spritesheet.png, and suddenly

If you hard-code in =46, and then the image changes, then both the image, and your code has to change.

Does that make sense? 

 

 

Link to comment
Share on other sites

  • 4 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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