gpt4 book ai didi

ios - 从 SceneKit 检测 SpriteKit 按钮按下覆盖

转载 作者:行者123 更新时间:2023-11-28 14:25:55 24 4
gpt4 key购买 nike

我有一个简单的 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")
}
}
}
}

我的预期输出是看到“Main Menu Touch Begin”和“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()

关于ios - 从 SceneKit 检测 SpriteKit 按钮按下覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51569412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com