2013-01-31 49 views
0

我有一個奇怪的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]; 


} 

回答

0

您要添加的精靈的兩倍。在init中,將其添加到self,在createAndRunAnimation中將其添加到spriteSheet。這通常應該導致應用程序的斷言和放棄執行(異常)。

+0

即使刪除[self addChild:sprite]後,問題仍然是一樣的。在初始化期間,我可以調用createAndRunAnimationOnSprite並顯示spirte,但不會在之後。 – JasonBourne

+0

有沒有其他的指導?我真的把我的頭髮拉出來。 @ LearnCocos2D – JasonBourne

0

圖像名稱不準確或圖像損壞可能是圖像有問題。

使用一些其他的虛擬圖像運行的動畫,也儘量讓精靈與您的任何動畫幀即「ThisSprite_walking_1.png」,然後看看是否可行與否

0

有了這個,我希望替換代碼這將工作

- (ID)初始 {

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

}

- (空)createAndRunAnimationOnS prite {

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


}