簡體   English   中英

從SceneKit檢測SpriteKit按鈕按下疊加

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM