最近学cocos2d,看一般都用Zwoptex,但是这个软件要钱,所以用了一个在线版本的http://zwoptexapp.com/flashversion/基本功能也很好,需要注意的是,在最后导出png图片和plist文件时,一定要取相同的名字(切记啊切记),下面是素材
//=================================================================================
上代码:
@interface HelloWorldLayer :CCLayer
{
<wbr><wbr><span style="color:#608086">
CCSprite</span> *_sprite;</wbr></wbr>
<wbr><wbr><span style="color:#608086">
CCAction</span> *_walkAction;</wbr></wbr>
<wbr><wbr><span style="color:#608086">
CCAction</span> *_moveAction;</wbr></wbr>
<wbr></wbr>
}
@property(nonatomic,retain)CCSprite*sprite;
@property(nonatomic,retain)CCAction*walkAction;
@property(nonatomic,retain)CCAction*moveAction;
// returns a CCScene that contains the HelloWorldLayer as the onlychild
+(CCScene *) scene;
@end
#import "HelloWorldLayer.h"
// HelloWorldLayer implementation
@implementationHelloWorldLayer
@synthesizesprite=_sprite;
@synthesize moveAction =_moveAction;
@synthesize walkAction =_walkAction;
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScenenode];
<wbr></wbr>
// 'layer' is an autorelease object.
HelloWorldLayer*layer =[HelloWorldLayernode];
<wbr></wbr>
// add layer as a child to scene
[scene
addChild: layer];
<wbr></wbr>
// return the scene
return scene;
}
-(id)init{
<wbr><wbr><wbr></wbr></wbr></wbr>
<wbr><wbr><wbr>if((self=[super init])){</wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><span style="color:#42595e"><span style="color:#000000">[[</span><span style="color:#608086">CCSpriteFrameCache
</span>sharedSpriteFrameCache<span style="color:#000000">]</span>addSpriteFramesWithFile<span style="color:#000000">:</span></span></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>@"AnimHero.plist"];//第1步
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> CCSpriteBatchNode*spriteSheet =[CCSpriteBatchNode<wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="color:#42595e">batchNodeWithFile</span>:<span style="color:#862621">@"AnimHero.png"</span>];</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr> [<span style="color:#772561">self
</span><span style="color:#42595e">addChild</span>:spriteSheet];//第2步</wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><span style="color:#653fa5">NSMutableArray</span> *walkAnimFrames =[<span style="color:#653fa5">NSMutableArray</span><span style="color:#39237e">array</span>];</wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><span style="color:#772561">for</span>(<span style="color:#772561">int</span>i=<span style="color:#2036fb">1</span>; i<=<span style="color:#2036fb">3</span>; i++)</wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr> {</wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr>[walkAnimFrames<span style="color:#39237e">addObject</span>:</wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr> [[</wbr></wbr></wbr></wbr></wbr></wbr></wbr>CCSpriteFrameCacheshared SpriteFrameCache]spriteFrameByName:
<wbr><wbr><wbr><wbr><wbr><wbr><wbr> [<span style="color:#653fa5">NSString</span><span style="color:#39237e">stringWithFormat</span>:<span style="color:#862621">@"hero%i.png"</span>,i]]];</wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr> } //第3步</wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><span style="color:#608086">CCAnimation</span>*walkAnim = [<span style="color:#608086">CCAnimation</span><wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="color:#42595e">animationWithFrames</span>:walkAnimFrames<span style="color:#42595e">delay</span>:<span style="color:#2036fb">0.1f</span>];//第4步</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>_sprite=[CCSprite
spriteWithSpriteFrameNam<wbr>e<span style="color:#000000">:</span><span style="color:#862621">@"hero1.png"</span><span style="color:#000000">];</span></wbr>
<wbr><wbr><wbr><span style="color:#608086">_sprite</span>.<span style="color:#608086">position</span>=<span style="color:#6d4a30">ccp</span>(240,160);</wbr></wbr></wbr>
<wbr></wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>self.walkAction=[CCRepeatForever
actionWithAction:
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>[<span style="color:#608086">CCAnimate
</span><span style="color:#42595e">actionWithAnimation</span>:walkAni<wbr><span style="color:#42595e">restoreOriginalFrame</span>:<span style="color:#772561">NO</span>]];</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr> [</wbr></wbr></wbr></wbr>_spriterunAction:_walkAction];<wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr>[spriteSheet<span style="color:#42595e"> addChild</span>:<span style="color:#608086">_sprite</span>];<wbr>//第5步<wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr>}</wbr></wbr></wbr>
<wbr></wbr>
return self;
}
<wbr></wbr>
-(void) dealloc
{
<wbr><wbr><span style="color:#772561">self</span>.<span style="color:#608086">sprite</span>=<span style="color:#772561">nil</span>;</wbr></wbr>
<wbr><wbr></wbr></wbr>self.moveAction=nil;
<wbr><wbr></wbr></wbr>self.walkAction=nil;
<wbr><wbr>[</wbr></wbr>superdealloc];
}
@end
逐步解析:
第1步:缓冲sprite帧和纹理。<wbr><wbr><wbr></wbr></wbr></wbr>[[CCSpriteFrameCache<wbr></wbr>sharedSpriteFrameCache]<wbr></wbr>addSpriteFramesWithFile:
<wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>@"AnimHero.plist"];
<wbr></wbr>
<wbr><wbr><wbr>首先,调用CCSpriteFrameCache的addSpriteFramesWithFile方法,然后把Zwoptex生成的plist文件当作参数传进去。这个方法做了以下几件事:1:寻找resource下和输入的参数名字一样但是后缀是.png的图片文件,然后把这个文件加入到共享的CCTextureCache中。就是AnimHero.png</wbr></wbr></wbr>
<wbr><wbr>2:解析plist文件,追踪所有的sprite在spritesheet中的位置,内部使用CCSpriteFrame对象追踪这些信息。</wbr></wbr>
第2步:创建一个精灵处理批节点。
CCSpriteBatchNode<wbr>*spriteSheet= [</wbr>CCSpriteBatchNode<wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#42595e; line-height:1.5">batchNodeWithFile</span>:<span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#862621; line-height:1.5">@"AnimHero.png"</span>];</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr>[<span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#772561; line-height:1.5">self</span><wbr><span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#42595e; line-height:1.5">addChild</span>:spriteSheet];</wbr></wbr></wbr></wbr></wbr></wbr>
接下来,创建CCSpriteBatchNode对象,把spritesheet当作参数传进去,spritesheet工作原理:1:创建一个CCSpriteBatchNode对象,通过传递一个包含所有sprite的spritesheet的名字作为参数,并加入到当前场景中。2:在spritesheet中创建的任何一个sprite,都应该当作CCSpriteBatchNode的一个孩子加进去,只要sprite包含在sprite中,就ok了。3:CCSpriteBatchNode可以智能遍历所有的孩子节点。
第3步:收集帧列表。
NSMutableArray<wbr>*walkAnimFrames =[<span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#653fa5; line-height:1.5">NSMutableArray</span><wbr><span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#39237e; line-height:1.5">array</span>];</wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#772561; line-height:1.5">for</span>(<span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#772561; line-height:1.5">int</span><wbr>i=<span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#2036fb; line-height:1.5">1</span>;i
<=<span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#2036fb; line-height:1.5">3</span>;i++)</wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr>{</wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr>[walkAnimFrames<wbr><span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#39237e; line-height:1.5">addObject</span>:</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>[[</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>CCSpriteFrameCache<wbr></wbr>sharedSpriteFrameCache]<wbr></wbr>spriteFrameByName:
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>[<span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#653fa5; line-height:1.5">NSString</span><wbr><span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#39237e; line-height:1.5">stringWithFormat</span>:<span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#862621; line-height:1.5">@"hero%i.png"</span>,i]]];</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr>}<wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>为了创建一系列的动画帧,我们简单地遍历我们的图片名字(它们是按照hero1.png-->hero3.png的方式命名的),然后使用共享的CCSpriteFrameCache来获得每一个动画帧。记住,它们已经在缓存里了,因为我们前面调用了addSpriteFramesWithFile方法。</wbr></wbr>
第4步:创建对象动画
CCAnimation<wbr>*walkAnim = [<span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#608086; line-height:1.5">CCAnimation</span><wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#42595e; line-height:1.5">animationWithFrames</span>:walkAnimFrames<wbr><span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#42595e; line-height:1.5">delay</span>:<span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#2036fb; line-height:1.5">0.1f</span>]</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
接下来,我们通过传入sprite帧列表来创建一个CCAnimation对象,并且指定动画播放的速度。我们使用0.1来指定每个动画帧之间的时间间隔。
第5步:创建sprite并且让他run动画action
_sprite=[CCSprite<wbr></wbr>spriteWithSpriteFrameNam<wbr>e<span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#000000; line-height:1.5">:</span><span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#862621; line-height:1.5">@"hero1.png"</span><span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#000000; line-height:1.5">];</span></wbr>
<wbr><wbr><wbr><span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#608086; line-height:1.5">_sprite</span>.<span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#608086; line-height:1.5">position</span>=<span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#6d4a30; line-height:1.5">ccp</span>(240,160);</wbr></wbr></wbr>
<wbr></wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>self.walkAction<wbr>=[</wbr>CCRepeatForever<wbr></wbr>actionWithAction:
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>[<span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#608086; line-height:1.5">CCAnimate</span><wbr><span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#42595e; line-height:1.5">actionWithAnimation</span>:walkAni<wbr><span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#42595e; line-height:1.5">restoreOriginalFrame</span>:<span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#772561; line-height:1.5">NO</span>]];</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr>[</wbr></wbr></wbr></wbr></wbr>_sprite<wbr></wbr>runAction:_walkAction];<wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr>[spriteSheet<wbr><span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#42595e; line-height:1.5">addChild</span>:<span style="font-family:宋体,Verdana,Arial,Helvetica,sans-serif; color:#608086; line-height:1.5">_sprite</span>];</wbr></wbr></wbr></wbr>
我们首先通过spriteframe来创建一个sprite,并把它放在屏幕中间。然后,生成CCAnimationAction,并赋值给场景的walkAction属性,最后让熊来运行这个action。
最后,我们把hero加个场景中--把它当作spritesheet的孩子加到spritesheet中去。注意,如果在这里我们没有把它加到spritsheet中,而是加到当前层里面的话。那么我们将得不到spritesheet为我们带来的性能提升!!
分享到:
相关推荐
【iOS-Cocos2d游戏开发】Cocos2d-iPhone动作Action-扩展动作 http://blog.csdn.net/tt5267621/article/details/7626450
【iOS-Cocos2d游戏开发】Cocos2d-iPhone动作Action-瞬时动作 http://blog.csdn.net/tt5267621/article/details/7624781
【iOS-Cocos2d游戏开发】Cocos2d-iPhone动作Action-组合动作 http://blog.csdn.net/tt5267621/article/details/7626137
【iOS-Cocos2d游戏开发】Cocos2d-iPhone动作Action-延时动作 http://blog.csdn.net/tt5267621/article/details/7625309
【iOS-Cocos2d游戏开发之二十二 】CCSpeed实现CCAnimate动画进行时设置慢动作以及设置游戏加减速进行(塔防游戏必备)! http://blog.csdn.net/xiaominghimi/article/details/7009503
【iOS-Cocos2d游戏开发】Cocos2d-iPhone动作Action-组合动作 http://blog.csdn.net/tt5267621/article/details/7626094
【iOS-Cocos2d游戏开发】触屏事件处理机制 http://blog.csdn.net/tt5267621/article/details/7631729
《iOS5 cocos2d游戏开发实战(第2版)》免费P
【iphone游戏开发】iphone-Cocos2d游戏开发之三:精灵表的详细讲解(二)小鸟眨眼睛
仿照愤怒的小鸟,缩放场景写了个DEMO ...说明:缩放不会影响精灵的运动,只简单的写了个精灵的运动,缩放地图的时候会有些黑线,这跟tileMap有关,跟缩放场景无关,如果用图片当场景不会出现这种情况!
iOS 5 cocos2d游戏开发实战(第2版)
iOS实例开发源码——iliu-ios-cocos2d-viking-cba07f8.zip
iOS游戏应用源代码——iliu-ios-cocos2d-viking-cba07f8.zip
iOS实例开发源码——iOS-Gamer-Cocos2D-86714e3.zip
iOS游戏应用源代码——iOS-Gamer-Cocos2D-86714e3.zip
Mac-OS-X-配置-Cocos2d-x-开发环境(iOS-和-Android).doc
详细讲述iOS中,利用cocos2d开发精灵
12-IOS游戏开发Cocos2d.pdf
4、【cocos2d-x IOS游戏开发-捕鱼达人4】基本游戏框架 资源引用页: 5、【cocos2d-x IOS游戏开发-捕鱼达人5】C++中函数代理与信号插槽机制 ...8、【cocos2d-x IOS游戏开发-捕鱼达人8】游戏中的配置文件 资源引用页: