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