按下SceneKit中的"检测精灵套件"按钮覆盖



我有一个简单的HUD,我使用sprite工具包构建,它是我的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")
}
}
}
}

我的预期输出是看到"主菜单触摸开始"one_answers"主菜单接触结束",但我什么都没得到。任何帮助都将不胜感激。

1(通过在覆盖上将isUserInteractionEnabled设置为false,覆盖将永远不会接收到触摸事件。

2( 通过手动缩放,你可以让自己在数学上游刃有余。SpriteKit旨在为您处理缩放,这就是scaleMode存在的原因。在最后的过程中扩大一次规模,然后不断扩大每一件小事,这更有意义。

做以下更改,它应该对您有效。

设置

overlayScene.isUserInteractionEnabled = true

为了允许您的场景接收触摸,

然后清理你的代码:

class OverlayScene : SKScene {
var timeLabel:SKLabelNode!
var mainMenu:SKSpriteNode!
override init(size: CGSize) {
super.init(size:size)
}
convenience override init() {
self.init(size: CGSize(width:1024,height:768))
//setup the overlay scene
self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
//automatically resize to fill the viewport

self.scaleMode = .fill
// 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
timeLabel.fontColor = UIColor.white
addChild(timeLabel)
mainMenu = SKSpriteNode(imageNamed: "MainMenu_ButtonHighlighted-IpadMini.png")
mainMenu.anchorPoint = CGPoint(x: 0, y: 0)
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 == self.atPoint(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 == self.atPoint(location) {
print("Main Menu Touch Ended")
}
}
}
}

和用途:

sceneView.overlaySKScene = OverlayScene()

最新更新