我正在尝试为tv做基本的2D蛇游戏。
手势有问题。我在GameScene类的didMoveToView(view: SKView)
方法中添加手势来查看。本课程还扩展了SKScene类。这是我的didMoveToView
方法和handleSwipe
方法。
override func didMoveToView(view: SKView) {
let swipeUp = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
swipeUp.direction = .Up
view.addGestureRecognizer(swipeUp)
let swipeDown = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
swipeDown.direction = .Down
view.addGestureRecognizer(swipeDown)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
swipeLeft.direction = .Left
view.addGestureRecognizer(swipeLeft)
let swipeRight = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
swipeRight.direction = .Right
view.addGestureRecognizer(swipeRight)
backgroundColor = SKColor.blackColor()
}
func handleSwipe(sender: UIGestureRecognizer) {
if let gesture = sender as? UISwipeGestureRecognizer {
switch(gesture.direction) {
case UISwipeGestureRecognizerDirection.Right: m_snake.moveRight()
case UISwipeGestureRecognizerDirection.Left: m_snake.moveLeft()
case UISwipeGestureRecognizerDirection.Up: m_snake.moveUp()
case UISwipeGestureRecognizerDirection.Down: m_snake.moveDown()
default:
break
}
}
}
当我在遥控器上滑动时,似乎我的程序没有输入handleSwipe
方法。我在模拟器上测试这个。知道为什么这不起作用吗?
答案 0 :(得分:3)
func handleSwipes(sender:UISwipeGestureRecognizer) {
if let gesture = sender as? UISwipeGestureRecognizer {
switch(gesture.direction) {
case UISwipeGestureRecognizerDirection.Right: m_snake.moveRight()
case UISwipeGestureRecognizerDirection.Left: m_snake.moveLeft()
case UISwipeGestureRecognizerDirection.Up: m_snake.moveUp()
case UISwipeGestureRecognizerDirection.Down: m_snake.moveDown()
default:
break
}
}
}
我刚刚在我当前的项目中测试了你的代码,它工作得很好
要使滑动工作,请像这样配置模拟器;