将游戏屏幕分成左右触摸?
Splitting game screen into right and left touch?
所以我是 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
}
}
}
touchesBegan
中的 touch
是什么?我认为你应该像这样使用 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
}
}
所以我是 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
}
}
}
touchesBegan
中的 touch
是什么?我认为你应该像这样使用 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
}
}