【问题标题】:Move SKSpriteNode with CoreMotion使用 CoreMotion 移动 SKSpriteNode
【发布时间】:2014-06-16 21:22:56
【问题描述】:

我有一个代表用户的 SKSpriteNode,我希望这个节点随着用户的移动而移动,如果用户走路,那么节点会朝同一个方向移动。

起始位置是视图的中心,我认为可以使用 CMMotionManager (iOS 7) 中的 rotationRate、userAcceleration 和重力使其工作,但我不知道我是否错了或我应该做什么操作使用这些值。

如果有人可以帮助我,我很感激。

谢谢。

【问题讨论】:

  • 你能展示一下你到目前为止所做的尝试,以及给你带来了什么结果吗?
  • 你可以在这里找到一个很好的使用加速度计和 SpriteKit 的教程:code.tutsplus.com/tutorials/…

标签: sprite-kit gravity core-motion skspritenode cmmotionmanager


【解决方案1】:

现在可以了

if ([self.motionManager isDeviceMotionAvailable] == YES) {
            [self.motionManager startDeviceMotionUpdatesToQueue:self.queueMotion withHandler:^(CMDeviceMotion *motion, NSError *error) {
                self.xpos = motion.userAcceleration.x * motion.gravity.x * motion.rotationRate.x;
                self.ypos = motion.userAcceleration.y * motion.gravity.y * motion.rotationRate.y;

                [self performSelectorOnMainThread:@selector(updatePosition:) withObject:nil waitUntilDone:YES];
            }];
        }

然后我将这些 x 和 y 坐标添加到 SKSpriteNode 位置,并将 SKSpriteNode 移动到这个最终位置。我得到的是 SKSpriteNode 在我行走时不会移动,但如果我大力加速它会移动但只是片刻,如果我以恒定速度行走它不会移动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多