如何动画CCSprite在Cocos2D 3.x? [英] How to animate CCSprite in Cocos2D 3.x?
本文介绍了如何动画CCSprite在Cocos2D 3.x?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您是否知道如何在新的 Cocos2D v3.x 中为 CCSprite
设置动画?
很多类被改变,旧的方法似乎不工作。
NSMutableArray * animFrames = [NSMutableArray array] ;
for(int i = 1; i <= 3; i ++){
CCSpriteFrame * frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@Sprite-%d.png,i ]];
[animFrames addObject:frame];
}
CCAnimation * animation = [CCAnimation animationWithName:@rundelay:0.1f frames:animFrames];
[mySprite runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]]];
任何想法?
/ p>
额外资讯
解决方案
这是它的工作原理:
NSMutableArray * animationFrames = [NSMutableArray array];
for(int i = 1; i <= FRAMES; ++ i)
{
CCSpriteFrame * spriteFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@ animationFrame%d.png,i]]; //
}
//从之前创建的框架集创建动画
CCAnimation * animation = [CCAnimation animationWithSpriteFrames:animationFrames delay:delay];
//创建一个动画,然后可以分配给sprite
CCActionAnimate * animationAction = [CCActionAnimate actionWithAnimation:animation];
CCActionRepeatForever * repeatingAnimation = [CCActionRepeatForever actionWithAction:animationAction];
[self runAction:repeatingAnimation];
do you know how to animate a CCSprite
in the new Cocos2D v3.x ?
A lot of classes are changed, and old method seems not work.
NSMutableArray *animFrames = [NSMutableArray array];
for(int i = 1; i <= 3; i++) {
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Sprite-%d.png",i]];
[animFrames addObject:frame];
}
CCAnimation *animation = [CCAnimation animationWithName:@"run" delay:0.1f frames:animFrames];
[mySprite runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]]];
any idea?
thanks.
extra info
解决方案
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];
这篇关于如何动画CCSprite在Cocos2D 3.x?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文