【问题标题】:Animation in Cocos2d.Cocos2d 中的动画。
【发布时间】:2010-06-06 14:20:28
【问题描述】:
我正在尝试在cocos2D 中创建sprite animate。我相信我已经设置了动画,但是如何将animating sprite 绘制到屏幕上?这是我所拥有的:
id anim = [[[CCAnimation alloc] initWithName:@"char_walking" delay:1/12.0] autorelease];
[anim addFrame:@"run2.png"];
[anim addFrame:@"run1.png"];
[anim addFrame:@"run3.png"];
[anim addFrame:@"run4.png"];
[anim addFrame:@"run3.png"];
[anim addFrame:@"run1.png"];
id myAction = [CCAnimate actionWithAnimation:anim];
id repeating = [CCRepeatForever actionWithAction:myAction];
[character do:repeating];
character = [CCSprite spriteWithSpriteFrame:anim];
character.position = ccp(160, 240);
[self addChild:character];
提前致谢,
约翰
【问题讨论】:
标签:
iphone
animation
cocos2d-iphone
sprite
【解决方案1】:
也许这只是一个剪切和粘贴错误,但看起来您是在告诉精灵在创建动画之前重复动画,所以您添加到节点的角色精灵永远不会收到发送的 CCAnimate 动作给它。
【解决方案2】:
您没有按照 addFrame 方法的要求添加 spriteFrames。
用这一行:
[字符做:重复];
也许你正在寻找[character runAction:repeating];
字符 = [CCSprite
spriteWithSpriteFrame:anim];
这里,anim 不是 spriteFrame,而是 CCanimation。
基本上,你有一些问题。
您可以尝试使用zwoptex 来创建您的 .plist 文件:
CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
[cache addSpriteFramesWithFile:@"runImages.plist"];
CCSprite *startingImage = [CCSprite spriteWithSpriteFrameName:@"run1.png"];
[self addChild:startingImage];
//创建你的精灵帧
NSArray *animFrames = [[NSArray alloc] initWithCapacity:6];
[animFrames addFrame:[cache spriteFrameByName:@"run2.png"]];
[animFrames addFrame:[cache spriteFrameByName:@"run1.png"]];
[animFrames addFrame:[cache spriteFrameByName:@"run3.png"]];
[animFrames addFrame:[cache spriteFrameByName:@"run4.png"]];
[animFrames addFrame:[cache spriteFrameByName:@"run3.png"]];
[animFrames addFrame:[cache spriteFrameByName:@"run1.png"]];
//运行动画
CCAnimation *animation = [CCAnimation animationWithName:@"char_walking" delay:1/12.0 frames:animFrames];
id anim = [CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO];
[startingImage runAction:anim];