开发者

Animation in Cocos2d.

开发者 https://www.devze.com 2023-01-02 17:10 出处:网络
I am trying to make a sprite animate in cocos2D.I believe I have the animation set up, but how do I draw the animating sprite onto the screen?Here is what I have:

I am trying to make a sprite animate in cocos2D. I believe I have the animation set up, but how do I draw the animating sprite onto the screen? Here is what I have:

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 spriteWithSpriteFra开发者_如何转开发me:anim];
character.position = ccp(160, 240);
[self addChild:character];

Thanks in advance, John


Maybe it was just a cut-and-paste error, but it looks like you're telling the sprite to repeat the animation BEFORE you create it, so the character sprite you are adding to the node never gets the CCAnimate action sent to it.


You're not adding spriteFrames as the addFrame method requires.

with this line:

[character do:repeating];

maybe you're looking for [character runAction:repeating];

character = [CCSprite spriteWithSpriteFrame:anim];

Here, anim is not a spriteFrame, it's a CCanimation.

basically, you have a few problems.

you could try something like this using zwoptex to create your .plist file:

CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache]; 

[cache addSpriteFramesWithFile:@"runImages.plist"]; 

CCSprite *startingImage = [CCSprite spriteWithSpriteFrameName:@"run1.png"];

[self addChild:startingImage];

//create your sprite frames

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"]];

//run the animation

CCAnimation *animation = [CCAnimation animationWithName:@"char_walking" delay:1/12.0 frames:animFrames];
id anim = [CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO];    
[startingImage runAction:anim];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号