【问题标题】:Swift Sprite-kit move objectSwift Sprite-kit 移动对象
【发布时间】:2016-08-31 18:33:07
【问题描述】:

我现在正在使用 coreMotion 来移动我的主角,但是在使用此代码之后我遇到了设备方向问题。

    manager.startAccelerometerUpdates()
    let data = manager.accelerometerData
    manager.accelerometerUpdateInterval = 0.0001
    manager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()){
        (data, error) in
        self.plane.physicsBody!.applyForce(CGVectorMake(120.0 * CGFloat((data?.acceleration.x)!), 0))
    }

所以我的新问题是,我怎样才能通过触摸屏幕左侧或触摸屏幕右侧来移动我的对象,以便角色平滑移动并且可以在 x 轴上向后和第四个没有有问题吗?

【问题讨论】:

  • 你施加了很大的力,为什么你更新为0.001,1/60秒是0.016666666667,你在加速度计上投入了太多的工作

标签: ios swift xcode sprite-kit game-physics


【解决方案1】:

每个 SKScene 都有 4 种可供您使用的触摸方法(开始、结束、移动、取消)。

例如

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
     for touch in touches {
         let location = touch.locationInNode(self)

         if location.x < frame.midX { // left side
              plane.physicsBody?.applyForce(CGVectorMake(-120.0, 0)) // move left
         } else {
              plane.physicsBody?.applyForce(CGVectorMake(120.0, 0)) // move right
         }
     }
}

将 120 调整为您喜欢的值,也取决于您的精灵有多大。

希望对你有帮助

【讨论】:

  • 说得通但是它给出了一个错误,说不能将 CGPoint 的类型转换为 CGFloat 的预期参数类型? @crashoverride777
  • 是的,抱歉刚刚更新了我的答案。你需要说如果 location.x
猜你喜欢
  • 2020-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多