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 ;)