通过 touchesBegan 更改 SKSpriteNode 属性
Changing SKSpriteNode property via touchesBegan
我想知道是否有比我目前的方法更有效的方法来更改 touchesBegan
中 SKSpriteNode
的 属性。以下是我的方法:
- (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"];
}
}
}
...
}
您当前的逻辑表明您有多个名为 'start' 的节点。如果确实如此,我建议创建一个数组并将所有 'start' 节点存储在所述数组中。
但是,如果您只有一个名为 'start' 的节点,则不需要循环,因为您已经有了对具有触摸功能的节点的引用。
我找到了解决方案:
- (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。
我想知道是否有比我目前的方法更有效的方法来更改 touchesBegan
中 SKSpriteNode
的 属性。以下是我的方法:
- (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"]; } } } ... }
您当前的逻辑表明您有多个名为 'start' 的节点。如果确实如此,我建议创建一个数组并将所有 'start' 节点存储在所述数组中。
但是,如果您只有一个名为 'start' 的节点,则不需要循环,因为您已经有了对具有触摸功能的节点的引用。
我找到了解决方案:
- (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。