繁体   English   中英

使用UIPanGestureRecognizer轻拂SpriteKit Sprite

[英]Using a UIPanGestureRecognizer to flick a SpriteKit sprite

我正在尝试使用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-可能对于我想要实现的代码来说,此代码太简单了,我应该计算在施加力之前精灵应该移动多远,以便可以根据其运动给定特定的速度矢量?

我只是通过这种方式(快速)解决了这个问题:

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)
}

我不知道为什么这种方法有效,只是偶然发现它。 但这实际上适用于任何大小的视图。

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) 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM