Search code examples
swiftsprite-kittouch-event

Splitting touchesMoved across a single SKSpriteNode


Using SpriteKit, I have created a SKSpriteNode called lightSaber, and using the touchesMoved method, I am able to either rotate the SKSpriteNode or translate it left or right. Is there a way to make it so it does one or the either depending on where I grab the SKSpriteNode? (e.g. the blade or the handle)


Solution

  • You could get the position of the touchEvent via

    CGPoint location = [touch locationInNode:self];
    

    This will give you the capability to add custom logic to where you touch the screen. For example:

    if(location.x > 300) {
       //Do this
    } else {
       //Do that
    }
    

    You could also make the sword in two pieces, i.e two nodes and name each node and listen to a touch event on both nodes.

    Get the node within touchesBegan by

    SKNode *node = [self nodeAtPoint:location];
    if([node.name isEqualToString@"Handle"]) {
    
    } else if ([node.name isEqualToString@"Blade"]) {
    
    }