Tutorial is not working(?)

Hello everyone, I’m a begginer and I recently started coding in Corona. I also started the tutorial on this site, however, it seems that the tutorial is wrong or I am somehow dumb. I am talking about part 5. I tried to do it 5 times now, reading everything carefully, it didn’t work. Then I tried using the version bundled in the chapter source files, that file worked. Then I literally copied every line it wanted me to copy and it worked, but on my version somehow the ship is not colliding with asteroids and the gravity is not set to 0! I assign my/not working file and source file, can anyone please tell me what went wrong? Cause i inspected the whole file many times.

my file

local composer = require( “composer” )

local scene = composer.newScene()


– Code outside of the scene event functions below will only be executed ONCE unless

– the scene is removed entirely (not recycled) via “composer.removeScene()”


–Load physics–

local physics = require (“physics”)

physics.start()

physics.setGravity( 0, 0 )

–Image sheet

local sheetOptions =

{

  frames =

  {

    {

      --Asteroid 1 (1)

      x = 0,

      y = 0,

      width = 102,

      height = 85

    },

    {

      --Asteroid 2 (2)

      x = 0,

      y = 85,

      width = 90,

      height = 83

    },

    {

      --Asteroid 3 (3)

      x = 0,

      y = 168,

      width = 100,

      height = 97

    },

    {

      – Ship (4)

      x = 0,

      y = 265,

      width = 98,

      height = 79

    },

    {

      --Laser (5)

      x = 98,

      y = 265,

      width = 14,

      height = 40

    },

  },

}

local objectSheet = graphics.newImageSheet( “gameObjects.png”, sheetOptions )

– Initializing all viarables here boi

local lives = 3

local score = 0

local died = false

local asteroidsTable = {}

local Ship

local gameLoopTimer

local livesText

local scoreText

–Creating display groups

local backGroup

local mainGroup

local uiGroup

–Function “update the text”

local function UpdateText()

 livesText.text = "Lives: " … lives

 scoreText.text = "Score: " … score

end

local function createAsteroid()

local newAsteroid = display.newImageRect( mainGroup, objectSheet, 1, 102, 85 )

table.insert( asteroidsTable, newAsteroid )

physics.addBody( newAsteroid, “dynamic”, { radius=40, bounce=0.8 } )

newAsteroid.myName = “asteroid”

local whereFrom = math.random( 3 )

if ( whereFrom == 1 ) then

– From the left

newAsteroid.x = -60

newAsteroid.y = math.random( 500 )

newAsteroid:setLinearVelocity( math.random( 40,120 ), math.random( 20,60 ) )

elseif ( whereFrom == 2 ) then

– From the top

newAsteroid.x = math.random( display.contentWidth )

newAsteroid.y = -60

newAsteroid:setLinearVelocity( math.random( -40,40 ), math.random( 40,120 ) )

elseif ( whereFrom == 3 ) then

– From the right

newAsteroid.x = display.contentWidth + 60

newAsteroid.y = math.random( 500 )

newAsteroid:setLinearVelocity( math.random( -120,-40 ), math.random( 20,60 ) )

end

newAsteroid:applyTorque( math.random( -6,6 ) )

end

–Firing laser function

local function FireLaser()

  local newLaser = display.newImageRect( mainGroup, objectSheet, 5, 14, 40 )

  physics.addBody( newLaser, “dynamic”, {isSensor = true} )

  newLaser.isBullet = true

  newLaser.myName = “laser”

  newLaser.x  = Ship.x

  newLaser.y = Ship.y

  newLaser:toBack()

  transition.to(newLaser, { y = -40 , time = 500, onComplete = function() display.remove(newLaser) end })

end

local function dragTheShip( event )

  local Ship = event.target

  local phase = event.phase

  if (phase == “began”) then

    --Set OnTouch event

    display.currentStage:setFocus( Ship )

    --Store initial offset position

    Ship.touchOffsetX = event.x - Ship.x

  elseif (phase == “moved”) then

    --Move to touch position

    Ship.x = event.x - Ship.touchOffsetX

  elseif ( “ended” == phase or “cancelled” == phase ) then

      – Release touch focus on the Ship

      display.currentStage:setFocus( nil )

  end

  return true

