从SceneKit检测SpriteKit按钮按下叠加

时间:2018-07-28 08:11:46

标签: ios swift sprite-kit scenekit

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

我的预期输出是看到“主菜单触摸开始”和“主菜单触摸已结束”,但是我什么也没得到。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

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()