0
我正在用Swift制作游戏。 我需要添加节点,我按下屏幕,我知道这不是来自其他星球,但我不能这样做。添加触摸开始的节点
我正在用Swift制作游戏。 我需要添加节点,我按下屏幕,我知道这不是来自其他星球,但我不能这样做。添加触摸开始的节点
高度简化的例子看起来是这样的:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let location = touch.location(in: self)
let node = SKNode()
node.position = location
addChild(node)
}
这里是你可以通过点击添加删除标签:
class ViewController: UIViewController{
private var labels = [UILabel]()
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {
return
}
for label in labels {
if label.frame.contains(touch.location(in: view)) {
label.removeFromSuperview()
return
}
}
let label = UILabel()
view.addSubview(label)
labels.append(label)
label.text = "touch"
label.sizeToFit()
label.center = touch.location(in: view)
}
}
其实它的工作原理,但我只能添加一个节点,当我按两次我得到一个错误。控制台说:“终止与类型NSException的未捕获的异常” –
忘记它,我错过了一行代码 –
酷,一个异常将是一个非常奇怪的行为与此代码:) –