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?