starexplorer

Someone, please help me. My asteroids and laser are not displaying on the simulator. I can’t seem to figure out why it’s not displaying.

[lua]


– main.lua


– Your code here

local physics = require( “physics” )

physics.start()

physics.setGravity( 0, 0 )

– Seed the random number generator

math.randomseed( os.time())

– 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

– Set up display groups

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

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

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

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

– Hide the status bar

display.setStatusBar( display.HiddenStatusBar )

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

[/lua]

Thanks for formatting that code post (you rock), but in this case you should just zip up your entire project and share a link to it on dropbox (or other well known sharing system).

The problem could be in the code and/or assets.  

It will be far easier and faster for folks to see the entire thing on their own machines.

I think your code works fine but you don’t use it. Nowhere in your code you call  createAsteroid or fireLaser function:)

If you add  on the end of the main.lua file function call

createAsteroid()

you will see that your code works (new asteroid appear). I think you have incomplete code of game Start Explorer.  Complete code you can find below. I don’t remember from which chapter I took it.

----------------------------------------------------------------------------------------- -- -- main.lua -- -- Title: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Star Explorer - a space shooter game. -- Description: &nbsp; &nbsp; Maneuver your starship through an asteroid field, destroying -- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;asteroids as you progress. -- Controls: &nbsp; &nbsp; &nbsp; &nbsp;Tap on ship to fire. Drag to move left/right. -- Sounds: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Shooting/explosion sounds. -- Source: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;https://docs.coronalabs.com/guide/programming/02/index.html ----------------------------------------------------------------------------------------- -- Include physics (space so gravity = (0, 0)) local physics = require("physics") physics.start() physics.setGravity( 0, 0 ) --physics.setDrawMode( "hybrid" ) -- Seed the random number generator math.randomseed(os.time()) -- Load the image sheets local sheetOptions = { &nbsp; &nbsp; &nbsp;frames = &nbsp; &nbsp; &nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp;--1 Asteroid 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x = 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;y = 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;width = 102, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;height = 85 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp;--2 Asteroid 2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x = 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;y = 85, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;width = 90, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;height = 83 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp;--3 Asteroid 3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x = 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;y = 168, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;width = 100, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;height = 97 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp;--4 Ship &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x = 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;y = 265, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;width = 98, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;height = 79 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp;--5 Laser &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x = 98, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;y = 265, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;width = 14, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;height = 40 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, &nbsp; &nbsp; &nbsp;} } local objectSheet = graphics.newImageSheet( "gameObjects.png", sheetOptions ) -- Initialize variables local lives = 3 local score = 0 local died = false -- Empty table initialized local asteroidsTable = {} local ship local gameLoopTimer local livesText local scoreText -- Set up display groups local backGroup = display.newGroup() &nbsp; &nbsp;-- Display group for background image local mainGroup = display.newGroup() &nbsp; &nbsp;-- Display group for the main images (ships, asteroids) local uiGroup = display.newGroup() &nbsp; &nbsp; &nbsp;-- Display group for the user interface -- Load the background local background = display.newImageRect( backGroup, "background.png", 800, 1400 ) background.x = display.contentCenterX background.y = display.contentCenterY -- Load the ship 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" -- Load the UI livesText = display.newText(uiGroup, "Lives: " .. lives, 200, 80, native.systemFont, 36) scoreText = display.newText( uiGroup, "Score: " .. score, 400, 80, native.systemFont, 36 ) -- Hide the status bar display.setStatusBar( display.HiddenStatusBar ) -- Function to update text local function updateText() &nbsp; &nbsp; &nbsp;livesText.text = "Lives: " .. lives &nbsp; &nbsp; &nbsp;scoreText.text = "Score: " .. score end -- Function to create asteroids local function createAsteroid() &nbsp; &nbsp; &nbsp;local newAsteroid = display.newImageRect(mainGroup, objectSheet, 1, 102, 85) &nbsp; &nbsp; &nbsp;-- To keep track of the asteroids insert into a table &nbsp; &nbsp; &nbsp;table.insert(asteroidsTable, newAsteroid) &nbsp; &nbsp; &nbsp;physics.addBody( newAsteroid, "dynamic", {radius=40, bounce=0.8} ) &nbsp; &nbsp; &nbsp;newAsteroid.myName = "asteroid" &nbsp; &nbsp; &nbsp;-- where to create the asteroid logic &nbsp; &nbsp; &nbsp;local whereFrom = math.random(3) &nbsp; &nbsp; &nbsp;if (whereFrom == 1) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- From the left and anywhere between y = 0-\>500 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newAsteroid.x = -60 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newAsteroid.y = math.random(500) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newAsteroid:setLinearVelocity(math.random(40, 120), math.random(20, 60)) &nbsp; &nbsp; &nbsp;elseif (whereFrom == 2) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newAsteroid.x = math.random(display.contentWidth) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newAsteroid.y = -60 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newAsteroid:setLinearVelocity(math.random(-40, 40), math.random(40, 120)) &nbsp; &nbsp; &nbsp;elseif (whereFrom == 3) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newAsteroid.x = display.contentWidth + 60 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newAsteroid.y = math.random(500) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newAsteroid:setLinearVelocity(math.random(-120, -40), math.random(20, 60)) &nbsp; &nbsp; &nbsp;end &nbsp; &nbsp; &nbsp;-- Add torque to the asteroid image &nbsp; &nbsp; &nbsp;newAsteroid:applyTorque(math.random(-6,6)) end -- Function to shoot lasers local function fireLaser() &nbsp; &nbsp; &nbsp;local newLaser = display.newImageRect( mainGroup, objectSheet, 5, 14, 40 ) &nbsp; &nbsp; &nbsp;physics.addBody( newLaser, "dynamic", { isSensor=true} ) &nbsp; &nbsp; &nbsp;newLaser.isBullet = true &nbsp; &nbsp; &nbsp;newLaser.myName = "laser" &nbsp; &nbsp; &nbsp;newLaser.x = ship.x &nbsp; &nbsp; &nbsp;newLaser.y = ship.y &nbsp; &nbsp; &nbsp;newLaser:toBack() &nbsp; &nbsp; &nbsp;transition.to(newLaser, {y = -40, time=500, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onComplete = function() display.remove(newLaser) end &nbsp; &nbsp; &nbsp;}) end -- Add event to shoot laser ship:addEventListener("tap", fireLaser) -- Function to move the ship local function dragShip(event) &nbsp; &nbsp; &nbsp;-- ship var is the thing that was 'touched' &nbsp; &nbsp; &nbsp;local ship = event.target &nbsp; &nbsp; &nbsp;-- phase var for the 4 phases of the touch event "began", "moved", "ended", "cancelled" &nbsp; &nbsp; &nbsp;local phase = event.phase &nbsp; &nbsp; &nbsp;if ("began" == phase) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Set touch focus on the ship &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display.currentStage:setFocus(ship) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Store initial offset position &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ship.touchOffsetX = event.x - ship.x &nbsp; &nbsp; &nbsp;elseif ("moved" == phase) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Move the ship to the new touch position &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Using the offset value produces a smooth dragging effect &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --if (ship.x \< display.contentWidth-50 and ship.x \> 50) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ship.x = event.x - ship.touchOffsetX &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --end &nbsp; &nbsp; &nbsp;elseif ("ended" == phase or "cancelled" == phase) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Release touch focus from ship &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display.currentStage:setFocus(nil) &nbsp; &nbsp; &nbsp;end &nbsp; &nbsp; &nbsp;return true -- Prevents touch propagation to underlying objects end -- Add event to drag ship ship:addEventListener("touch", dragShip) -- Game loop function local function gameLoop() &nbsp; &nbsp; &nbsp;-- Create new asteroid &nbsp; &nbsp; &nbsp;createAsteroid() &nbsp; &nbsp; &nbsp;-- Remove asteroids that have drifted off the screen &nbsp; &nbsp; &nbsp;for i = #asteroidsTable, 1, -1 do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local thisAsteroid = asteroidsTable[i] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( thisAsteroid.x \< -100 or &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;thisAsteroid.x \> display.contentWidth + 100 or &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;thisAsteroid.y \< -100 or &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;thisAsteroid.y \> display.contentHeight + 100) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;display.remove(thisAsteroid) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;table.remove(asteroidsTable, i) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp;end end gameLoopTimer = timer.performWithDelay( 500, gameLoop, 0 ) -- Function to restore ship after death local function restoreShip() &nbsp; &nbsp; &nbsp;-- set body active to false so it does not interact with asteroids on fade-in &nbsp; &nbsp; &nbsp;ship.isBodyActive = false &nbsp; &nbsp; &nbsp;-- Position and set the velocity of the ship &nbsp; &nbsp; &nbsp;ship:setLinearVelocity(0, 0) &nbsp; &nbsp; &nbsp;ship.x = display.contentCenterX &nbsp; &nbsp; &nbsp;ship.y = display.contentHeight-100 &nbsp; &nbsp; &nbsp;-- Fade in the ship &nbsp; &nbsp; &nbsp;-- Alpha 0-\>1, 4000ms, onComplete turn body active, set died to false &nbsp; &nbsp; &nbsp;transition.to(ship, {alpha=1, time=4000, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onComplete = function() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ship.isBodyActive = true &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;died = false &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print("set died to false") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp;}) end local function onCollision( event ) &nbsp; &nbsp; &nbsp;--print("onCollision called") &nbsp; &nbsp; &nbsp;if (event.phase == "began") then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local obj1 = event.object1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local obj2 = event.object2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("onCollision called " .. obj1.myName .. " " .. obj2.myName .. " " .. tostring(died)) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Handle the laser / asteroid collision &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( (obj1.myName == "laser" and obj2.myName == "asteroid") or &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(obj1.myName == "asteroid" and obj2.myName == "laser") ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- Remove both the laser and asteroid &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;display.remove(obj1) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;display.remove(obj2) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for i = #asteroidsTable, 1, -1 do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (asteroidsTable[i] == obj1 or asteroidsTable[i] == obj2) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;table.remove(asteroidsTable, i) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- Increase score &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;score = score + 100 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scoreText.text = "Score: " .. score &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Handle the ship / asteroid collision &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif ( &nbsp;(obj1.myName == "ship" and obj2.myName == "asteroid") or &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (obj1.myName == "asteroid" and obj2.myName == "ship") ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print("inside death loop") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (died == false) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; died = true &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Update lives &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lives = lives - 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; livesText.text = "Lives: " .. lives &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (lives == 0) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;display.remove(ship) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ship.alpha = 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;timer.performWithDelay( 1000, restoreShip ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp;end end Runtime:addEventListener("collision", onCollision)

