gpt4 book ai didi

objective-c - cocos2d动画总是崩溃

转载 作者:行者123 更新时间:2023-12-03 17:05:28 24 4
gpt4 key购买 nike

我对 cocos2d 和 oc 编程真的很陌生,我一直在试图弄清楚 Sprite 动画是如何工作的。由于最新的更新,我得到了很多不再工作的旧代码。

我有以下内容(一个包含 3 个 Sprite 的 spriteheet,我想要为其设置动画)

不知怎的,它总是崩溃

这是在我的 init 方法中

[[CCSpriteFrameCache sharedSpriteFrameCache ] addSpriteFramesWithFile:@"eno_glasses.plist" ];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"eno_glasses.png"];
[self addChild:spriteSheet];

_body = [CCSprite spriteWithSpriteFrameName: [NSString stringWithFormat:@"eno_glasses02.png"]];
[spriteSheet addChild:_body];
_body.position = CGPointMake(screenSize.width / 2, screenSize.height / 2);



NSMutableArray *animFrames = [NSMutableArray array];
for(int i = 1; i < 3; i++) {
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"eno_glasses0%d.png",i]];
[animFrames addObject:frame];
}
CCAnimation *animation = [CCAnimation animationWithFrames:animFrames delay:0.03f];
CCAnimate* anime = [CCAnimate actionWithAnimation:animation];
[self runAction:anime];

我知道可以找到图像,因为这有效(并且我可以从 plist 01,02,03 获取图像)

我是否在某处忘记了零?

 [[CCSpriteFrameCache sharedSpriteFrameCache ] addSpriteFramesWithFile:@"eno_glasses.plist" ];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"eno_glasses.png"];
[self addChild:spriteSheet];

_body = [CCSprite spriteWithSpriteFrameName: [NSString stringWithFormat:@"eno_glasses02.png"]];
[spriteSheet addChild:_body];
_body.position = CGPointMake(screenSize.width / 2, screenSize.height / 2);

最佳答案

使用:

[_body runAction:anime];

至于重复(假设一次)

id anime1=[CCAnimate actionWithAnimation:animation];
id delay=[CCDelayTime actionWithDuration:2.0f];
id anime2=[CCAnimate actionWithAnimation:animation];
id twice = [CCSequence actions:anime1,delay,anime2,nil];
[_body runAction:twice];

此外,您可能需要选择“eno_glasses01.png”作为起始帧。

关于objective-c - cocos2d动画总是崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9633512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com