end

local function gameLoop()

–creates new asteriod

  createAsteroid()

  for i = #asteroidsTable, 1, -1 do

    local CurrentAstroid = asteroidsTable[i]

      if (CurrentAstroid.x < -100 or

      CurrentAstroid.x > display.contentWidth + 100 or

      CurrentAstroid.y < -100 or

      CurrentAstroid.y > display.contentHeight + 100)

        then

        display.remove(CurrentAstroid)

      table.remove( asteroidsTable, i)

    end

  end

end

local function RestoreShip ()

   Ship.isBodyActive = false

   Ship.x = display.contentCenterX

   Ship.y = display.contentHeight - 100

   --Fading the Ship

   transition.to(Ship, {alpha = 1, time = 4000,

    onComplete = function()

      Ship.isBodyActive = true

      died = false

  end

    })

end

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

      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

      UpdateText()

    elseif ((obj1.myName == “Ship” and obj2.myName == “asteroid”) or (obj1.myName == “Ship” and obj2.myName == “asteroid”)) then

      if (died == false) then

        died = true

        lives = lives - 1

        UpdateText()

        if (lives == 0) then

          display.remove( Ship )

        else

          Ship.alpha = 0

          timer.performWithDelay( 1000, RestoreShip )

        end

      end

    end

  end

end


– Scene event functions


– create()

function scene:create( event )

local sceneGroup = self.view

physics.stop()

backGroup = display.newGroup()

  sceneGroup:insert( backGroup )

mainGroup = display.newGroup()

sceneGroup:insert( mainGroup )

uiGroup = display.newGroup()

sceneGroup:insert( uiGroup )

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”

livesText = display.newText( uiGroup, "Lives: " … lives, 200, 80, native.systemFont, 36 )

  scoreText = display.newText( uiGroup, "Score: " … score, 400, 80, native.systemFont, 36 )

Ship:addEventListener(“tap”, FireLaser)

Ship:addEventListener(“touch”, dragTheShip)

end

– show()

function scene:show( event )

local sceneGroup = self.view

local phase = event.phase

if ( phase == “will” ) then

– Code here runs when the scene is still off screen (but is about to come on screen)

elseif ( phase == “did” ) then

physics.start()

Runtime:addEventListener(“collision”, onCollision)

gameLoopTimer = timer.performWithDelay( 500, gameLoop, 0)

end

end

– hide()

function scene:hide( event )

local sceneGroup = self.view

local phase = event.phase

if ( phase == “will” ) then

– Code here runs when the scene is on screen (but is about to go off screen)

elseif ( phase == “did” ) then

– Code here runs immediately after the scene goes entirely off screen

end

end

– destroy()

function scene:destroy( event )

local sceneGroup = self.view

– Code here runs prior to the removal of scene’s view

end


– Scene event function listeners


scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )


return scene

Source file (working)

local composer = require( “composer” )

local scene = composer.newScene()


– Code outside of the scene event functions below will only be executed ONCE unless

– the scene is removed entirely (not recycled) via “composer.removeScene()”


local physics = require( “physics” )

physics.start()

physics.setGravity( 0, 0 )

– Configure image sheet

local sheetOptions =

{

    frames =

    {

        {   – 1) asteroid 1

            x = 0,

            y = 0,

            width = 102,

            height = 85

        },

        {   – 2) asteroid 2

            x = 0,

            y = 85,

            width = 90,

            height = 83

        },

        {   – 3) asteroid 3

            x = 0,

            y = 168,

            width = 100,

            height = 97

        },

        {   – 4) ship

            x = 0,

            y = 265,

            width = 98,

            height = 79

        },

        {   – 5) laser

            x = 98,

            y = 265,

            width = 14,

            height = 40

        },

    },

}

local objectSheet = graphics.newImageSheet( “gameObjects.png”, sheetOptions )

– Initialize variables

local lives = 3

local score = 0

local died = false

local asteroidsTable = {}

