OGeek|极客世界-中国程序员成长平台

标题: ios - Swift Sprite-kit 移动对象 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:25
标题: ios - Swift Sprite-kit 移动对象

我现在正在使用 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 轴上向后和第四个没有有问题吗?



Best Answer-推荐答案


每个 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 调整为您喜欢的值,也取决于您的 Sprite 有多大。

希望对你有帮助

关于ios - Swift Sprite-kit 移动对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39256670/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4