从SceneKit中检测SpriteKit按钮按叠加
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从SceneKit中检测SpriteKit按钮按叠加相关的知识,希望对你有一定的参考价值。
我有一个简单的HUD,我使用精灵套件构建,这是我的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")
}
}
}
}
我的预期输出是看“主菜单触摸开始”和“主菜单触摸结束”但我没有得到任何东西。任何帮助将非常感激。
答案
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()
以上是关于从SceneKit中检测SpriteKit按钮按叠加的主要内容,如果未能解决你的问题,请参考以下文章
SpriteKit/Scenekit TouchesBegan 或 GestureRecognizer
SpriteKit 和 SceneKit – 如何完全暂停游戏?