【问题标题】:Node that was "touched" wasn't actually touched被“触动”的节点实际上并没有被触动
【发布时间】:2014-08-26 23:31:59
【问题描述】:

我尝试使用 spriteKit 制作一个应用程序,其中当用户触摸 sprite 节点时,会有一条 NSLog 消息表明该节点已被触摸。 我试图通过将触摸节点的名称与我检查是否被触摸的节点名称进行比较来做到这一点。问题是系统识别出节点被触摸了,尽管它没有被触摸。我认为是节点区域有问题,系统认为某个节点在某处,而绝对不是。

-(void)selectNodeForTouch:(CGPoint)touchLocation
{
    if([[touchedNode name] isEqualToString:@"menuPlayButton"]){

        NSLog(@"Pressed");
        }
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    for (UITouch *touch in touches) {
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:touch.view];
        CGPoint positionInScene = [touch locationInNode:self];

        [self selectNodeForTouch:positionInScene];


    }
}

任何想法可能是什么问题? 在此先感谢

【问题讨论】:

  • 该代码没有根据触摸位置选择节点,并且您没有包含分配touchNode的代码。

标签: ios sprite-kit skspritenode touch-event


【解决方案1】:

您的selectNodeForTouch: 方法完全错误。它使用touchedNode 变量,该变量未在您发布的代码中的任何地方定义。此方法应如下所示:

- (void)selectNodeForTouch:(CGPoint)touchLocation
{
    NSArray *nodes = [self nodesAtPoint:touchLocation];
    for (SKNode *node in nodes) {
        if([node.name isEqualToString:@"menuPlayButton"]) {
            NSLog(@"Pressed");
        }
    }
}

【讨论】:

  • 还不工作..尽管如此,当我按下屏幕上的某个地方时,有时它会打印“按下”,尽管我没有按下节点......还有其他建议吗?
  • 可能是menuPlayButton很大很大?
  • 是的,它太大了,你知道如何在不破坏图像实际尺寸的情况下让它变小吗?谢谢
  • menuSettingsButton = [SKSpriteNode spriteNodeWithImageNamed:@"menuSettingsButton"]; menuSettingsButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)-100); menuSettingsButton.name = @"settingsButton"; [self addChild:menuSettingsButton];
  • *menuSettingsButton 在 .h 文件中被声明。
猜你喜欢
  • 2018-04-01
  • 1970-01-01
  • 2021-07-22
  • 1970-01-01
  • 2021-05-16
  • 1970-01-01
  • 1970-01-01
  • 2021-11-03
  • 1970-01-01
相关资源
最近更新 更多