我想通过左右倾斜我的设备来移动一个球体。我现在拥有的代码正在运行,除了我必须上下倾斜设备 - 当我声明 data.acceleration.y --> Y 轴?
这是我的代码:
motionManager = [[CMMotionManager alloc] init];
if ([motionManager isAccelerometerAvailable] == YES) {
[motionManager startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init]
withHandler:^(CMAccelerometerData *data, NSError *error)
{
float destX, destY;
float currentX = sphereNode.position.x;
float currentY = sphereNode.position.y;
BOOL shouldMove = NO;
if(data.acceleration.y < -0.25) { // tilting the device to the right
destX = currentX + (data.acceleration.y * kPlayerSpeed);
destY = currentY;
shouldMove = YES;
} else if (data.acceleration.y > 0.25) { // tilting the device to the left
destX = currentX + (data.acceleration.y * kPlayerSpeed);
destY = currentY;
shouldMove = YES;
}
if(shouldMove) {
SKAction *action = [SKAction moveTo:CGPointMake(destX, destY) duration:1];
[sphereNode runAction:action];
}
}];
}
非常感谢任何帮助!
最佳答案
Y 轴确实与设备的顶部和底部对齐。您很可能正在寻找 CMAccelerometerData 数据对象的 x 轴。
这一切都在 Event handling guide for iOS : Motion Events 中进行了解释文档。
也许看看一些教程(例如 iOS Programming Recipe 19: Using Core Motion to Access Gyro and Accelerometer )也可以帮助您。
关于ios - 通过倾斜设备移动节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21832506/