Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode 在场景中检测何时触摸按钮?_Xcode_Swift_Sprite Kit_Skscene - Fatal编程技术网

Xcode 在场景中检测何时触摸按钮?

Xcode 在场景中检测何时触摸按钮?,xcode,swift,sprite-kit,skscene,Xcode,Swift,Sprite Kit,Skscene,我创建了一个故事板并添加了一个带有两个按钮的场景。我不知道如何知道何时在GameSecene.swift类中按下按钮 如何做到这一点 你可以用触摸开始 下面是您的示例代码: override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { for touch: AnyObject in touches{ let location = touch.locationInNode(self)

我创建了一个故事板并添加了一个带有两个按钮的场景。我不知道如何知道何时在GameSecene.swift类中按下按钮

如何做到这一点


你可以用
触摸开始

下面是您的示例代码:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

    for touch: AnyObject in touches{
        let location = touch.locationInNode(self)
        if self.nodeAtPoint(location) == self.playButton{

              //your code
        }
    }
}

你可以用
触摸开始

下面是您的示例代码:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

    for touch: AnyObject in touches{
        let location = touch.locationInNode(self)
        if self.nodeAtPoint(location) == self.playButton{

              //your code
        }
    }
}

您应该以编程方式将
ui按钮添加到
SKView
(例如,在
didMoveToView
中)中,而不是在IB中。然后可以使用
按钮设置按钮的目标。addTarget:action:forControlEvents:
。请记住调用
按钮。
中的removeFromSuperview()
将从视图中移动
,否则您将在下一个场景中看到按钮。

您应该以编程方式将
UIButton
添加到
sk场景的
SKView
(例如在
didMoveToView
中),而不是在IB中。然后可以使用
按钮设置按钮的目标。addTarget:action:forControlEvents:
。请记住调用
按钮。
中的removeFromSuperview()
将从视图中移动
,否则您将在下一个场景中看到按钮。

您似乎在混合UIKit和SpriteKit。我个人建议不要将UIButtons与Sprite工具包结合使用。这样做是否有具体原因

有两种方法可以在精灵工具包场景中实现按钮行为:

  • 让场景对象处理触摸
  • 让按钮自己处理触摸
  • Dharmesh的回答使用方法(1),在方法(1)中,他实现了
    -touchsbegind
    方法。 在我当前的项目中,我使用SKNode子类作为按钮(2)。我不熟悉Swift语法,所以我从我的项目中发布了Objective-C代码。不过,方法调用是类似的,应该有助于说明这一点

    如果希望SKNode接收触摸,请将
    userInteractionEnabled
    设置为
    YES
    否则,将最接近的祖先设置为
    userInteractionEnabled=YES
    (通常是包含SKNode的场景)将收到一条
    -touchesBegined
    /
    -touchesMoved
    /
    -touchesEnded
    消息

    @interface VTObject : SKNode
    
    @end
    
    ...
    
    @implementation VTObject
    
    - (instancetype)init {     
      if (self = [super init]) {
        self.userInteractionEnabled = YES;
      }
    
      return self;
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {    
      NSLog(@"button touched!");
    }
    
    @end
    

    你似乎在这里混合了UIKit和SpriteKit。我个人建议不要将UIButtons与Sprite工具包结合使用。这样做是否有具体原因

    有两种方法可以在精灵工具包场景中实现按钮行为:

  • 让场景对象处理触摸
  • 让按钮自己处理触摸
  • Dharmesh的回答使用方法(1),在方法(1)中,他实现了
    -touchsbegind
    方法。 在我当前的项目中,我使用SKNode子类作为按钮(2)。我不熟悉Swift语法,所以我从我的项目中发布了Objective-C代码。不过,方法调用是类似的,应该有助于说明这一点

    如果希望SKNode接收触摸,请将
    userInteractionEnabled
    设置为
    YES
    否则,将最接近的祖先设置为
    userInteractionEnabled=YES
    (通常是包含SKNode的场景)将收到一条
    -touchesBegined
    /
    -touchesMoved
    /
    -touchesEnded
    消息

    @interface VTObject : SKNode
    
    @end
    
    ...
    
    @implementation VTObject
    
    - (instancetype)init {     
      if (self = [super init]) {
        self.userInteractionEnabled = YES;
      }
    
      return self;
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {    
      NSLog(@"button touched!");
    }
    
    @end
    

    您可以使用类似的组件。它允许您对各种触摸事件执行操作。这些是实际的UIButton还是您正在使用的节点作为按钮您可以使用类似的组件。它允许您对各种触摸事件执行操作。这些按钮是实际的UIButton还是您正在使用的节点作为按钮