繁体   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