【发布时间】:2016-10-24 00:26:02
【问题描述】:
好的,目前在我的游戏中,我可以拖动、滑动和轻弹节点。但是,我希望用户只能滑动/轻拂节点,并且我不希望用户能够拖动节点。我该怎么做?
这是我当前的代码:
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)
}
}
【问题讨论】:
-
请更清楚地定义您想要实现的目标。
-
我猜:您的目标是让节点在手指从滑动手势中释放之前不移动,此时手指在屏幕上移动的速率和距离决定了速率和节点的行进方向?
标签: ios swift sprite-kit game-physics