cocos2d sprite repeat animation forever(cocos2d sprite 永远重复动画)
问题描述
在我的 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: delay: restoreOriginalFrame:
在 cocos2d v2.0 中被弃用了.所以我的问题是,在 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];
并在运行中():
[_hero runAction:[CCAnimate actionWithAnimation:_keepRunning]];
但它仍然不起作用.我该怎么办?
But it still doesn't work. What should I do?
推荐答案
首先你从 'http://www.codeandweb.com/texturepacker/download'并制作 p-List 并在代码下方使用它
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];
收集帧列表(精灵)
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);
Sprite RepeaetForever for manSprite
Sprite RepeaetForever for manSprite
id first=[CCSequence actions:[CCRepeatForever actionWithAction:
[CCAnimate actionWithAnimation:walkAnim]],nil];
[manSprite runAction:first];
这篇关于cocos2d sprite 永远重复动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:cocos2d sprite 永远重复动画
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01