Thanks for formatting that code post (you rock), but in this case you should just zip up your entire project and share a link to it on dropbox (or other well known sharing system).

The problem could be in the code and/or assets.  

It will be far easier and faster for folks to see the entire thing on their own machines.

I think your code works fine but you don’t use it. Nowhere in your code you call  createAsteroid or fireLaser function:)

If you add  on the end of the main.lua file function call

createAsteroid()

you will see that your code works (new asteroid appear). I think you have incomplete code of game Start Explorer.  Complete code you can find below. I don’t remember from which chapter I took it.

----------------------------------------------------------------------------------------- -- -- main.lua -- -- Title: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Star Explorer - a space shooter game. -- Description: &nbsp; &nbsp; Maneuver your starship through an asteroid field, destroying -- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;asteroids as you progress. -- Controls: &nbsp; &nbsp; &nbsp; &nbsp;Tap on ship to fire. Drag to move left/right. -- Sounds: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Shooting/explosion sounds. -- Source: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;https://docs.coronalabs.com/guide/programming/02/index.html ----------------------------------------------------------------------------------------- -- Include physics (space so gravity = (0, 0)) local physics = require("physics") physics.start() physics.setGravity( 0, 0 ) --physics.setDrawMode( "hybrid" ) -- Seed the random number generator math.randomseed(os.time()) -- Load the image sheets local sheetOptions = { &nbsp; &nbsp; &nbsp;frames = &nbsp; &nbsp; &nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp;--1 Asteroid 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x = 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;y = 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;width = 102, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;height = 85 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp;--2 Asteroid 2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x = 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;y = 85, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;width = 90, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;height = 83 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp;--3 Asteroid 3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x = 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;y = 168, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;width = 100, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;height = 97 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp;--4 Ship &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x = 0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;y = 265, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;width = 98, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;height = 79 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp;--5 Laser &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x = 98, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;y = 265, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;width = 14, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;height = 40 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, &nbsp; &nbsp; &nbsp;} } local objectSheet = graphics.newImageSheet( "gameObjects.png", sheetOptions ) -- Initialize variables local lives = 3 local score = 0 local died = false -- Empty table initialized local asteroidsTable = {} local ship local gameLoopTimer local livesText local scoreText -- Set up display groups local backGroup = display.newGroup() &nbsp; &nbsp;-- Display group for background image local mainGroup = display.newGroup() &nbsp; &nbsp;-- Display group for the main images (ships, asteroids) local uiGroup = display.newGroup() &nbsp; &nbsp; &nbsp;-- Display group for the user interface -- Load the background local background = display.newImageRect( backGroup, "background.png", 800, 1400 ) background.x = display.contentCenterX background.y = display.contentCenterY -- Load the ship 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" -- Load the UI livesText = display.newText(uiGroup, "Lives: " .. lives, 200, 80, native.systemFont, 36) scoreText = display.newText( uiGroup, "Score: " .. score, 400, 80, native.systemFont, 36 ) -- Hide the status bar display.setStatusBar( display.HiddenStatusBar ) -- Function to update text local function updateText() &nbsp; &nbsp; &nbsp;livesText.text = "Lives: " .. lives &nbsp; &nbsp; &nbsp;scoreText.text = "Score: " .. score end -- Function to create asteroids local function createAsteroid() &nbsp; &nbsp; &nbsp;local newAsteroid = display.newImageRect(mainGroup, objectSheet, 1, 102, 85) &nbsp; &nbsp; &nbsp;-- To keep track of the asteroids insert into a table &nbsp; &nbsp; &nbsp;table.insert(asteroidsTable, newAsteroid) &nbsp; &nbsp; &nbsp;physics.addBody( newAsteroid, "dynamic", {radius=40, bounce=0.8} ) &nbsp; &nbsp; &nbsp;newAsteroid.myName = "asteroid" &nbsp; &nbsp; &nbsp;-- where to create the asteroid logic &nbsp; &nbsp; &nbsp;local whereFrom = math.random(3) &nbsp; &nbsp; &nbsp;if (whereFrom == 1) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- From the left and anywhere between y = 0-\>500 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newAsteroid.x = -60 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newAsteroid.y = math.random(500) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newAsteroid:setLinearVelocity(math.random(40, 120), math.random(20, 60)) &nbsp; &nbsp; &nbsp;elseif (whereFrom == 2) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newAsteroid.x = math.random(display.contentWidth) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newAsteroid.y = -60 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newAsteroid:setLinearVelocity(math.random(-40, 40), math.random(40, 120)) &nbsp; &nbsp; &nbsp;elseif (whereFrom == 3) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newAsteroid.x = display.contentWidth + 60 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newAsteroid.y = math.random(500) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newAsteroid:setLinearVelocity(math.random(-120, -40), math.random(20, 60)) &nbsp; &nbsp; &nbsp;end &nbsp; &nbsp; &nbsp;-- Add torque to the asteroid image &nbsp; &nbsp; &nbsp;newAsteroid:applyTorque(math.random(-6,6)) end -- Function to shoot lasers local function fireLaser() &nbsp; &nbsp; &nbsp;local newLaser = display.newImageRect( mainGroup, objectSheet, 5, 14, 40 ) &nbsp; &nbsp; &nbsp;physics.addBody( newLaser, "dynamic", { isSensor=true} ) &nbsp; &nbsp; &nbsp;newLaser.isBullet = true &nbsp; &nbsp; &nbsp;newLaser.myName = "laser" &nbsp; &nbsp; &nbsp;newLaser.x = ship.x &nbsp; &nbsp; &nbsp;newLaser.y = ship.y &nbsp; &nbsp; &nbsp;newLaser:toBack() &nbsp; &nbsp; &nbsp;transition.to(newLaser, {y = -40, time=500, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onComplete = function() display.remove(newLaser) end &nbsp; &nbsp; &nbsp;}) end -- Add event to shoot laser ship:addEventListener("tap", fireLaser) -- Function to move the ship local function dragShip(event) &nbsp; &nbsp; &nbsp;-- ship var is the thing that was 'touched' &nbsp; &nbsp; &nbsp;local ship = event.target &nbsp; &nbsp; &nbsp;-- phase var for the 4 phases of the touch event "began", "moved", "ended", "cancelled" &nbsp; &nbsp; &nbsp;local phase = event.phase &nbsp; &nbsp; &nbsp;if ("began" == phase) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Set touch focus on the ship &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display.currentStage:setFocus(ship) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Store initial offset position &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ship.touchOffsetX = event.x - ship.x &nbsp; &nbsp; &nbsp;elseif ("moved" == phase) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Move the ship to the new touch position &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Using the offset value produces a smooth dragging effect &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --if (ship.x \< display.contentWidth-50 and ship.x \> 50) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ship.x = event.x - ship.touchOffsetX &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --end &nbsp; &nbsp; &nbsp;elseif ("ended" == phase or "cancelled" == phase) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Release touch focus from ship &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display.currentStage:setFocus(nil) &nbsp; &nbsp; &nbsp;end &nbsp; &nbsp; &nbsp;return true -- Prevents touch propagation to underlying objects end -- Add event to drag ship ship:addEventListener("touch", dragShip) -- Game loop function local function gameLoop() &nbsp; &nbsp; &nbsp;-- Create new asteroid &nbsp; &nbsp; &nbsp;createAsteroid() &nbsp; &nbsp; &nbsp;-- Remove asteroids that have drifted off the screen &nbsp; &nbsp; &nbsp;for i = #asteroidsTable, 1, -1 do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local thisAsteroid = asteroidsTable[i] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( thisAsteroid.x \< -100 or &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;thisAsteroid.x \> display.contentWidth + 100 or &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;thisAsteroid.y \< -100 or &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;thisAsteroid.y \> display.contentHeight + 100) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;display.remove(thisAsteroid) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;table.remove(asteroidsTable, i) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp;end end gameLoopTimer = timer.performWithDelay( 500, gameLoop, 0 ) -- Function to restore ship after death local function restoreShip() &nbsp; &nbsp; &nbsp;-- set body active to false so it does not interact with asteroids on fade-in &nbsp; &nbsp; &nbsp;ship.isBodyActive = false &nbsp; &nbsp; &nbsp;-- Position and set the velocity of the ship &nbsp; &nbsp; &nbsp;ship:setLinearVelocity(0, 0) &nbsp; &nbsp; &nbsp;ship.x = display.contentCenterX &nbsp; &nbsp; &nbsp;ship.y = display.contentHeight-100 &nbsp; &nbsp; &nbsp;-- Fade in the ship &nbsp; &nbsp; &nbsp;-- Alpha 0-\>1, 4000ms, onComplete turn body active, set died to false &nbsp; &nbsp; &nbsp;transition.to(ship, {alpha=1, time=4000, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onComplete = function() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ship.isBodyActive = true &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;died = false &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print("set died to false") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp;}) end local function onCollision( event ) &nbsp; &nbsp; &nbsp;--print("onCollision called") &nbsp; &nbsp; &nbsp;if (event.phase == "began") then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local obj1 = event.object1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local obj2 = event.object2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("onCollision called " .. obj1.myName .. " " .. obj2.myName .. " " .. tostring(died)) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Handle the laser / asteroid collision &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( (obj1.myName == "laser" and obj2.myName == "asteroid") or &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(obj1.myName == "asteroid" and obj2.myName == "laser") ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- Remove both the laser and asteroid &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;display.remove(obj1) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;display.remove(obj2) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for i = #asteroidsTable, 1, -1 do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (asteroidsTable[i] == obj1 or asteroidsTable[i] == obj2) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;table.remove(asteroidsTable, i) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- Increase score &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;score = score + 100 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;scoreText.text = "Score: " .. score &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Handle the ship / asteroid collision &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif ( &nbsp;(obj1.myName == "ship" and obj2.myName == "asteroid") or &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (obj1.myName == "asteroid" and obj2.myName == "ship") ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print("inside death loop") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (died == false) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; died = true &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Update lives &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lives = lives - 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; livesText.text = "Lives: " .. lives &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (lives == 0) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;display.remove(ship) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ship.alpha = 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;timer.performWithDelay( 1000, restoreShip ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp;end end Runtime:addEventListener("collision", onCollision)