SpriteKit中的摩擦力

Dra*_*lex 1 game-physics sprite-kit skphysicsbody swift

我正在使用Swift,SpriteKit和Xcode 6,我的屏幕上有一个圆形节点,我可以用手指改变节点的位置,但我想对这个节点实施摩擦效果,但我现在不行怎么做,这是我的代码:

class PlayScene: SKScene
{    
let firstCircle = SKSpriteNode(imageNamed: "Circle")

// theses 3 variables allows me to move the node according to my finger, but my finger don't have to touch the node
var departCircleLocation = CGPoint()
var departTouchLocation = CGPoint()
var currentTouchLocation = CGPoint()

override func didMoveToView(view: SKView)
{
    addChild(firstCircle)
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
{
    for touch: AnyObject in touches
    {
        departTouchLocation = touch.locationInNode(self) 
    }

    departCircleLocation = firstCircle.position
}

override func touchesMoved(touches: NSSet, withEvent event: UIEvent)
{
    for touch: AnyObject in touches
    {
        currentTouchLocation = touch.locationInNode(self)
    }

    moveCircle()
}

func moveCircle()
{
    firstCircle.position.x = departCircleLocation.x - departTouchLocation.x + currentTouchLocation.x
    firstCircle.position.y = departCircleLocation.y - departTouchLocation.y + currentTouchLocation.y
}
}
Run Code Online (Sandbox Code Playgroud)

我试着把它放到我的代码中,但它不起作用:

firstCircle.physicsBody = SKPhysicsBody(circleOfRadius: 7)
firstCircle.physicsBody?.affectedByGravity = false
firstCircle.physicsBody?.friction = 0.4
Run Code Online (Sandbox Code Playgroud)

我不想引力,因为从上面看比赛

有人可以向我解释如何实现对此节点的摩擦?我希望例如当我以一定速度向上滑动并且当我松开手指时,节点继续上升一点时间,但是如果我再次将手指放在屏幕上,则摩擦立即停止,例如:

如果我将手指放在节点下50像素,它将什么也不做,但如果我将手指(节点下50像素)移动到节点下的80像素,节点将从30像素向下移动,这适用于任何x和y方向,我可以移动节点而不触摸它,但是如果我的手指它将沿着路径移动,现在如果我将手指从30像素快速移动并且我释放我的手指,节点将继续由于摩擦力下降,但我的手指不再在屏幕上(效果看起来像我们在iphone上滚动网站时,由于你的滑动速度松开手指时会有一点摩擦)但是我不能使用UISwipeGesture,因为它只能检测向上,向右,向下和向左的方向,而不是我想要的每个方向.我希望你能理解我在说什么

0x1*_*41E 5

这是一个如何基于触摸移动精灵的示例.这种方法使用velocity物理体的属性来移动精灵,因此它将与场景中的其他物理体适当地相互作用并受到阻尼,摩擦等的影响.

首先,定义比例和阻尼属性.的scale控制在该圆朝向触摸的位置移动的速率.值越大,圆圈移动速度越快.该damping属性用于在触摸结束时减慢精灵的速度.较小的值会以更快的速度减慢精灵的速度.

let scale:CGFloat = 2.0
let damping:CGFloat = 0.98

var point:CGPoint?
Run Code Online (Sandbox Code Playgroud)

触摸开始时保存位置.精灵将朝这个位置移动.

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {        
    if let touch = touches.anyObject() as UITouch? {
        let location = touch.locationInNode(self)
        point = location
    }
}
Run Code Online (Sandbox Code Playgroud)

触摸移动时更新位置

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
    if let touch = touches.anyObject() as UITouch? {
        let location = touch.locationInNode(self)
        point = location
    }
}
Run Code Online (Sandbox Code Playgroud)

触摸结束时重置触摸位置

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    point = nil
}
Run Code Online (Sandbox Code Playgroud)

将精灵移动到指定位置

func moveNodeToPoint(sprite:SKSpriteNode, point:CGPoint) {
    var dx:CGFloat = (point.x - sprite.position.x) * scale
    var dy:CGFloat = (point.y - sprite.position.y) * scale
    sprite.physicsBody?.velocity = CGVectorMake(dx, dy)
}
Run Code Online (Sandbox Code Playgroud)

在渲染每个帧之前调用它.如果触摸处于活动状态,它会调用更新精灵位置的函数,否则会随着时间的推移减慢精灵的速度

override func update(currentTime: CFTimeInterval) {
    if (point != nil) {
        moveNodeToPoint(firstCircle, point: point!)
    }
    else {
        // This will slow the circle over time when after the touch ends
        let dx = firstCircle.physicsBody!.velocity.dx * damping
        let dy = firstCircle.physicsBody!.velocity.dy * damping
        firstCircle.physicsBody!.velocity = CGVectorMake(dx, dy)
    }
}
Run Code Online (Sandbox Code Playgroud)