我正在尝试使用UIGestureRecognizers来在SpriteKit场景中移动精灵。到目前为止它移动得很好,但是一旦我把手指从屏幕上移开它就会停止。我现在要做的是将手势结束转换为精灵上的轻弹样式动作,使其继续沿我移动手指的方向,基于该运动的力度。
这就是我到目前为止所看到的水平运动,但仍然在Y轴上以错误的方向发送精灵。
if (recognizer.state == UIGestureRecognizerStateEnded) {
CGPoint velocity = [recognizer velocityInView:self.view];
velocity = [self convertPointFromView:velocity];
[touchedNode.physicsBody applyForce:CGVectorMake(velocity.x, velocity.y)];
}
更新 - 了解当前刷卡时会发生什么。平移和释放产生的效果导致精灵以正确的方向行进,但是向下添加了一个向下的角度。向下滑动会导致行程向上移动,向上滑动会导致行程向下移动。
更新2 - 可能是这段代码对于我想要实现的内容来说太简单了,我应该计算精灵在施加力之前应该移动多远,这样我可以根据它的运动给它一个特定的速度向量?
答案 0 :(得分:0)
我只是通过这种方式解决了这个问题(快速):
let transformerX = 1024/self.view!.frame.size.with
let transformerY = 768/self.view!.frame.size.height
if (recognizer.state == .ended) {
let velocity = recognizer.velocity(InView:self.view)
touchedNode.physicsBody?.applyForce:CGVector(dx:velocity.x* transformerX, dy: velocity.y* transformerY)
}
我不知道为什么这种方式有效,只是偶然发现它。但它确实适用于任何大小的视图。
答案 1 :(得分:0)
let transformerX = 1024/self.view!.frame.size.width
let transformerY = 768/self.view!.frame.size.height
if (recognizer.state == .ended) {
let velocity = recognizer.velocity(InView:self.view)
touchedNode.physicsBody?.applyForce:CGVector(dx:velocity.x* transformerX, dy: velocity.y* transformerY)
}