02fentym
02fentym

Reputation: 1772

How do you determine which SKSpriteNode was clicked?

I'm trying to use an SKSpriteNode as a transition from one scene to the next. How can I do this?

[EDIT] Please note that this is for OSX not iOS. The touchesBegan method for iOS does not seem to work in OSX.

Upvotes: 1

Views: 1165

Answers (3)

Indra
Indra

Reputation: 548

Try this, its work for me

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

     for (UITouch *aTouch in touches) {
         if (aTouch.tapCount >= 1) {
            // The view responds to the tap
           // do something here...
     }
}

Upvotes: 0

02fentym
02fentym

Reputation: 1772

Okay, here's the solution for OSX.

You must first initialize the scene (the self object) so that it monitors for clicks.

self.userInteractionEnabled = YES; //do this somewhere in initialization

In the mouseDown event handler, check to see if a node (specifically an SKSpriteNode in this case) has been touched.

-(void)mouseDown:(NSEvent *)theEvent {
    CGPoint location = [theEvent locationInNode:self]; //get location of touch
    SKSpriteNode *spriteTouched = (SKSpriteNode*)[self nodeAtPoint:location]; //get a node if touched at that location
    //DO SOMETHING WITH THE NODE
    ...
}

Upvotes: 5

mihnea2kx
mihnea2kx

Reputation: 110

First you need to set a name for the node . node.name = @"node's name"; Then in the touchesBegan method add this :

for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];
        if([[self nodeAtPoint:location].name isEqualToString:@"node's name"]){
            //present scene code here
        }
    }

Upvotes: 2

Related Questions