通过touchesBegan更改SKSpriteNode属性

时间:2015-11-19 22:25:49

标签: objective-c sprite-kit skspritenode touchesbegan

我想知道是否有更有效的方法来更改SKSpriteNodetouchesBegan的属性,而不是我当前的方法。以下是我的方法:

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

2 个答案:

答案 0 :(得分:0)

您当前的逻辑表明您有多个名为“start”的节点。如果确实如此,我建议创建一个数组并将所有'start'节点存储在所述数组中。

但是,如果您只有一个名为“start”的节点,则不需要循环,因为您已经具有对具有触摸功能的节点的引用。

答案 1 :(得分:0)

我找到了解决方案:

- (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;
    }

}

当我应该使用spritenode时,我正在使用Node。