Не удается загрузить анимацию Cocos2d CCSprite
У меня странная проблема с анимацией Cocos2d.
На 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];
}
3 ответа
Может быть вероятность того, что с вашим изображением что-то не так, либо имя вашего изображения неверно, либо изображение повреждено.
Используйте некоторые другие фиктивные изображения для запуска анимации, а также попробуйте создать спрайт с любым кадром анимации, например "ThisSprite_walking_1.png", а затем посмотрите, работает ли он или нет
Замените свой код этим, я надеюсь, что это сработает
- (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];
}
Вы добавляете спрайт дважды. В init вы добавляете его в себя, в createAndRunAnimation вы добавляете его в spriteSheet. Это обычно должно вызывать утверждение и прерывать выполнение приложения (исключение).