1
好吧,目前在我的遊戲中,我可以拖動,滑動並輕拂節點。但是,我希望用戶只能輕掃/輕拂節點,並且我不希望用戶能夠拖動節點。我怎樣才能做到這一點?如何在不拖動的情況下輕彈/滑動精靈?
這裏是我當前的代碼:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
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
for touch in touches {
let location = touch.location(in: self)
touchPoint = location
if touching {
if touchPoint != ball.position {
let dt:CGFloat = 1.0/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
}
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
touching = false
for touch in touches {
let location = touch.location(in: self)
touchPoint = location
let node = self.atPoint(location)
}
}
請定義更清楚你想達到什麼。 – matthiaswitt
我在猜測:你的目標是讓節點不動,直到手指從滑動手勢釋放爲止,此時手指在屏幕上的移動速度和距離決定了節點的移動速度和方向? – Confused