Reputation: 89
I'm trying to start the accelerometer for a game I'm making using the new Swift programming language, but it doesn't seem to work. This is my code right now:
var motionManager=CMMotionManager()
var queue=NSOperationQueue()
motionManager.accelerometerUpdateInterval = (1/40)
motionManager.startAccelerometerUpdatesToQueue(queue, withHandler {(accelerometerData:CMAccelerometerData!, error:NSError!) in
self.update(accelerometerData.acceleration)
})
I'm not getting any errors, but when I run this nothing happens (my update function isn't called). What exactly is wrong with this?
Upvotes: 0
Views: 1056
Reputation: 16399
Your motionManager
and queue
are local variables, so the objects are deallocated at the end of this function. Store them somewhere, like in an instance variable. I've made a similar mistake a bunch of times myself.
Upvotes: 0
Reputation: 1542
Is the accelerometer available? check if motionManager.accelerometerAvailable
is false. Maybe you just don't have it the the device you're running on.
Note that you should use something like 1.0/40.0
for the interval to be what you expect.
if you use the simulator you can't test the accelerometer. See in Can i test accelerometer effect in Xcode simulator?
Upvotes: 1