Search code examples
iosobjective-csprite-kitskactionsknode

SKAction not working properly


I have a sequence of SKActions that I would like to play but they do not play properly. The eye only closes and never opens again. I have no clue why this is occurring but maybe you do! Help is appreciated, thank you.

-(void)blink {

    SKAction *delay = [SKAction waitForDuration:3];
    SKAction *blinkEye = [SKAction resizeToHeight:1 duration:.2];
    SKAction *delay2 = [SKAction waitForDuration:.5];
    SKAction *openEye = [SKAction resizeToHeight:3 duration:.2];

    SKAction *group1 = [SKAction group:@[delay,blinkEye]];
    SKAction *group2 = [SKAction group:@[delay2,openEye]];

    SKAction *all = [SKAction sequence:@[group1,group2]];

    SKAction *repeat = [SKAction repeatActionForever:all];

    [self runAction:repeat];

}

In MyScene.m

-(void)update:(CFTimeInterval)currentTime {
 /* Called before each frame is rendered */
    Player *player = (Player *)[self childNodeWithName:@"player"];
    Player *lefteye = (Player *)[player childNodeWithName:@"leye"];
    Player *righteye = (Player *)[player childNodeWithName:@"reye"];
    [lefteye blink];
    [righteye blink];

}

Solution

  • There's no need for grouping, it's causing the problem.

    SKAction *delay = [SKAction waitForDuration:3];
    SKAction *blinkEye = [SKAction resizeToHeight:1 duration:.2];
    SKAction *delay2 = [SKAction waitForDuration:.5];
    SKAction *openEye = [SKAction resizeToHeight:3 duration:.2];
    
    SKAction *all = [SKAction sequence:@[delay,blinkEye, delay2,openEye]]];
    

    And letting blink the eye on every frame update can't work. This function is called up to 60 times a second.

    Instead do it like this on creation of the eye object:

    SKAction *delay = [SKAction waitForDuration:3];
    SKAction *blinkEye = [SKAction resizeToHeight:1 duration:.2];
    SKAction *delay2 = [SKAction waitForDuration:.5];
    SKAction *openEye = [SKAction resizeToHeight:3 duration:.2];
    
    SKAction *all = [SKAction sequence:@[delay,blinkEye, delay2,openEye]]];
    
    //create the lefteye object
    lefteye = [[Eye alloc] init...]; 
    //create the righteye object
    righteye = [[Eye alloc] init...];
    [lefteye repeatActionForever:all];