0
votes

After making adjustments to my code as suggested, my animation now works. Now, I'd like to ask how to reverse the movements of the sprite. I am trying to make the sprite move as if the wind is blowing in it. Here's my code now:

-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    self.isTouchEnabled = YES;

    CCSprite *black = [CCSprite spriteWithFile:@"b.png"];
    black.position = ccp(100, 160);
    black.scaleX = 100 / black.contentSize.width;
    black.anchorPoint = ccp(0.03, 0);

    CCSpriteFrameCache *frame = [CCSpriteFrameCache sharedSpriteFrameCache];
    [frame addSpriteFramesWithFile:@"bLongAnimation.plist"];
    CCSpriteBatchNode *bHair = [CCSpriteBatchNode batchNodeWithFile:@"bLongAnimation.png"];
    [self addChild:bHair];

    [self addChild:black z:1 tag:1];

    //Animation
    NSMutableArray *animateBlackHair = [NSMutableArray arrayWithCapacity:10];
    for (int i = 1; i < 10; i++)
    {
        NSString *animBlackHair = [NSString stringWithFormat:@"b%i.png", i];
        CCSpriteFrame *blackFrame = [frame spriteFrameByName:animBlackHair];
        [animateBlackHair addObject:blackFrame];

        //I added this code block thinking it might work
        for(i = 10; i > 1; i--)
        {
            NSString *revAnimation = [NSString stringWithFormat:@"bRightLong%i.png", i];
            CCSpriteFrame *revFrame = [frame spriteFrameByName:revAnimation];
            [animateBlackHair1 addObject:revFrame];
        }
    }
    CCAnimation *blowHair = [CCAnimation animationWithSpriteFrames:animateBlackHair delay:0.1];
    CCAction *blowingHair = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:blowHair]];
    [black runAction:blowingHair];        
}
return self;}

My guess didn't work so I was wondering how I can reverse the movements as soon as it finishes the first one?

UPDATE: Never mind, I figured it out. I just moved the for loop for reversing the action outside of the other loop. Thank you for the help.

1
I am not sure if this is the problem but having the delay be 0 might make your animation run too fast so that you can't see it, try setting it to 0.1 or 0.4. - bluestunt
Strange, that was the problem indeed. I tried to set it to 0 because I had a timer that only runs for 5 seconds on that scene. Thank you. - user1597438

1 Answers

2
votes

Try setting the delay on the animation to something other than 0. Try 0.1 or 0.4 or something. If you set it to zero I don't think the animation runs, and if it does, it runs too fast to be visible.