SpriteKit Swift:触摸移动的物体 [英] SpriteKiit Swift: touch a moving object
本文介绍了SpriteKit Swift:触摸移动的物体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个可以在墙上移动和反弹的 Color Sprite 对象.当我抓住它并在屏幕上触摸它时,如何使它消失?
I have a Color Sprite object which can move and bounce at the wall. How can I make it disappear when I catch it and touch it on the screen?
推荐答案
你必须为你的精灵设置一个名字,比如ballNode",然后在touchesBegan"函数中处理它.
you have to set a name for your sprite like "ballNode", then in "touchesBegan" function you can handle it.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in (touches) {
let positionInScene = touch.location(in: self)
let touchedNode = self.atPoint(positionInScene)
if let name = touchedNode.name {
if name == "ballNode" {
//make it hidden by touchedNode.isHidden = true
//or remove it from parent by touchedNode.removeFromParent()
}
}
}
}
这篇关于SpriteKit Swift:触摸移动的物体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文