我有一個奇怪的Cocos2d動畫問題。Cocos2d CCSprite動畫無法加載
在init中,如果我的createAndRunAnimationonSprite方法被調用完美。
但是,如果我等待並使用方法showSprite將其分配給按鈕,則精靈將永遠不會顯示。我對這種行爲發生的原因感到茫然。我沒有其他方法或類。
HelloWorldLayer.h
@interface AnimationViewerLayer : CCLayer
{
CCSprite *sprite;
}
HelloWorldLayer.m
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if((self=[super init])) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
// initialize the sprite
sprite = [CCSprite node];
sprite.position = ccp(winSize.width/2 , winSize.height/2);
[self addChild: sprite];
// If this is uncommented the sprite will show up.
// [self createAndRunAnimationOnSprite];
}
return self;
}
- (void) showSprite {
[self createAndRunAnimationOnSprite];
}
-(void) createAndRunAnimationOnSprite {
// stop all previous ones
[sprite stopAllActions];
NSLog(@"Create and Run Animation");
CGSize winSize = [CCDirector sharedDirector].winSize;
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"ThisSprite.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"ThisSprite.png"];
[self addChild:spriteSheet];
NSMutableArray *aniFrames = [NSMutableArray array];
for(int i = 1; i <= 3; ++i) {
[aniFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"ThisSprite_walking_%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithFrames:aniFrames delay:0.1f];
sprite = [CCSprite spriteWithSpriteFrameName:@"ThisSprite_walking_1.png"];
sprite.position = ccp(winSize.width/2, winSize.height/2);
id walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
[sprite runAction:walkAction];
[spriteSheet addChild:sprite];
}
即使刪除[self addChild:sprite]後,問題仍然是一樣的。在初始化期間,我可以調用createAndRunAnimationOnSprite並顯示spirte,但不會在之後。 – JasonBourne
有沒有其他的指導?我真的把我的頭髮拉出來。 @ LearnCocos2D – JasonBourne