嗨,我正在为iphone制作一个游戏,当精灵移动时,我希望它在3个图像之间更改图像,这样它看起来像是在运行。我现在正在使用cocos2d,而且我对cocos2d还很陌生。我知道如何用可可做这个,但这对可可不起作用。
所以我的问题是,当我把手指放在屏幕上的某个位置时,我如何在3个图像之间更改精灵图像,并且我想循环它?
提前谢谢。
对于刚接触cocos2d的人来说,这是一个非常复杂的问题。
我会先制作无限动画。让它发挥作用,然后暂停、恢复和翻转动画。
可以使用与添加精灵相同的方法设置动画。
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]]];
如果你不熟悉精灵表,有很多免费资源可以创建精灵表和plist(TexturePacker有一个很好的界面)
如果你很难让它发挥作用,Ray Wenderlich有很好的教程。如果你做到了这一点,这里有一些暂停、恢复和翻转动画的指针
用于暂停或恢复
[mySprite pauseSchedulerAndActions];
[mySprite resumeSchedulerAndActions];
每当触摸方向切换水平方向时翻转动画
mySprite.flipX = YES;
mySprite.flipX = NO;
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"animations/grossini.plist"];
CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"grossini_dance_01.png"];
sprite.position = ccp( 100, 100);
CCSpriteSheet *spritesheet = [CCSpriteSheet spriteSheetWithFile:@"animations/grossini.png"];
[spritesheet addChild:sprite];
[self addChild:spritesheet];
NSMutableArray *animFrames = [NSMutableArray array];
for(int i = 1; i < 15; i++) {
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"grossini_dance_%02d.png",i]];
[animFrames addObject:frame];
}
CCAnimation *animation = [CCAnimation animationWithName:@"dance" frames:animFrames];
// 14 frames * 0.2sec = 2,8 seconds
[sprite runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithDuration:2.8f animation:animation restoreOriginalFrame:NO] ]];