local ship

local gameLoopTimer

local livesText

local scoreText

local backGroup

local mainGroup

local uiGroup

local function updateText()

livesText.text = "Lives: " … lives

scoreText.text = "Score: " … score

end

local function createAsteroid()

local newAsteroid = display.newImageRect( mainGroup, objectSheet, 1, 102, 85 )

table.insert( asteroidsTable, newAsteroid )

physics.addBody( newAsteroid, “dynamic”, { radius=40, bounce=0.8 } )

newAsteroid.myName = “asteroid”

local whereFrom = math.random( 3 )

if ( whereFrom == 1 ) then

– From the left

newAsteroid.x = -60

newAsteroid.y = math.random( 500 )

newAsteroid:setLinearVelocity( math.random( 40,120 ), math.random( 20,60 ) )

elseif ( whereFrom == 2 ) then

– From the top

newAsteroid.x = math.random( display.contentWidth )

newAsteroid.y = -60

newAsteroid:setLinearVelocity( math.random( -40,40 ), math.random( 40,120 ) )

elseif ( whereFrom == 3 ) then

– From the right

newAsteroid.x = display.contentWidth + 60

newAsteroid.y = math.random( 500 )

newAsteroid:setLinearVelocity( math.random( -120,-40 ), math.random( 20,60 ) )

end

newAsteroid:applyTorque( math.random( -6,6 ) )

end

local function fireLaser()

local newLaser = display.newImageRect( mainGroup, objectSheet, 5, 14, 40 )

physics.addBody( newLaser, “dynamic”, { isSensor=true } )

newLaser.isBullet = true

newLaser.myName = “laser”

newLaser.x = ship.x

newLaser.y = ship.y

newLaser:toBack()

transition.to( newLaser, { y=-40, time=500,

onComplete = function() display.remove( newLaser ) end

} )

end

local function dragShip( event )

local ship = event.target

local phase = event.phase

if ( “began” == phase ) then

– Set touch focus on the ship

display.currentStage:setFocus( ship )

– Store initial offset position

ship.touchOffsetX = event.x - ship.x

elseif ( “moved” == phase ) then

– Move the ship to the new touch position

ship.x = event.x - ship.touchOffsetX

elseif ( “ended” == phase or “cancelled” == phase ) then

– Release touch focus on the ship

display.currentStage:setFocus( nil )

end

return true  – Prevents touch propagation to underlying objects

end

local function gameLoop()

– Create new asteroid

createAsteroid()

– Remove asteroids which have drifted off screen

for i = #asteroidsTable, 1, -1 do

local thisAsteroid = asteroidsTable[i]

if ( thisAsteroid.x < -100 or

thisAsteroid.x > display.contentWidth + 100 or

thisAsteroid.y < -100 or

thisAsteroid.y > display.contentHeight + 100 )

then

display.remove( thisAsteroid )

table.remove( asteroidsTable, i )

end

end

end

local function restoreShip()

ship.isBodyActive = false

ship.x = display.contentCenterX

ship.y = display.contentHeight - 100

– Fade in the ship

transition.to( ship, { alpha=1, time=4000,

onComplete = function()

ship.isBodyActive = true

died = false

end

} )

end

local function endGame()

composer.gotoScene( “menu”, { time=800, effect=“crossFade” } )

end

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

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

end

end

end


– Scene event functions


– create()

function scene:create( event )

local sceneGroup = self.view

– Code here runs when the scene is first created but has not yet appeared on screen

physics.pause()  – Temporarily pause the physics engine

– Set up display groups

backGroup = display.newGroup()  – Display group for the background image

sceneGroup:insert( backGroup )  – Insert into the scene’s view group

mainGroup = display.newGroup()  – Display group for the ship, asteroids, lasers, etc.

sceneGroup:insert( mainGroup )  – Insert into the scene’s view group

uiGroup = display.newGroup()    – Display group for UI objects like the score

sceneGroup:insert( uiGroup )    – Insert into the scene’s view group

– Load the background

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”

– Display lives and score

