简体   繁体   English

从SceneKit检测SpriteKit按钮按下叠加

[英]Detect SpriteKit button press overlay from SceneKit

I have a simple HUD that I built using sprite kit that is an overlay on my 3d scene kit game. 我有一个使用精灵套件构建的简单HUD,该套件是3D场景套件游戏的叠加层。 I have a visible "Main Menu" button that displays but for the life of me I can not detect that it's being pressed. 我有一个可见的“主菜单”按钮显示,但是对我而言,我无法检测到它被按下了。

Here is how the overlay is being setup. 这是设置叠加层的方式。

    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

Now here is the overlay scene that is being created. 现在,这是正在创建的叠加场景。

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")
        }
    }
}
}

My expected output would be to see "Main Menu Touch Began" and "Main Menu Touch Ended" but I am not getting anything. 我的预期输出是看到“ Main Menu Touch Began”和“ Main Menu Touch Ended”,但我什么也没得到。 Any help would be much appreciated. 任何帮助将非常感激。

1) By having isUserInteractionEnabled set to false on your overlay, your overlay is never going to receive touch events. 1)通过在叠加层上将isUserInteractionEnabled设置为false,叠加层将永远不会接收触摸事件。

2) With you manually scaling, you open yourself up to screwing around with your math. 2)手动缩放后,您就可以轻松进行数学运算。 SpriteKit is designed to handle scaling for you, that is why scaleMode exists. SpriteKit旨在为您处理缩放,这就是为什么scaleMode存在的原因。 It makes a lot more sense to scale once at the very end up process, then to constantly scale every little thing. 在最终过程中一次扩展,然后不断地扩展每一个小事情,这意义更大。

Do the following changes, and it should work for you. 进行以下更改,它应该适合您。

Set

overlayScene.isUserInteractionEnabled = true

To allow your scene to receive touches, 为了让您的场景获得感动,

then clean up your code: 然后清理您的代码:

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")
            }
        }
    }
}

and use: 并使用:

sceneView.overlaySKScene = OverlayScene()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM