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