Hello, I’m new to this forum. Hopefully someone will help me with this issue I’ve been having.
I’m creating a game where multiple objects move in random ways inside the screen. I need to avoid this objects going off screen. I’ve been trying to create walls close to the screen edges:
local leftWall = display.newLine (0, 480, 320, 1);
local rightWall = display.newLine (0, -1, 320, 1);
local ceiling = display.newLine (-1, 0, 1, 480);
local bottom = display.newLine (320, 0, 1, 480);
physics.addBody( leftWall, “static”, { density=1.0, friction=0.3, bounce=0.2 } )
physics.addBody( rightWall, “static”, {density=1.0, friction=0.3, bounce=0.2 } )
physics.addBody( ceiling, “static”, { density=1.0, friction=0.3, bounce=0.2 } )
physics.addBody( bottom, “static”, { density=1.0, friction=0.3, bounce=0.2 } )
Someone suggested me to use newLine instead of newRect since it’s easier to manage apparently… but it’s not working either way. With this particular version i only get 3 lines that basically divide the screen in half. I don’t really know why.
Does someone know any way to avoid my objects going off screen using some kind of boundaries or even functions?
I’m new at programming so please suggest possible solutions in the easiest way possible.
Thank you in advance!