cocos2d的精灵重复动画永远 [英] cocos2d sprite repeat animation forever
问题描述
在我的iOS游戏,我希望英雄继续运行,直到屏幕被触摸和主人公应该跳。所以我写:结果
在.h文件中:
In my iOS game, I hope the hero keep running until screen is touched and the hero should jump. So I write:
In .h file:
@interface Hero : CCSprite {
CCSprite *_hero;
id _keepRunning;
}
@property(nonatomic,retain) id keepRunning;
在.m文件:
@synthesize keepRunning = _keepRunning;
-(id) init {
_keepRunning = [CCRepeatForever actionWithAction:[CCAnimate actionWithSpriteSequence:@"heroRun%04d.png"
numFrames:30
delay:0.02f
restoreOriginalFrame:NO]];
}
随后的比赛开始时,我调用run()方法:
Then when the game starts, I call run () method:
-(void) run {
[_hero stopAllActions];
[_hero runAction:_keepRunning];
_heroState = RUNNING;
}
然后我发现 CCAnimate actionWithSpriteSequence:numFrames:延迟:restoreOriginalFrame:
是在cocos2d 2.0 pcated德$ P $。所以,我的问题是,在cocos2d V2.0,我怎么能实现这个动画?也就是说,让我的英雄正在运行?谢谢!结果
编辑:结果
我曾尝试这样的:
Then I found CCAnimate actionWithSpriteSequence: numFrames: delay: restoreOriginalFrame:
is deprecated in cocos2d v2.0. So my question is, in cocos2d v2.0, how can I implement this animation? Namely, keep my hero running? Thank you!
I have tried this:
-(CCAnimation*) getMyAnimationWithFramesName:(NSString*)nameFormat numFrames:(int)numFrames delay:(float)delay {
NSMutableArray *frames = [[NSMutableArray alloc] init];
for (int i = 1; i <= numFrames; i++) {
NSString *frameName = [NSString stringWithFormat:nameFormat,i];
[frames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:frameName]];
}
CCAnimation *ani = [CCAnimation animationWithSpriteFrames:frames delay:delay];
return ani;
}
在随后的init()
Then in init ():
_keepRunning = [self getMyAnimationWithFramesName:@"heroRun%04d.png" numFrames:30 delay:0.02f];
和中的run():
[_hero runAction:[CCAnimate actionWithAnimation:_keepRunning]];
但它仍然无法正常工作。我应该怎么办?
But it still doesn't work. What should I do?
推荐答案
首先,你从这里下载纹理的 HTTP://www.$c$candweb.com/texturepacker/download
并P列表,并使用它低于code
First of all you download Texture from 'http://www.codeandweb.com/texturepacker/download' and make p-List and use it below code
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"AnimatedMan.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"AnimatedMan.png"];
[self addChild:spriteSheet];
收集的帧的列表(精灵)
Gather the list of frames(sprite)
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for (int i=1; i<=6; i++) {
[walkAnimFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"step0%d.png",i]]];
}
给行动雪碧
CCAnimation *walkAnim = [CCAnimation animationWithSpriteFrames:walkAnimFrames delay:0.1f];
manSprite=[CCSprite spriteWithSpriteFrameName:@"step01.png"];
manSprite.position=ccp(winsize.width/2, winsize.height/2-40);
雪碧RepeaetForever为manSprite
Sprite RepeaetForever for manSprite
id first=[CCSequence actions:[CCRepeatForever actionWithAction:
[CCAnimate actionWithAnimation:walkAnim]],nil];
[manSprite runAction:first];
这篇关于cocos2d的精灵重复动画永远的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!