0

I'm currently trying to highlight two SKSpriteNodes and when both are highlighted, have them fade out from the screen. My code currently looks something like this:

// BOOL variables to indicate whether sprite is highlighted    
// Declared after the @implementation
BOOL sprite1Touch = NO;
BOOL sprite2Touch = NO;

Then the touch method:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    /* Called when a touch begins */
    for(UITouch *touch in touches)
    {
        UITouch *touch = [touches anyObject];
        CGPoint location = [touch locationInNode:self];
        SKNode *Sprite1 = [self nodeAtPoint:location];
        SKNode *Sprite2 = [self nodeAtPoint:location];

        // Resize action to highlight a sprite has been touched.
        SKAction *highlightedSprite = [SKAction scaleTo:0.55 duration:0.2];

        // Action to unhighlight by setting sprite size back to normal.
        SKAction *unhighlightedSprite = [SKAction scaleTo:0.5 duration:0.2];

        // Action to fade out sprites 
        SKAction *fadeOut = [SKAction fadeOutWithDuration:0.2];

        // Action to remove from parent
        SKAction *remove = [SKAction removeFromParent];

        if ([Sprite1.name isEqualToString:@"Sprite1"])
        {

            if (sprite1Touch == NO)
            {
                // if unhighlighted sprite is touched, highlight.
                [Sprite1 runAction:highlightedSprite];
                sprite1Touch = YES;
            }
            else if (sprite1Touch == YES)
            {
                // if highlighted sprite is touched, unhighlight.
                [Sprite1 runAction:unhighlightedSprite];
                sprite1Touch = NO;
            }
        }

        else if ([Sprite2.name isEqualToString:@"Sprite2"])
        {
            if (sprite2Touch == NO)
            {
                // if unhighlighted sprite is touched, highlight.
                [Sprite2 runAction:highlightedSprite];
                sprite2Touch = YES;
            }
            else if (sprite2Touch == YES)
            {
                // if highlighted sprite is touched, unhighlight.
               [Sprite2 runAction:unhighlightedSprite];
                sprite2Touch = NO;
            }          
        }

        // if both sprites are highlighted, fade out both of them and remove from parent
        if (sprite1Touch == YES && sprite2Touch == YES)
        {
            [Sprite1 runAction:fadeOut];
            [Sprite2 runAction:fadeOut];
            [Sprite1 runAction:remove];
            [Sprite2 runAction:remove];
        }
    }
}

When I run this in the simulator, highlighting/unhighlighting one sprite works fine. However, When both are highlighted (therefore meaning both should fade out) only the sprite that was last touched actually fades out. I have to unhighlight and then highlight the first touched sprite again for that to fade out also.

Does anyone know how I can fix this to have both sprites fade out at the same time?

  • It looks like you're setting the two "Touch" booleans to `NO` every time. How are you storing that information between touches? – jscs Mar 02 '16 at 18:51
  • Hmm. From how I look at it, when I touch them to highlight they're set to `YES`, could you explain what you mean? I'm only storing the information in those two boolean "Touch" variables, I'm not sure what you mean there. Apologies if I'm being vague, this is a little new to me! :) @JoshCaswell – Andrew Smith Mar 02 '16 at 18:58
  • In your real code, are those variables declared and used exactly as you've written them here? Or are they ivars or something else that will survive after this method runs? – jscs Mar 02 '16 at 19:00
  • @JoshCaswell apart from the variable names, which are slightly different, they are the same. The only differences are, I also set up an action to remove the sprites from the parent after fading out, as well as an action to change the sprite transparency when it is highlighted – Andrew Smith Mar 02 '16 at 19:11
  • @JoshCaswell Actually no I'm wrong! The BOOLs are declared at the start of the .m file – Andrew Smith Mar 02 '16 at 19:12
  • Please edit the code in your post to [exactly what's necessary to demonstrate the problem you have](http://stackoverflow.com/help/mcve). – jscs Mar 02 '16 at 19:14

0 Answers0