1
votes

In my iOS game, I hope the hero keep running until screen is touched and the hero should jump. So I write:
In .h file:

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

In .m file:

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

Then when the game starts, I call run () method:

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

Then I found CCAnimate actionWithSpriteSequence: numFrames: delay: restoreOriginalFrame: is deprecated in cocos2d v2.0. So my question is, in cocos2d v2.0, how can I implement this animation? Namely, keep my hero running? Thank you!
EDIT:
I have tried this:

-(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;
}

Then in init ():

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

and in run ():

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

But it still doesn't work. What should I do?

1
Try a larger delay (ie 0.1). Try logging the frame names and/or the frames array to verify the names are correct. Also, in case you aren't using ARC you're leaking the frames array. - LearnCocos2D
@LearnCocos2D I checked and found I'm not using ARC. What's the reason for leaking the frames array? How to correct it? Thank you! - lkkeepmoving
leak is in the first line of your getMyAnimationWithFramesName:numFrames:delay: method. You do not destroy anywhere created array frames - Morion
You alloc/init the object, instead of using [NSMutableArray array] which returns an autoreleased object. Rule is that with alloc/init you take ownership of the object. But you don't release or autorelease it, so it leaks. If you didn't know that, check the rest of your code for such instances and at the latest use ARC for the next project. - LearnCocos2D

1 Answers

2
votes

First of all you download Texture from 'http://www.codeandweb.com/texturepacker/download' and make p-List and use it below code

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

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

[self addChild:spriteSheet];

Gather the list of frames(sprite)

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

give the Action to the Sprite

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

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

Sprite RepeaetForever for manSprite

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

[manSprite runAction:first];