在 Swift 和 sprite kit 中如何检测滑动的精灵?
In Swift and sprite kit how to detect swiped sprites?
我在屏幕上有移动的精灵。现在我确实抓住了触摸,如果触摸了精灵 - 我将其删除。但我想把它刷掉。意思是 - 我滑动它(任何方向),如果它是一个正确的精灵(检查名称) - 删除它。
我有一个触摸代码,但我认为它不需要粘贴在这里,它很标准,滑动代码肯定会有所不同。
有什么建议吗?谢谢!
您可以使用 UISwipeGestureRecognizer
并获取开始触摸位置。之后,您可以检查哪个节点位于该位置。
像那样:
override func didMoveToView(view: SKView) {
//create Swipe gesturerecognizer
var swipe = UISwipeGestureRecognizer()
//set Direction to Left
swipe.direction = .Left
//Call method "swipe" if swipe is done
swipe.addTarget(self, action: "swipe:")
self.view!.addGestureRecognizer(swipe)
}
func swipe(sender: UISwipeGestureRecognizer){
//get amount of touches in swipe
var touches = sender.numberOfTouches()
//loop through touches
for touch in 0..<touches{
var location = sender.locationOfTouch(touch, inView: self.view)
//Get the node at the location of the touch
var swipedNode = self.nodeAtPoint(location)
}
}
试试这个:
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent)
{
let touch = touches.first as! UITouch
let location = touch.locationInNode(self)
if sprite.frame.contains(location)
{
//remove sprite here
}
}
此外,如果您希望它在触摸时被删除,也请添加:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
let touch = touches.first as! UITouch
let location = touch.locationInNode(self)
if sprite.frame.contains(location)
{
//remove sprite here
}
}
我在屏幕上有移动的精灵。现在我确实抓住了触摸,如果触摸了精灵 - 我将其删除。但我想把它刷掉。意思是 - 我滑动它(任何方向),如果它是一个正确的精灵(检查名称) - 删除它。
我有一个触摸代码,但我认为它不需要粘贴在这里,它很标准,滑动代码肯定会有所不同。
有什么建议吗?谢谢!
您可以使用 UISwipeGestureRecognizer
并获取开始触摸位置。之后,您可以检查哪个节点位于该位置。
像那样:
override func didMoveToView(view: SKView) {
//create Swipe gesturerecognizer
var swipe = UISwipeGestureRecognizer()
//set Direction to Left
swipe.direction = .Left
//Call method "swipe" if swipe is done
swipe.addTarget(self, action: "swipe:")
self.view!.addGestureRecognizer(swipe)
}
func swipe(sender: UISwipeGestureRecognizer){
//get amount of touches in swipe
var touches = sender.numberOfTouches()
//loop through touches
for touch in 0..<touches{
var location = sender.locationOfTouch(touch, inView: self.view)
//Get the node at the location of the touch
var swipedNode = self.nodeAtPoint(location)
}
}
试试这个:
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent)
{
let touch = touches.first as! UITouch
let location = touch.locationInNode(self)
if sprite.frame.contains(location)
{
//remove sprite here
}
}
此外,如果您希望它在触摸时被删除,也请添加:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
let touch = touches.first as! UITouch
let location = touch.locationInNode(self)
if sprite.frame.contains(location)
{
//remove sprite here
}
}