Search code examples
javaandroidcocos2d-iphonecocos2d-android

How to set frame of CCSprite


I have class of character in which is CCSprite variable. Character is moving to point and animating walking animation after touching button or jumping, everything works fine, but after animation CCSprite has bad frame(one of walking frame). At the end of my update function I wrote this code, but then it is just like frozen, character cant jump or walk more, number of running actions is always 1:

if(this.sprite.numberOfRunningActions() == 0){
  if(this.state != CharacterState.IDLE){
    this.changeState(CharacterState.IDLE); // without this row it works still fine
  }
}

changeState function:

public void changeState(CharacterState state){
  sprite.stopAllActions();
  this.state = state;
  switch(state){
    case IDLE:{ this.sprite = CCSprite.sprite(CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName("Player.png")); break;}
    case WALK_LEFT:{ this.sprite.runAction(wAction); break; }
     .
     .
     .

Solution

  • Ok, I solved it alone. :) I have created class variable of CCSpriteFrame type.

    CCSpriteFrame frame_idle;
    

    In class constructor:

    frame_idle = CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName("Player.png");
    

    And case IDLE I have changed to:

    case IDLE:{ this.sprite.setDisplayFrame(frame_idle); break; }