I am making a game in which you should be able to move the player by tilting the device left and right. I'm using the accelerometer for this. In my GameScene class I call movementSetup in my Player class which does this:
-(void)movementSetup {
self.motionManager = [[CMMotionManager alloc]init];
self.motionManager.accelerometerUpdateInterval = 0.2;
[self.motionManager startAccelerometerUpdates];
self.accelerometerData = self.motionManager.accelerometerData;
}
Then in the update method in my GameScene class I call my movement method in my Player class:
-(void)movement {
CGVector moveLeftVector = CGVectorMake(-1, 0);
CGVector moveRightVector = CGVectorMake(1, 0);
if (fabs(self.accelerometerData.acceleration.x)<1) {
SKAction *moveLeft = [SKAction moveBy:moveLeftVector duration:0.2];
[self.aPlayer runAction:moveLeft];
}
if (fabs(self.accelerometerData.acceleration.x)>1) {
SKAction *moveRight = [SKAction moveBy:moveRightVector duration:0.2];
[self.aPlayer runAction:moveRight];
}
}
I also declared 3 properties in my player class a SKSpriteNode *aPlayer, a CMMotionManager *motionManager and a CMAccelerometerData *accelerometerData.
When I run this and test this on my device (iPhone 6) the sprite node just slowly moves to the left regardless of me tilting the device. Why?
EDIT: I also got a memory warning after a while
I just found out what I did wrong:
First of all move the self.accelerometerData = self.motionManager.accelerometerData to the movement method so it gets updated every frame.
Then change the lesser than and equal than values to something lower than 1 because the accelerometer won't get values higher than 1;