Search code examples
iosobjective-csprite-kitskspritenodeskscene

how to make a button in Sprite kit for touchesBegan


basically for now I got TitleScene, where are present name of the my game and when tap anywhere on screen it jumps to GameScene, it looks something like this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

SKScene *scene = [GameScene sceneWithSize:self.size];
SKTransition *transition = [SKTransition pushWithDirection:SKTransitionDirectionUp duration:1.0f];
[self.view presentScene:scene transition:transition];
}

My question how can i change this method to button?


Solution

  • maybe give your sprite a name of "button" so you know which one youre touching

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        /* Called when a touch begins */
    
        for (UITouch *touch in touches) {
            CGPoint location = [touch locationInNode:self];
            SKNode *touchedNode = [self nodeAtPoint:location];
    
            if (touchedNode && [touchedNode.name isEqual:@"button"]) {
                // your code here
            }
        }
    }