Control object with the accelerometer

What’s wrong with this code? When I test my app on my device the controls are messed up. I’m using landscape mode and when I tilt the phone north the ball moves to the right…

[code]
– Accelerometer
local motionx = 0
local motiony = 0

local function onAccelerate(event)
motionx = 15 * event.xGravity
motiony = 15 * event.yGravity
end
Runtime:addEventListener(“accelerometer”, onAccelerate)

local function moveBall(event)
ball.x = ball.x - motionx
ball.y = ball.y - motiony
end
Runtime:addEventListener(“enterFrame”, moveBall)
[/code] [import]uid: 14018 topic_id: 5545 reply_id: 305545[/import]

Accelerometer movements are based on a portrait scale, so when you are using landscape, x becomes y and y becomes x.

This code should work for you :slight_smile:

[code]
– Accelerometer
local motionx = 0
local motiony = 0

local function onAccelerate(event)
motionx = 15 * event.yGravity
motiony = 15 * event.xGravity
end
Runtime:addEventListener(“accelerometer”, onAccelerate)

local function moveBall(event)
ball.x = ball.x - motionx
ball.y = ball.y - motiony
end
Runtime:addEventListener(“enterFrame”, moveBall)
[/code] [import]uid: 7143 topic_id: 5545 reply_id: 18793[/import]

Thanks a lot, works great now :slight_smile: [import]uid: 14018 topic_id: 5545 reply_id: 18802[/import]

You’re welcome :slight_smile:

// Ed. [import]uid: 7143 topic_id: 5545 reply_id: 18803[/import]