[英]Animating a sprite in cocos2d , box2d and Xcode
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.
我打算用在xcode中使用cocos2d和box2d添加的精灵来制作动画。 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" 实现文件如下:#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 另外,您可能要考虑不要将操作重新分配给CCBlink并进行其他操作并致电
[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.
Sprite动画最简单的方法之一。
https://sites.google.com/site/rajanallathambi1/cocos2d-tutorials/sprite-animation-without-plist-file https://sites.google.com/site/rajanallathambi1/cocos2d-tutorials/sprite-animation-without-plist-file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.