Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

ios - Sprite Frame Animation Cocos2d 3.0

I've been trying to make an animated sprite, they're are a lot of tutorials but they're all for Cocos2d 2.x. My sprite sheet is named flappbird.png and the .plist is named flappbird.plist

I have this code but every time i start the scene it just crashes, this is in my init method

// -----------------------------------------------------------------------

_player = [CCSprite spriteWithImageNamed:@"monster1.png"]; // comes from your .plist file
_player.position  = ccp(self.contentSize.width/28,self.contentSize.height/2);
_player.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, _player.contentSize} cornerRadius:0]; // 1
_player.physicsBody.collisionGroup = @"playerGroup";
_player.physicsBody.type = CCPhysicsBodyTypeStatic;
CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:@"monster1.png"];
[batchNode addChild:_player];
[self addChild:batchNode];

NSMutableArray *animFrames = [NSMutableArray array];
for(int i = 1; i < 5; i++)
{
    CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"flapbird%d.png",i]];
    [animFrames addObject:frame];
}

CCAnimation *animation = [CCAnimation animationWithSpriteFrames:animFrames delay:0.2f];
[_player runAction:[CCActionRepeatForever actionWithAction:[CCActionAnimate actionWithAnimation:animation]]];

[_physicsWorld addChild:_player];

// -----------------------------------------------------------------------
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Animate sprite with spritesheet in Cocos2d 3.0

Make sure to add #import "CCAnimation.h" at the beginning of your code

Also add the sprite sheet after the self.userInteractionEnabled = YES; in the init

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"your.plist"];

No add all this where the sprite will be

//The sprite animation
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 7; ++i)
{
     [walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"monster%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation
                         animationWithSpriteFrames:walkAnimFrames delay:0.1f]; //Speed in which the frames will go at

//Adding png to sprite
monstertest = [CCSprite spriteWithImageNamed:@"monster1.png"];

//Positioning the sprite
monstertest.position  = ccp(self.contentSize.width/2,self.contentSize.height/2);

//Repeating the sprite animation
CCActionAnimate *animationAction = [CCActionAnimate actionWithAnimation:walkAnim];
CCActionRepeatForever *repeatingAnimation = [CCActionRepeatForever actionWithAction:animationAction];

//Animation continuously repeating
[monstertest runAction:repeatingAnimation];

//Adding the Sprite to the Scene
[self addChild:monstertest];

Hope this helps somebody :D Cheers


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...