【问题标题】:Changing SKSpriteNode property via touchesBegan通过 touchesBegan 更改 SKSpriteNode 属性
【发布时间】:2016-02-22 06:44:59
【问题描述】:

我想知道是否有比我当前的方法更有效的方法来更改 touchesBeganSKSpriteNode 的属性。以下是我的方法:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [触摸任何对象]; 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"]; } } } ... }

【问题讨论】:

  • 为什么需要有 if ([node.name isEqualToString:@"Start"]){ 然后 if ([node2.name isEqualToString:@"Start"]){ ?
  • 另外说明,您必须尽可能多地重复使用纹理。方法 textureWithImageNamed 加载纹理,这是一个昂贵的操作。也许您可以预先加载纹理并稍后应用它。
  • 您能否在for 循环之外创建SKTexture(然后将node2.texture 分配给该纹理?那会是更好的方法吗?
  • 当我在第一个for 循环中尝试时,我无法访问/更改名为“Start”的节点的属性;那是因为它是一个精灵节点。这就是为什么我必须创建另一个访问精灵节点的for 循环。我做了一个编辑:它应该是SKNode *node = [self nodeAtPoint: location];

标签: objective-c sprite-kit skspritenode touchesbegan


【解决方案1】:

我找到了解决办法:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多