[英]How do I create a pause and unpause button in Swift and Sprite Kit?
[英]How do create a button using Sprite Kit and Swift?
嗨,我目前正在為iOS開發平台游戲。 我希望能夠通過按下按鈕左右移動我的角色。 如何創建按鈕? 是否使用UIKit
或SKNode
?
如果可能,我想了解兩種方法或最有效的方法。
提前致謝!!!!
瑪特·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.