Swift SpriteKit:创造逼真的驾驶体验2D

时间:2017-07-17 17:41:56

标签: ios swift sprite-kit

如何创造逼真的驾驶体验?

我正在使用带有SpriteKit的iOS Swift 3使用函数applyForce加速当我点击加油按钮时制动时我向物理部门添加了摩擦力,我也似乎无法向右转,也不知道该如何做它

现在转向我正在使用左右转动分割屏幕,但是我正在使用applyForce,但它非常糟糕,因为它在汽车停止时以非常不切实际的方式转动。

当我施加力时,它只会上升,所以如果我创造一个转动机制并且我做了一次,那么汽车仍然会上升。

另外注意:多点触控不起作用?

有任何帮助吗?感谢

override func didMove(to view: SKView) {

    // Multi Touch
    self.view?.isMultipleTouchEnabled = true

    car.carNode.position = CGPoint(x: 0, y: 0)
    car.carNode.physicsBody = SKPhysicsBody(rectangleOf: car.carNode.size)
    car.carNode.physicsBody?.affectedByGravity = false
    car.carNode.physicsBody?.angularDamping = 0.1
    car.carNode.physicsBody?.linearDamping = 0.1
    car.carNode.physicsBody?.friction = 0.1
    car.carNode.physicsBody?.mass = 1

    self.addChild(car.carNode)

    // HUD Display
    gas.gasButton.position = CGPoint(x: 300, y: -500)
    self.addChild(gas.gasButton)

    brake.brakeButton.position = CGPoint(x: 150, y: -500)
    self.addChild(brake.brakeButton)

}

override func update(_ currentTime: TimeInterval) {

if touching {

    car.carNode.physicsBody?.applyForce(CGVector(dx: 0, dy: 180))
  }
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

for touch in touches {
    let location = touch.location(in: self)

    if location.x < self.frame.size.width / 2 {

        // Left side of the screen
        car.carNode.physicsBody?.applyForce(CGVector(dx: -100, dy: 0))

    } else {

        // Right side of the screen
        car.carNode.physicsBody?.applyForce(CGVector(dx: 100, dy: 0))
    }

    // Gas Button
    if (gas.gasButton.contains(location)) {

        touching = true
    }
        // Brake Button
    else if (brake.brakeButton.contains(location)) {

        car.carNode.physicsBody?.friction = 1
    }

  }
}

game

1 个答案:

答案 0 :(得分:0)

整合评论

for touch in touches {
    let location = touch.location(in: self)

    let velY = car.carNode.physicsBody?.velocity.dy
    let factor:CGFloat = 100
    let maxFactor:CGFloat = 300
    //limit
    let dxCalc = factor * velY > maxFactor ? maxFactor : factor * velY

    if location.x < self.frame.size.width / 2 {

        // Left side of the screen
       car.carNode.physicsBody?.applyForce(CGVector(dx: -dxCalc, dy: 0))

     } else {

        // Right side of the screen
        car.carNode.physicsBody?.applyForce(CGVector(dx: dxCalc, dy: 0))
    }

    // Gas Button
    //etc

} }