Sprite Frame Animation Cocos2D 3.0

Sprite Frame Animation Cocos2d 3.0

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

A simple sprite sheet animation in Cocos2d-JS

The problem was twofold. First there has to be a texture defined for the animation, and second, if it should be a continous animation, than the animFrames have to of type animationFrame. The working code is as follows (put in the ctor function of the helloworld example):

// Create sprite and set attributes
this.mostafa = cc.Sprite.create(res.Mostafa_single_png);
this.mostafa.attr({
x: size.width / 3,
y: size.height / 3,
scale: 0.5,
rotation: 0
});
this.addChild(this.mostafa, 0);

// Load sprite frames to frame cache, add texture node
cc.spriteFrameCache.addSpriteFrames(res.Mostafa_plist);
var mostafaTexture = cc.textureCache.addImage(res.Mostafa_png),
mostafaImages = cc.SpriteBatchNode.create(mostafaTexture);
this.addChild(mostafaImages);

var animFrames = [];
var str = "";
for (var i = 1; i < 9; i++) {
str = "mosquito_fly" + (i < 10 ? ("0" + i) : i) + ".png";
var spriteFrame = cc.spriteFrameCache.getSpriteFrame(str);
var animFrame = new cc.AnimationFrame();
animFrame.initWithSpriteFrame(spriteFrame, 1, null);
animFrames.push(animFrame);
}
var animation = cc.Animation.create(animFrames, 0.08, 100);
var animate = cc.Animate.create(animation);

this.mostafa.runAction(animate);

How to animate CCSprite in Cocos2D 3.x?

This is how it works:

    NSMutableArray *animationFrames = [NSMutableArray array];

for(int i = 1; i <= FRAMES; ++i)
{
CCSpriteFrame *spriteFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"animationFrame%d.png", i]]; //
}

//Create an animation from the set of frames you created earlier
CCAnimation *animation = [CCAnimation animationWithSpriteFrames: animationFrames delay:delay];

//Create an action with the animation that can then be assigned to a sprite
CCActionAnimate *animationAction = [CCActionAnimate actionWithAnimation:animation];

CCActionRepeatForever *repeatingAnimation = [CCActionRepeatForever actionWithAction:animationAction];
[self runAction:repeatingAnimation];

Stop Sprite Animation in cocos2d

The animation does not stop because the CCRepeatForever is the action which animates forever means continuously. Change below line:

[m_gamecoinffect runAction:[CCRepeatForever actionWithAction:coineffect]];

With

[m_gamecoinffect runAction:coineffect];


Related Topics



Leave a reply



Submit