【问题标题】:Cocos2d CCSprite animation fails to loadCocos2d CCSprite 动画加载失败
【发布时间】:2013-02-21 15:23:36
【问题描述】:

我遇到了一个奇怪的 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];


}

【问题讨论】:

    标签: animation cocos2d-iphone ccsprite ccspritebatchnode


    【解决方案1】:

    您添加了两次精灵。在 init 中将其添加到 self,在 createAndRunAnimation 中将其添加到 spriteSheet。这通常会导致应用程序的断言和中止执行(异常)。

    【讨论】:

    • 即使在删除 [self addChild: sprite]; 之后,问题仍然存在。在初始化期间,我可以调用 createAndRunAnimationOnSprite 并且精灵出现,但之后没有。
    • 还有其他指导吗?我真的要拔头发了。 @LearnCocos2D
    【解决方案2】:

    您的图片可能有问题,您的图片名称不准确或图片已损坏。

    使用其他一些虚拟图像来运行动画,并尝试使用您的任何动画帧(即“ThisSprite_walking_1.png”)制作精灵,然后查看它是否有效

    【讨论】:

      【解决方案3】:

      用这个替换你的代码我希望这能工作

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

      }

      -(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];
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-05
        相关资源
        最近更新 更多