livesText = display.newText( uiGroup, "Lives: " … lives, 200, 80, native.systemFont, 36 )

scoreText = display.newText( uiGroup, "Score: " … score, 400, 80, native.systemFont, 36 )

ship:addEventListener( “tap”, fireLaser )

ship:addEventListener( “touch”, dragShip )

end

– show()

function scene:show( event )

local sceneGroup = self.view

local phase = event.phase

if ( phase == “will” ) then

– Code here runs when the scene is still off screen (but is about to come on screen)

elseif ( phase == “did” ) then

– Code here runs when the scene is entirely on screen

physics.start()

Runtime:addEventListener( “collision”, onCollision )

gameLoopTimer = timer.performWithDelay( 500, gameLoop, 0 )

end

end

– hide()

function scene:hide( event )

local sceneGroup = self.view

local phase = event.phase

if ( phase == “will” ) then

– Code here runs when the scene is on screen (but is about to go off screen)

timer.cancel( gameLoopTimer )

elseif ( phase == “did” ) then

– Code here runs immediately after the scene goes entirely off screen

Runtime:removeEventListener( “collision”, onCollision )

physics.pause()

composer.removeScene( “game” )

end

end

– destroy()

function scene:destroy( event )

local sceneGroup = self.view

– Code here runs prior to the removal of scene’s view

end


– Scene event function listeners


scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )


return scene

Hello and welcome to the Corona Community Forums.

You are likely not going to get a lot of help with your question for two reasons. First, you posted unformatted code. Please use the blue <> button in the row with Bold, Italic, etc. and paste your code into the popup window. It will help people be able to read your code.

Secondly, you should never dump large blocks of code. People won’t take the time to read through that much code.

In a quick scan, I see you changed the name of the ship from “ship” to “Ship”. Lua is case sensitive. There is a good chance you missed one of these when you changed the case of the variable name.

Next, look to see if your text editor has a “diff” function (show the differences between two files). That could help you isolate where your code differs from the working code.

Finally, please take a moment to review this post which contains some great advice on how to ask a great question to get your best shot at great answers:  https://forums.coronalabs.com/topic/55780-ask-a-better-question-get-a-better-answer/

Rob

Thanks Rob Miracle, I will do what You have advised me. Sorry for being “kind of dumb”. Of course the reason was stupid, I am sorry I even asked the question here

Thanks for the answear anyway! It was so fast, that I am still suprised how fast it is. Good job on supporting the community C:

I hope I didn’t take too much time

MightyPancake

You’re not being “kind of dumb”. Many new developers don’t read our pinned posts. Don’t apologize for asking questions. It’s why we are here.

We just want to make sure you have a good experience and get good answers.

Rob

Well, then You are good at it! Corona is a nice tool!

Hello and welcome to the Corona Community Forums.

You are likely not going to get a lot of help with your question for two reasons. First, you posted unformatted code. Please use the blue <> button in the row with Bold, Italic, etc. and paste your code into the popup window. It will help people be able to read your code.

Secondly, you should never dump large blocks of code. People won’t take the time to read through that much code.

In a quick scan, I see you changed the name of the ship from “ship” to “Ship”. Lua is case sensitive. There is a good chance you missed one of these when you changed the case of the variable name.

Next, look to see if your text editor has a “diff” function (show the differences between two files). That could help you isolate where your code differs from the working code.

Finally, please take a moment to review this post which contains some great advice on how to ask a great question to get your best shot at great answers:  https://forums.coronalabs.com/topic/55780-ask-a-better-question-get-a-better-answer/

Rob

Thanks Rob Miracle, I will do what You have advised me. Sorry for being “kind of dumb”. Of course the reason was stupid, I am sorry I even asked the question here

Thanks for the answear anyway! It was so fast, that I am still suprised how fast it is. Good job on supporting the community C:

I hope I didn’t take too much time

MightyPancake

You’re not being “kind of dumb”. Many new developers don’t read our pinned posts. Don’t apologize for asking questions. It’s why we are here.

We just want to make sure you have a good experience and get good answers.

Rob

Well, then You are good at it! Corona is a nice tool!