Can't keep objects whithin screen

So I’ve been trying to make a game similar to asteroids following Corona’s tutorial and then modifying, adding features etc. For some reason I can’t keep my ship 100% inside the screen.I’ve tried creating invisible walls and adding bouncing to them, and I’ve tried adding them to a collision function(the ship seems to detect collision but it doesn’t do anything). Here’s my code:(outside of any composer’s scenes)

local function onCollision(event) if event.phase == 'began' then local obj1 = event.object1 local obj2 = event.object2 if obj1.myName == 'laser' and obj2.myName == 'asteroid' or obj1.myName == 'asteroid' and obj2.myName == 'laser' then -- Remove both the laser and asteroid display.remove(obj1) display.remove(obj2) for i = #asteroidsTable, 1, -1 do if asteroidsTable[i] == obj1 or asteroidsTable[i] == obj2 then table.remove(asteroidsTable, i) break end end -- Increase score score = score + 100 scoreText.text = 'Score: ' .. score elseif obj1.myName == 'ship' and obj2.myName == 'asteroid' or obj1.myName == 'asteroid' and obj2.myName == 'ship' then audio.play(hitSound, {channel = 2})-- Ship gets hit displayRandom\_explosions() if died == false then died = true -- Update lives lives = lives - 1 livesText.text = 'Lives: ' .. lives if lives == 0 then display.remove(ship) timer.performWithDelay(2000, endGame) else ship.alpha = 0 timer.performWithDelay(1000, restoreShip) end end elseif obj1.myName == 'ship' and obj2.myName == 'leftWall' or obj1.myName == 'leftWall' and obj2.myName == 'ship' then ship.x = 50 end end end

and:(in composer’s scene:create(event))

 local leftWall = display.newRect(backGroup, 20, display.contentHeight-100, 10, 10)--invisible wall 1 local rightWall = display.newRect(backGroup, display.contentWidth-20, display.contentHeight-100, 10, 10)--invisible wall 2 physics.addBody(leftWall, 'static', {bounce = 0.1}) physics.addBody(rightWall, 'static', {bounce = 0.1}) leftWall.myName = 'leftWall' rightWall.myName = 'rightWall' local background = display.newImageRect(backGroup, 'background.png', 800, 1400) background.x = display.contentCenterX background.y = display.contentCenterY ship = display.newImageRect(mainGroup, objectSheet, 4, 98, 79) ship.x = display.contentCenterX ship.y = display.contentHeight - 100 physics.addBody(ship, {radius=30, isSensor=true}) ship.myName = 'ship'

THANK YOU SO MUCH FOR YOUR TIME  :D  :D  ;)  :wink:

Hi.  Manually adjusting the x and y position of an object will conflict with the physics system and give you un-specified results. 

That is, you can force the ‘ship’ off the edge of the screen by directly modifying the x position and no physics ‘blocker’ will stop it.

So, instead do this:

  1. Pre-calculate the min and max X positions corresponding to the left-most and right-most position you want the ship to move to .

  2. In your movement code, check if the ship is too far left or too far right and if so, move it back inside the min,max bounds.

Perfect! Thank you! I added a test to see if the ship is inside the screen to both sides inside the movement of the ship(‘ended’).

you can’t go off camera; Keeping a rigidbody within the screen bounds?

Not sure I understand your question.

You can move a body off screen even if it is ‘blocked’.  Why? How?  Because manually updating a position bypasses the physics system.

If you’re moving a body with forces all is well, but again manually setting <x,y> will cause unexpected behaviors.

Hi.  Manually adjusting the x and y position of an object will conflict with the physics system and give you un-specified results. 

That is, you can force the ‘ship’ off the edge of the screen by directly modifying the x position and no physics ‘blocker’ will stop it.

So, instead do this:

  1. Pre-calculate the min and max X positions corresponding to the left-most and right-most position you want the ship to move to .

  2. In your movement code, check if the ship is too far left or too far right and if so, move it back inside the min,max bounds.

Perfect! Thank you! I added a test to see if the ship is inside the screen to both sides inside the movement of the ship(‘ended’).

you can’t go off camera; Keeping a rigidbody within the screen bounds?

Not sure I understand your question.

You can move a body off screen even if it is ‘blocked’.  Why? How?  Because manually updating a position bypasses the physics system.

If you’re moving a body with forces all is well, but again manually setting <x,y> will cause unexpected behaviors.