ios - swift SpriteKit : Creating a realistic driving experience 2D

标签 ios swift sprite-kit

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

我正在使用带有 SpriteKit 的 iOS Swift 3,当我点击 throttle 按钮时使用函数 applyForce 来加速,当我刹车时我给 physicsBody 添加了摩擦力,而且我似乎无法右转,不知道该怎么做

现在为了转弯,我正在使用左转和右转来分割屏幕,但我正在使用 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

最佳答案

评论的整合

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

}

关于ios - swift SpriteKit : Creating a realistic driving experience 2D,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45150465/

相关文章:

json - 解析 Alamofire json 响应

ios - 使某个 Sprite 忽略 SKPhysicsBody

iOS 9 Sprite Kit 自定义着色器似乎被忽略

ios - iphone可以拍红眼照片吗

ios - 如何存储当前的 MKCooperativeRegion 或缩放级别?

ios - 有什么方法可以阻止 UIImageView 中的图像在 iOS 上使用 SDWebImage 延迟加载?

ios - 在 iOS 中从 map 生成图像

ios - 恢复码paymentQueue RestoreCompletedTransactions Finished() 是不是必须要有?

ios - 表格单元格的 label.text 返回 nil (swift)

ios - SpriteKit 边框除底部外的所有边