Accelerometer Question
Hey there everyone,
I've got a question regarding implementing the iOS accelerometer into one of my games. The game I want to incorporate this into is an iPhone game in Landscape mode. I want to control an Actor's vertical position by using the accelerometer. Can someone post the code for that, as the template I usually use is failing me
Thanks in advance!
- Alex
I've got a question regarding implementing the iOS accelerometer into one of my games. The game I want to incorporate this into is an iPhone game in Landscape mode. I want to control an Actor's vertical position by using the accelerometer. Can someone post the code for that, as the template I usually use is failing me

- Alex
Comments
when attribute device accelerometer x is less then or equal to -0.2 move down
another way to do it thats smoother is :
Constrain self position y to : self.Position.Y +((min(.5,max(-.5,( game.Accelerometer.X + 0 ))))*17)
If you wanted to go one step further and have a calibrate button for your accelerometer if you use that constrain, make a attribute calibrate and in the constrain position towards the end where its +0 change that to +game.calibrate
then in your calibrate button ahve when touch is pressed change attribute calibrate to: game.Accelerometer.X *-1
cheers
- Alex
One quick question regarding the constrain attribute above.
I want it set up that when the actor is going up, it's rotation changes to 5, and when the actor is going down, its rotation changes to -5. While keeping the same constrain attribute behavior above, is there a rule I can add in to determine if the actor is moving up and down so I can change its rotation accordingly? Thanks again for your help on this.
- Alex
When game.Accelerometer.Y > .02
....Change Attribute: self.Rotation To: 5
When game.Accelerometer.Y <.02
....Change Attribute: self.Rotation To: -5
but it will be accelerometer.x not y
When Accelerometer.X > .02
Change Rotation to 5
When Accelerometer.X < .02
Change Rotation to -5
but when I tested this on my device, it didn't work. My rotation just stayed at 0. By the way, I am using the calibrate method you described earlier John, and it works great, but I don't know if it would effect this. Thanks again guys.
- Alex
I have it set up for the game to autorotate. However, if the user flips the device from one landscape mode to the other, the accelerometer controls don't work as intended. If you go back and re-calibrate with the new device orientation, the accelerometer controls are opposite. Can you provide some assistance with this issue also?
You will need to continually test for the accelerometer data (put them into a timer).
About the orientation:
My guess is that you will probably need four separate rule sets. One for each orientation.
You will need to flip the rules for Accelerometer.X between both landscape modes.
You will need to flip the rules for Accelerometer.Y between both portrait modes.
(Negative becomes positive, and positive becomes negative.)
If game.Screen.Device Orientation is Landscape Left
// Use constrain attribute from above //
If game.Screen.Device Orientation is Landscape Right
// swap all positive and negative game.Accelerometer.X values with the constrain above //
Is this what you mean? Thanks again for your help!
Cheers
-Matt