Hello,
I’ve been trying to figure out how to change the gravity (Always pointing down) of the device based on the orientation, and i haven’t been able to do it. I wrote this code and it seems to be working fine, except for when in a certain orientation the ball freezes. ANY help is GREATLY appreciated!
Thanks,
Chris
–Set variable for width and height of device
_W = display.contentWidth;
_H = display.contentHeight;
–Hide Status Bar
display.setStatusBar(display.HiddenStatusBar);
–Start Physics
local physics = require (“physics”);
physics.start();
physics.setGravity(9.8,0);
physics.setScale( 60 )
physics.setDrawMode("");
–Set Accelerometer to maximum responsiveness
system.setAccelerometerInterval( 100 );
–Change gravity based on orientaiton of the device
local function left(event)
if(event.xGravity < -0.4) then
physics.setGravity(-9.8, 0);
ball.y = ball.y + (-5 * event.yGravity);
end
end
local function right(event)
if(event.xGravity > 0.4) then
physics.setGravity(9.8, 0);
ball.y = ball.y + (-5 * event.yGravity);
end
end
local function top(event)
if(event.yGravity > 0.4) then
physics.setGravity(0, -9.8);
ball.x = ball.x + (5 * event.xGravity);
end
end
local function down(event)
if(event.yGravity < -0.4) then
physics.setGravity(0, 9.8);
ball.x = ball.x + (5 * event.xGravity);
end
end
–Background Image
background = display.newImage(“background.png”);
–Ball Image
ball = display.newImage(“ball.png”);
ball.x = _W * 0.5; ball.y = _H * 0.5;
physics.addBody(ball, “dynamic”, {radius=37});
–Walls
top_wall = display.newRect(0, 0, _W, 5);
top_wall:setReferencePoint(display.CenterReferencePoint);
top_wall.y = -5;
physics.addBody(top_wall, “static”);
bottom_wall = display.newRect(0, 0, _W, 5);
bottom_wall:setReferencePoint(display.CenterReferencePoint);
bottom_wall.y = 485;
physics.addBody(bottom_wall, “static”);
left_wall = display.newRect(0, 0, 0, _H);
left_wall:setReferencePoint(display.CenterReferencePoint);
physics.addBody(left_wall, “static”);
right_wall = display.newRect(0, 0, 0, _H);
right_wall:setReferencePoint(display.CenterReferencePoint);
right_wall.x = 321;
physics.addBody(right_wall, “static”);
–Add event listener’s
Runtime:addEventListener(“accelerometer”, left);
Runtime:addEventListener(“accelerometer”, right);
Runtime:addEventListener(“accelerometer”, top);
Runtime:addEventListener(“accelerometer”, down);
[import]uid: 17138 topic_id: 8926 reply_id: 308926[/import]