簡體   English   中英

如何使用Sprite Kit和Swift創建按鈕?

[英]How do create a button using Sprite Kit and Swift?

嗨,我目前正在為iOS開發平台游戲。 我希望能夠通過按下按鈕左右移動我的角色。 如何創建按鈕? 是否使用UIKitSKNode

如果可能,我想了解兩種方法或最有效的方法。

提前致謝!!!!

瑪特·A

處理用戶新聞事件的常用策略是使用系統提供的func touchesBegan:touches withEvent:event

//add trigger node into your scene
let triggerNode = SKSpriteNode(texture: SKTextureAtlas(named: "scenery.atlas").textureNamed(soundImg))
triggerNode.name = "TriggerNodeName"
triggerNode.zPosition = 10
addChild(triggerNode)

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches{
        let location = touch.location(in: self)
        let touchNode = atPoint(location)
        print("\(touchNode.name)")

        if touchNode.name == "TriggerNodeName"{
            //write your logic here
            ...
            return
        }
     }
}

這是我的工作:當touchesBegan開始時,我的播放器將向左走。 然后,我使用touchesEnded告訴播放器停止播放。

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        for touch in touches{
            let location = touch.location(in: self)
            let touchNode = atPoint(location)
            print("\(touchNode.name)")

            if touchNode.name == "Button"{
                //write your logic here
                thePlayer.goLeft()
                print("Pressed!")
            }
        }


    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {


        for touch in touches{
            let location = touch.location(in: self)
            let touchNode = atPoint(location)
            print("\(touchNode.name)")

            if touchNode.name == "Button"{
                //write your logic here
                thePlayer.stop()
                print("Pressed!")
            }
        }


    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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