好的,所以目前在我的游戏中我有一个可以抛出的球。但是,我想让用户只能将球扔出某一点。我已经尝试了一段时间,但我无法弄明白。我怎样才能做到这一点?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first as! UITouch?
let location = touch?.location(in: self)
if ball.frame.contains(location!) {
touchPoint = location!
touching = true
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first as! UITouch?
let location = touch?.location(in: self)
touchPoint = location!
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
touching = false
}
override func update(_ currentTime: TimeInterval) {
if touching {
let dt:CGFloat = 2.8/60.0
let distance = CGVector(dx: touchPoint.x-ball.position.x, dy: touchPoint.y-ball.position.y)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
ball.physicsBody!.velocity=velocity
}
}
到目前为止,我有一张我的游戏图片。目前,可以在屏幕上的任何位置拖动,抛出或滑动球。但是,我想让球只能被触摸,抛出或拖动到屏幕中间的下方。但是如果用户的手指意外地达到极限,我希望球继续被抛出。
答案 0 :(得分:0)
您必须根据实际大小和游戏场景anchorPoint调整底部区域的坐标。例如,示例中的“100”假定您的场景的anchorPoint为(0,0),底部滚动区域的大小为100。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first as UITouch! {
let location = touch.location(in: self)
guard location < 100 else { return }
//if ball.frame.contains(location!) {
touchPoint = location!
touching = true
//}
}
}