我在我的viewcontroller中使用此代码通过滑动手势在GameScene中移动播放器:
override func viewDidLoad() {
super.viewDidLoad()
var scene:GameMenuScene!
var scene1: GameScene!
var scene2: LevelScene!
let skView = view as SKView
skView.multipleTouchEnabled = false
scene = GameMenuScene(size: skView.bounds.size)
skView.showsFPS = true
skView.showsNodeCount = true
skView.presentScene(scene)
let swipeRight:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedRight:"))
swipeRight.direction = .Right
view.addGestureRecognizer(swipeRight)
let swipeLeft:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedLeft:"))
swipeLeft.direction = .Left
view.addGestureRecognizer(swipeLeft)
let swipeUp:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedUp:"))
swipeUp.direction = .Up
view.addGestureRecognizer(swipeUp)
let swipeDown:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedDown:"))
swipeDown.direction = .Down
view.addGestureRecognizer(swipeDown)
}
func swipedRight(sender: UISwipeGestureRecognizer){
let skView = view as SKView
let gameScene = skView.scene as GameScene
gameScene.swipedRight1(sender)
}
func swipedLeft(sender: UISwipeGestureRecognizer){
let skView = view as SKView
let gameScene = skView.scene as GameScene
gameScene.swipedLeft1(sender)
}
func swipedDown(sender: UISwipeGestureRecognizer){
let skView = view as SKView
let gameScene = skView.scene as GameScene
gameScene.swipedDown1(sender)
}
func swipedUp(sender: UISwipeGestureRecognizer){
let skView = view as SKView
let gameScene = skView.scene as GameScene
gameScene.swipedUp1(sender)
}
问题:当我在另一个屏幕而不是gameScene并且我滑动时,游戏崩溃是因为"无法识别的选择器已发送",所以我想在GameScene而不是LevelScene中使用滑动功能或GameMenuScene。对于GameMenuScene和LevelScene,我想添加其他滑动功能。
如何检测我所在的场景,以便制作if语句?
谢谢
答案 0 :(得分:0)
添加这个......
override func willMoveFromView(view: SKView) {
if view.gestureRecognizers != nil {
for gesture in view.gestureRecognizers! {
if let recognizer = gesture as? UISwipeGestureRecognizer {
view.removeGestureRecognizer(recognizer)
}
}
}
}