将游戏画面拆分为左右触控?

时间:2015-06-08 05:25:32

标签: ios swift uigesturerecognizer gesture

所以我是Swift的新手,我正在试图弄清楚如何将屏幕分成两半并使其成为如果屏幕的右侧被轻敲,那么对象将向右移动,如果点击屏幕的左侧,然后对象将向左移动。我试图玩弄手势,但我还没有走得太远。我怎样才能解决这个问题?它在locationinNode

时给出了错误

我正在使用故事板添加移动对象。

这就是我所拥有的:

import UIKit
class ViewController: UIViewController { 

@IBOutlet var Lane: [UIImageView]!
@IBOutlet weak var Player: UIImageView!
@IBOutlet weak var Platform: UIImageView!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func MoveLeft(){
    UIView.animateWithDuration(0.2, delay: 0, options:UIViewAnimationOptions.CurveLinear, animations: {
        self.Player.center = CGPoint(x: self.Player.center.x - 125, y: self.Player.center.y)
        }, completion: nil)
}

func MoveRight(){
    UIView.animateWithDuration(0.2, delay: 0, options:UIViewAnimationOptions.CurveLinear, animations: {
        self.Player.center = CGPoint(x: self.Player.center.x + 125, y: self.Player.center.y)
        }, completion: nil)
}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
 let location = touch.locationInNode(self)
 if location.x < self.size.width/2 {
// left code
MoveLeft()
 }
 else {
  // right code
 }

}

}

1 个答案:

答案 0 :(得分:4)

touch中的touchesBegan是什么?我认为你应该像touches.first as! UITouch这样使用:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    let location = (touches.first as! UITouch).locationInView(self.view)
    if location.x < self.view.bounds.size.width/2 { 
        // left code
        MoveLeft()
    } else {
        // right code
    }
}