![](/img/trans.png)
[英]Animations of SpriteKit overlay on SceneKit stops when SceneKit animations stop
[英]Detect SpriteKit button press overlay from SceneKit
我有一个使用精灵套件构建的简单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")
}
}
}
}
我的预期输出是看到“ Main Menu Touch Began”和“ 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.