如何定义节点的区域以使其成为按钮
how to define the area of a node to make it a button
一如既往,我不熟悉 swift 和 SpriteKit,抱歉。
我正在创建一个改变场景的按钮。我正在使用一个简单的代码来检测触摸是否在 SKNode 区域,如果是,它会改变场景。
我的问题是 node.position 是由 CGPoint 定义的,而不是区域,所以当您触摸屏幕时,您实际上并没有触摸到节点。
有什么建议吗?
不知道怎么解决这个问题
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let touchedNode = atPoint(location)
if touchedNode.name == "B" {
let menuScene = MenuScene(size: view!.bounds.size)
view!.presentScene(menuScene)
}
}
}
已解决!我用文本初始化器而不是名称初始化器创建了节点 "B",这就是我找不到它的原因。
let b = SKLabelNode(name: "B")
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let nodeB = self.childNode(withName: "B")
let frameB = nodeB!.calculateAccumulatedFrame()
let minX = frameB.minX
let maxX = frameB.maxX
let minY = frameB.minY
let maxY = frameB.maxY
if location.x > minX && location.x < maxX {
if location.y > minY && location.y < maxY {
let gameScene = GameScene(size: view!.bounds.size)
view!.presentScene(gameScene)
}
}
}
}
一如既往,我不熟悉 swift 和 SpriteKit,抱歉。
我正在创建一个改变场景的按钮。我正在使用一个简单的代码来检测触摸是否在 SKNode 区域,如果是,它会改变场景。
我的问题是 node.position 是由 CGPoint 定义的,而不是区域,所以当您触摸屏幕时,您实际上并没有触摸到节点。
有什么建议吗?
不知道怎么解决这个问题
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let touchedNode = atPoint(location)
if touchedNode.name == "B" {
let menuScene = MenuScene(size: view!.bounds.size)
view!.presentScene(menuScene)
}
}
}
已解决!我用文本初始化器而不是名称初始化器创建了节点 "B",这就是我找不到它的原因。
let b = SKLabelNode(name: "B")
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let nodeB = self.childNode(withName: "B")
let frameB = nodeB!.calculateAccumulatedFrame()
let minX = frameB.minX
let maxX = frameB.maxX
let minY = frameB.minY
let maxY = frameB.maxY
if location.x > minX && location.x < maxX {
if location.y > minY && location.y < maxY {
let gameScene = GameScene(size: view!.bounds.size)
view!.presentScene(gameScene)
}
}
}
}