最近学cocos2d,看一般都用Zwoptex,但是这个软件要钱,所以用了一个在线版本的http://zwoptexapp.com/flashversion/基本功能也很好,需要注意的是,在最后导出png图片和plist文件时,一定要取相同的名字(切记啊切记),下面是素材
//=================================================================================
{
<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
<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>
第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为我们带来的性能提升!!