user5177178
user5177178

Reputation:

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"];
                }
            }
    }
        ...
}

Upvotes: 0

Views: 224

Answers (2)

user5177178
user5177178

Reputation:

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.

Upvotes: 0

sangony
sangony

Reputation: 11696

Your current logic suggest that you have multiple nodes with the name 'start'. If this is actually the case I suggest creating an array and storing all 'start' nodes in said array.

If however you only have a single node with the name 'start', then no loop is needed as you already have a reference to the node with the touch function.

Upvotes: 0

Related Questions