开发者

Animating a sprite in cocos2d , box2d and Xcode

开发者 https://www.devze.com 2023-03-14 17:33 出处:网络
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

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 implem开发者_运维百科entation 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.


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];


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

0

精彩评论

暂无评论...
验证码 换一张
取 消