2013-01-31 1 views
0

이상한 Cocos2d 애니메이션 문제가 있습니다.Cocos2d CCSprite 애니메이션을로드 할 수 없습니다.

내 createAndRunAnimationonSprite 메소드가 호출되면 초기화시 완벽하게 작동합니다.

하지만 show sprite 메서드를 사용하여 버튼에 할당하면 스프라이트가 표시되지 않습니다. 나는이 행동이 왜 일어나고 있는지에 관해서 분실하고있다. 다른 방법이나 수업이 없습니다.

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를 호출 할 수 있습니다. – 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]; 


} 
관련 문제