1

Please, I need help in making this code work. I intend to do an animation with a sprite I added using cocos2d and box2d in xcode. But for some odd reasons I cannot get the sprite to animate repeatedly.

This code builds successfully but animates only once. Can anyone help and tell me what I am not doing right?

The implementation file are as follows: #import "Mosquito.h" #import "Box2DHelpers.h"

@implementation Mosquito

@synthesize flyingAnim;

 - (void) dealloc{
 [flyingAnim release];

[super dealloc];
}




 -(void)initAnimations {

     flyingAnim = [self loadPlistForAnimationWithName:@"flyingAnim"
  andClassName:NSStringFromClass([self class])];
 [[CCAnimationCache sharedAnimationCache] addAnimation:flyingAnim
          name:@"flyingAnim"];

          }

    -(void)changeState:(CharacterStates)newState {
    [self stopAllActions];
    id action = nil;
    //  id flyingAction = nil;
    //CGPoint newPosition;
    [self setCharacterState:newState];
  switch (newState) {

            case kStateIdle:
                [self setDisplayFrame:
                 [[CCSpriteFrameCache sharedSpriteFrameCache]
                  spriteFrameByName:@"Mosquito_anim_1.png"]];
                break;
    case kStateFlying:

        action = [CCAnimate actionWithAnimation:flyingAnim
                           restoreOriginalFrame:NO];
        break;

    case kStateTakingDamage:
        action = [CCBlink actionWithDuration:1.0 blinks:3.0];
        break;

    default:
            //CCLOG(@"Unhandled state %d in Mosquito", newState);
        break;
  }
   if (action != nil) {
    [self runAction:action];
  }
    }




- (id)initWithWorld:(b2World *)theWorld atLocation:(CGPoint)location {
if ((self = [super init])) {
    world = theWorld;
    [self setDisplayFrame:[[CCSpriteFrameCache
                            sharedSpriteFrameCache]
    spriteFrameByName:@"Mosquito_anim_1.png"]];
    gameObjectType = kMosquitoType;
    characterHealth = 100.0f;
    [self createBodyAtLocation:location];
    [self initAnimations];
}
return self;
}


- (void) updateStateWithDeltaTime:(ccTime)deltaTime
         andListOfGameObjects:(CCArray *)listOfGameObjects {
    //CGPoint oldPosition = self.position;



if ((characterState == kStateDestroyed) &&
    ([self numberOfRunningActions] > 0)) {
    return;
}
if (characterState != kStateFlying &&
    [self numberOfRunningActions] == 0) {
    [self changeState:kStateFlying];
}


  }
   @end

Thanks.

0

2 Answers 2

0
id repeatAnimation = [CCRepeatForever actionWithAction:action];

To repeat forever, you need to do that, otherwise you need to just do:

[self runAction:action];

again.

Also, you might want to consider not reassigning action to CCBlink and make another action and call

[self stopAllActions];
id blinkAction = [CCBlink actionWithDuration:1.0 blinks:3.0];
[self runAction:blinkAction];
2
  • thanks man. I will try your suggestions and see what happens. Once again thanks.
    – Zaki
    Jun 22, 2011 at 5:23
  • THANKS A MILLION! Man your suggestions worked...yes it worked. I am grateful for your help. Once again thanks.
    – Zaki
    Jun 22, 2011 at 20:08
0

This may Help you. One of the easiest ways for sprite animation.

https://sites.google.com/site/rajanallathambi1/cocos2d-tutorials/sprite-animation-without-plist-file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.