简体   繁体   中英

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. 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. 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.

2) With you manually scaling, you open yourself up to screwing around with your math. SpriteKit is designed to handle scaling for you, that is why scaleMode exists. 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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