我有一个奇怪的Cocos2d动画问题。
在初始化时,如果我的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];
}
答案 0 :(得分:0)
你要加两次精灵。在init中,将其添加到self,在createAndRunAnimation中将其添加到spriteSheet。这通常应该导致应用程序的断言和中止执行(例外)。
答案 1 :(得分:0)
您的图片可能存在问题,可能是图片名称不准确或图片损坏。
使用其他虚拟图像来运行动画,并尝试使用任何动画帧制作精灵,即“ThisSprite_walking_1.png”,然后查看它是否有效
答案 2 :(得分:0)
用这个替换你的代码我希望这会起作用
- (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
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"ThisSprite.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"ThisSprite.png"];
[self addChild:spriteSheet];
sprite = [CCSprite spriteWithSpriteFrameName:@"ThisSprite_walking_1.png"];
sprite.position = ccp(winSize.width/2, winSize.height/2);
[spriteSheet addChild: sprite];
// If this is uncommented the sprite will show up.
//[self createAndRunAnimationOnSprite];
}
return self;
}
- (void)createAndRunAnimationOnSprite {
// stop all previous ones
[sprite stopAllActions];
NSLog(@"Create and Run Animation");
CGSize winSize = [CCDirector sharedDirector].winSize;
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];
id walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
[sprite runAction:walkAction];
}