【问题标题】:Cocos2d Sprite Sheet Not AnimatingCocos2d Sprite Sheet 没有动画
【发布时间】:2012-08-29 12:05:41
【问题描述】:

SpriteSheets 是我无法理解的一件事。我正在寻找一种非常清晰易懂的方式来了解它们。我已经阅读并研究了它们,但仍然有问题。

我不知道为什么我的代码不起作用。

-(void) addPlayer {

CGSize winSize = [[CCDirector sharedDirector] winSize];

CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"walk1.png"];
sprite.position = ccp(winSize.width/2, winSize.height/2);

CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:@"player-walk.png"];
[batchNode addChild:sprite];
[self addChild:batchNode z:350];






NSMutableArray *frames = [NSMutableArray array];
for(int i = 1; i <= 4; ++i) {
    [frames addObject:
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"walk%d.png", i]]];
}

CCAnimation *anim = [CCAnimation animation];

CCAnimate *animate = [CCAnimate actionWithAnimation:anim];
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:animate];
[self runAction:repeat];
}

为什么我的播放器没有动画?

【问题讨论】:

  • 在您的代码中看不到,您在哪里使用帧数组。您只需创建空动画对象
  • 您好,感谢您的帮助。我正在尝试使用 CCSpriteFrameCache 从 plist 中获取帧。

标签: cocos2d-iphone sprite


【解决方案1】:

Spritesheet 只是一种存储纹理的便捷方式。

您的 CCAnimation 对象没有帧。你收集了一组帧,但你从不使用它。改变你的

CCAnimation *anim = [CCAnimation animation];

行到

CCAnimation *anim = [CCAnimation animationWithFrames:frames delay:0.2f];

如果你想动画一个精灵,你必须在这个精灵上运行动画动作。使用[sprite runAction:repeat] 而不是[self runAction:repeat];

【讨论】:

  • 嘿,非常感谢您的帮助。我今天花了一整天试图弄清楚。 animationWithFrames 已弃用。我使用了animationWithSpriteFrames,它工作得很好。再次感谢您。
猜你喜欢
  • 1970-01-01
  • 2014-02-02
  • 2014-10-03
  • 2012-04-19
  • 1970-01-01
  • 2014-03-08
  • 2014-03-17
  • 2018-01-16
  • 2012-11-02
相关资源
最近更新 更多