0

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

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

我很感激有人可以帮助我。

谢谢。

4

1 回答 1

0

现在,我做

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 在我行走时不会移动,但如果我大力加速它会移动但只是片刻,如果我以恒定速度行走它不会移动。

于 2014-06-17T20:24:58.953 回答