【发布时间】:2019-01-05 06:06:49
【问题描述】:
我有一个简单的 HUD,它是使用 sprite kit 构建的,它是我的 3d 场景套件游戏的叠加层。我有一个可见的“主菜单”按钮,但在我的一生中,我无法检测到它被按下了。
下面是叠加层的设置方式。
sceneView = self.view as! GameSCNView
scene = SCNScene(named: "art.scnassets/MainScene.scn")
sceneView.scene = scene
sceneView.overlaySKScene = OverlayScene(size: sceneView.bounds.size)
overlayScene = sceneView.overlaySKScene as! OverlayScene
overlayScene.isUserInteractionEnabled = false
现在这里是正在创建的叠加场景。
class OverlayScene : SKScene {
var timeLabel:SKLabelNode!
var mainMenu:SKSpriteNode!
override init(size: CGSize) {
super.init(size: size)
//setup the overlay scene
self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
//automatically resize to fill the viewport
let widthScale = size.width / 1024
let heightScale = size.height / 768
self.scaleMode = .resizeFill
// Initialize the Score
timeLabel = SKLabelNode(text: "Time: 0")
timeLabel.position = CGPoint(x: -size.width * 0.35, y: size.height*0.45)
timeLabel.fontName = "AmericanTypewriter-Bold"
timeLabel.fontSize = 36 * widthScale
timeLabel.fontColor = UIColor.white
addChild(timeLabel)
mainMenu = SKSpriteNode(imageNamed: "MainMenu_ButtonHighlighted-IpadMini.png")
mainMenu.anchorPoint = CGPoint(x: 0, y: 0)
mainMenu.xScale = widthScale
mainMenu.yScale = heightScale
mainMenu.position = CGPoint(x: -size.width * 0.35, y: size.height*0.45)
mainMenu.isUserInteractionEnabled = false
mainMenu.zPosition = 0
addChild(mainMenu)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.location(in: self)
if mainMenu.contains(location) {
print("Main Menu Touch Began")
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.location(in: self)
if mainMenu.contains(location) {
print("Main Menu Touch Ended")
}
}
}
}
我的预期输出是看到“主菜单触摸开始”和“主菜单触摸结束”,但我什么也没得到。任何帮助将不胜感激。
【问题讨论】:
-
如果您将
isUserInteractionEnabled的mainMenu和OverlayScene都更改为true,会发生什么? -
什么都没有,而且我还发现将其设为 false 会使按钮触发,而 true 则不会。在我看来有点违反直觉。感谢您的评论!
-
是的,这家伙似乎也想通了:stackoverflow.com/questions/33850541/…
-
你能看到 touchesBegan 和 touchEnded 是否被调用,只是没有进入 if 语句吗?
-
通过将打印放在 for 语句和 if 语句之外,我没有得到任何输出。因此,在按下按钮或 SKScene 的任何其他部分时不会调用它们
标签: ios swift sprite-kit scenekit