我正在尝试使用SpriteKit和touchesEnded函数在Xcode Playgrounds中创建一个按钮,但以下代码并没有为我工作。 backButtonTriggerBox出现在屏幕上,但当我点击它时没有任何反应。
编辑:我更新了代码,以便所有内容都在一个类下,但它似乎仍然不起作用。
struct myVariables {
static var backButtonTriggerBox = SKSpriteNode(color: SKColor.red, size: CGSize(width: 40, height: 15))
}
class SpriteScene: SKScene {
class func initialLoad() {
let view = SKView(frame: CGRect(x: 0, y: 0, width: 550, height: 575))
let scene = SKScene(size: CGSize(width: 550, height: 575))
scene.backgroundColor = UIColor.white
scene.scaleMode = SKSceneScaleMode.aspectFit
view.presentScene(scene)
PlaygroundPage.current.liveView = view
let backButtonTriggerBox = myVariables.backButtonTriggerBox
backButtonTriggerBox.position = CGPoint(x: 275, y: 13)
myVariables.backButtonTriggerBox = backButtonTriggerBox
scene.addChild(backButtonTriggerBox)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: scene!)
let backButtonTriggerBox = myVariables.backButtonTriggerBox
if backButtonTriggerBox.contains(touchLocation) {
print("hi")
}
}
}
SpriteScene.initialLoad()
答案 0 :(得分:1)
可能是你的精灵没有添加到场景中吗?根据您的示例代码,它会添加到scene
,touchesEnded会SpriteScene
。该按钮甚至不在场景代码块