cocos2d精灵永远重复动画

时间:2013-10-13 19:36:33

标签: ios animation cocos2d-iphone sprite repeat

在我的iOS游戏中,我希望英雄继续运行直到触摸屏幕并且英雄应该跳跃。所以我写道:
在.h文件中:

@interface Hero : CCSprite {
    CCSprite *_hero;
    id _keepRunning;
}
@property(nonatomic,retain) id keepRunning;

在.m文件中:

@synthesize keepRunning = _keepRunning;
-(id) init {
    _keepRunning = [CCRepeatForever actionWithAction:[CCAnimate actionWithSpriteSequence:@"heroRun%04d.png"
                                                                               numFrames:30
                                                                                   delay:0.02f
                                                                    restoreOriginalFrame:NO]];        
}

然后当游戏开始时,我调用run()方法:

-(void) run {
    [_hero stopAllActions];
    [_hero runAction:_keepRunning];
    _heroState = RUNNING;
}

然后我发现在cocos2d v2.0中不推荐使用CCAnimate actionWithSpriteSequence: numFrames: delay: restoreOriginalFrame:。所以我的问题是,在cocos2d v2.0中,我该如何实现这个动画呢?也就是说,让我的英雄继续奔跑?谢谢!
编辑:
我试过这个:

-(CCAnimation*) getMyAnimationWithFramesName:(NSString*)nameFormat numFrames:(int)numFrames delay:(float)delay {
    NSMutableArray *frames = [[NSMutableArray alloc] init];
    for (int i = 1; i <= numFrames; i++) {
        NSString *frameName = [NSString stringWithFormat:nameFormat,i];
        [frames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:frameName]];
    }
    CCAnimation *ani = [CCAnimation animationWithSpriteFrames:frames delay:delay];
    return ani;
}

然后在init():

_keepRunning = [self getMyAnimationWithFramesName:@"heroRun%04d.png" numFrames:30 delay:0.02f];

并在run()中:

[_hero runAction:[CCAnimate actionWithAnimation:_keepRunning]];

但它仍然无效。我该怎么办?

1 个答案:

答案 0 :(得分:2)

首先,您从&#39; http://www.codeandweb.com/texturepacker/download&#39;下载纹理。 并制作p-List并在代码

下使用它
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"AnimatedMan.plist"];

CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"AnimatedMan.png"];

[self addChild:spriteSheet];

收集帧列表(精灵)

NSMutableArray *walkAnimFrames = [NSMutableArray array];
for (int i=1; i<=6; i++) {
    [walkAnimFrames addObject:
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
      [NSString stringWithFormat:@"step0%d.png",i]]];
}

将动作交给精灵

CCAnimation *walkAnim = [CCAnimation animationWithSpriteFrames:walkAnimFrames delay:0.1f];

        manSprite=[CCSprite spriteWithSpriteFrameName:@"step01.png"];
        manSprite.position=ccp(winsize.width/2, winsize.height/2-40);

manSprite的Sprite RepeaetForever

id first=[CCSequence actions:[CCRepeatForever actionWithAction:
                                      [CCAnimate actionWithAnimation:walkAnim]],nil];

[manSprite runAction:first];