Search code examples
objective-csprite-kitskspritenodetouchesbegan

Changing SKSpriteNode property via touchesBegan


I was wondering if there was a more efficient way to changing the property of a SKSpriteNode in touchesBegan than my current method. The following is my method:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    if ([node.name isEqualToString:@"Start"]){
        for (SKSpriteNode *node2 in [self children]){
            if ([node2.name isEqualToString:@"Start"]){
                node2.texture = [SKTexture textureWithImageNamed:@"button_beige_pressed"];
                }
            }
    }
        ...
}

Solution

  • I found a solution:

    - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        CGPoint location = [touch locationInNode:self];
        SKSpriteNode *node = (SKSpriteNode *)[self nodeAtPoint:location];
    
        if ([node.name isEqualToString:@"Start"]){
            node.texture = startPressed;
        }
    
    }
    

    I was using a Node when I should have been using a spritenode.