swift - SpriteKit 中的摩擦力

标签 swift sprite-kit game-physics skphysicsbody

我正在使用 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
}
}

我试图将其放入我的代码中,但它不起作用:

firstCircle.physicsBody = SKPhysicsBody(circleOfRadius: 7)
firstCircle.physicsBody?.affectedByGravity = false
firstCircle.physicsBody?.friction = 0.4

我不想要重力,因为游戏是从上面看的

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

如果我将手指放在节点下方 50 像素处,它不会执行任何操作,但如果我将手指(位于节点下方 50 像素处)移动到节点下方 80 像素处,节点将从 30 像素处向下移动,这有效在任何 x 和 y 方向上,我可以在不触摸它的情况下移动节点,但是如果我的手指它会沿着路径移动,例如现在如果我非常快速地将手指从 30 像素向下移动然后松开手指,节点将由于摩擦力继续向下,但我的手指不再在屏幕上(效果就像我们在 iphone 上滚动网站时,由于速度的原因,当你松开手指时有一点摩擦力)你的滑动),但我不能使用 UISwipeGesture,因为它只检测向上、向右、向下和向左方向,而不是我想要的每个方向。 我希望你能明白我在说什么

最佳答案

下面是一个如何根据触摸移动 Sprite 的示例。这种方法使用物理体的 velocity 属性来移动 Sprite ,因此它会与场景中的其他物理体进行适当的交互,并受到阻尼、摩擦等的影响。

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

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

var point:CGPoint?

在触摸开始时保存位置。 Sprite 会朝这个位置移动。

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

触摸移动时更新位置

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

触摸结束时重置触摸位置

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    point = nil
}

将 Sprite 移向指定位置

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

在渲染每一帧之前调用它。如果触摸处于事件状态,它会调用更新 Sprite 位置的函数,否则它会随着时间的推移减慢 Sprite

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

关于swift - SpriteKit 中的摩擦力,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27431085/

相关文章:

ios - 为什么在我的 SpriteKit 游戏中,移动逻辑不同?

box2d - 在没有重力的情况下减慢 box2d 中的物体

python - Pygame 具有类似流体的特征

swift - 在对象 deinit 的扩展中调用代码?

ios - 通用链接是否保留 POST 数据?

ios - iOS-如何获取与设备同步的电子邮件ID列表?

swift - SKSpriteNode 隐藏在父节点下方

swift - 如何在 MPMediaItem 中正确添加音乐编辑的谓词

swift - 如何用 SKPhysicsBody 复制 SKSpriteNode?

ios - 尝试制作一个平台,让我可以从下面跳过去,但可以降落在上面。难以微调逻辑