1
我想通過左右傾斜設備來移動球體。我現在的代碼正在工作,除了我必須上下傾斜設備 - 而我狀態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更改爲x來解決它。感謝這個文檔! –