StarExplorer Error

Hello guys.I am new to here and I am going through the getting started Guide.It is fun but at this moment I’m stuck with a problem and I cannot find the solution.I am trying to implement the StarExplorer tutorial and when I get to the point to load the Asteroids on the screen for the first time I get the error

ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ‘:’

                    stack traceback:

[C]: in function ‘addBody’

here is my code, implemented line by line from the tutorial.This is very frustrating :huh:

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 )

end

  table.insert( asteroidsTable, newAsteroid )

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

what am I doing wrong?

thank you in advance, 

S

                    

                    

The full error message should have a line number. You should be able to turn on line numbering in your text editor to determine what line of code that’s being reported. It would be really helpful if you could copy/paste the whole error from the console log window that opens with the simulator and let us know which line of code is causing the issue.

Also in the future, it’s really helpful to use code formatting either type in: 

[lua] ...paste your code here... [/lua]

or use the blue <> button in the edit bar (with Bold, Italic, etc.) and paste the code into the window that pops up.

Rob

I’m struggling with the same issue.The error is in this line :
<lua>

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

</lua>

The error is the same for me - 

<lua>

 C:\Users\Acer\Documents\Corona Projects\StarExplorer\main.lua:95: ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ‘:’

13:04:48.774  stack traceback:

13:04:48.774   [C]: in function ‘addBody’

13:04:48.774   C:\Users\Acer\Documents\Corona Projects\StarExplorer\main.lua:95: in main chunk

</lua>

Thanks in advance

mount4o

This error is likely due to an issue a couple of lines up. I’m guessing you’re in Chapter 3 at this point. Corona opens up a console log window behind the simulator where your “print” statements, warnings and errors get displayed. Can you look in that log and see if there are any warnings being issues above the error?

This specific error is saying that the first thing physics.addBody() expects to see is a table. Objects in Lua are done using tables and if this line (two lines above it) was successful: 

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

then newAsteroid would be a table. If it failed for some reason, it would be nil and would trigger the error you’re seeing.  That line of code is assuming a successful creation of a graphics.newImageSheet() around line 46 or so (depending on what code and blank lines you may have included or left out).  You’re looking for this line:

local objectSheet = graphics.newImageSheet( "gameObjects.png", options )

This this could fail silently or with a warning. If it can’t find “gameObjects.png” (file names are case sensitive!) or if the values in the options table declared above it don’t match the actual layout in the image sheet you’re trying to load.

First step: Look through that console log for other error and warning messages!

Second step: Make sure that “gameObjects.png” is in the same folder with main.lua and that it’s name matches the string you’re using exactly (including upper and lower case letters).

Rob

I couldn’t find anything above the error. It just says 

21:46:51.619 ERROR: Runtime error

The rest is loading directories and copyright.

The sprite sheet is in the folder with the same name in the code.The following lines seem correct to me 

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

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

If it is needed I’ll post my whole code. I just can’t seem to figure it out. Is there a problem with the fact that it is in a function ? Do I have to call the function to create the object and that to be causing the problem (no object - no table to add the physics body to) ?

Hello guys and thanks for your replies.

enterm, it seems that you are facing the same issue as I did.The solution is very simple and I guess it is a common mistake that newbies like us do at this point when they are trying to implement the Star Explorer.You need to include the rest of the code related to the asteroid creation inside the createAsteroid() function.

so your createAsteroid() function should look like this

[lua]

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

[/lua]

Rob thank you for your reply and your remarks on how to post code in here!

Thank you , stefan. That was kinda logical but , oh, well - newbie things. But I had trouble trying to make my code work. I copy-pasted your code and it all worked out fine. But when I do it with my hand-written code (which is exactly the same as yours I think I checked it 15 times repeatedly) it does not work. The same goes when i finished the whole game - with my code the game doesn’t even detect asteroids colliding with the ship. But with the project code that was provided at the end of the tutorial it works just fine. And this gets me confused.

Edit: It was a syntax error in my code. For the collisions problem M mean. For the first problem it was probably that as well but i got frustrated and went ahead so it’s too late to solve it.

The full error message should have a line number. You should be able to turn on line numbering in your text editor to determine what line of code that’s being reported. It would be really helpful if you could copy/paste the whole error from the console log window that opens with the simulator and let us know which line of code is causing the issue.

Also in the future, it’s really helpful to use code formatting either type in: 

[lua] ...paste your code here... [/lua]

or use the blue <> button in the edit bar (with Bold, Italic, etc.) and paste the code into the window that pops up.

Rob

I’m struggling with the same issue.The error is in this line :
<lua>

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

</lua>

The error is the same for me - 

<lua>

 C:\Users\Acer\Documents\Corona Projects\StarExplorer\main.lua:95: ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ‘:’

13:04:48.774  stack traceback:

13:04:48.774   [C]: in function ‘addBody’

13:04:48.774   C:\Users\Acer\Documents\Corona Projects\StarExplorer\main.lua:95: in main chunk

</lua>

Thanks in advance

mount4o

This error is likely due to an issue a couple of lines up. I’m guessing you’re in Chapter 3 at this point. Corona opens up a console log window behind the simulator where your “print” statements, warnings and errors get displayed. Can you look in that log and see if there are any warnings being issues above the error?

This specific error is saying that the first thing physics.addBody() expects to see is a table. Objects in Lua are done using tables and if this line (two lines above it) was successful: 

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

then newAsteroid would be a table. If it failed for some reason, it would be nil and would trigger the error you’re seeing.  That line of code is assuming a successful creation of a graphics.newImageSheet() around line 46 or so (depending on what code and blank lines you may have included or left out).  You’re looking for this line:

local objectSheet = graphics.newImageSheet( "gameObjects.png", options )

This this could fail silently or with a warning. If it can’t find “gameObjects.png” (file names are case sensitive!) or if the values in the options table declared above it don’t match the actual layout in the image sheet you’re trying to load.

First step: Look through that console log for other error and warning messages!

Second step: Make sure that “gameObjects.png” is in the same folder with main.lua and that it’s name matches the string you’re using exactly (including upper and lower case letters).

Rob

I couldn’t find anything above the error. It just says 

21:46:51.619 ERROR: Runtime error

The rest is loading directories and copyright.

The sprite sheet is in the folder with the same name in the code.The following lines seem correct to me 

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

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

If it is needed I’ll post my whole code. I just can’t seem to figure it out. Is there a problem with the fact that it is in a function ? Do I have to call the function to create the object and that to be causing the problem (no object - no table to add the physics body to) ?

Hello guys and thanks for your replies.

enterm, it seems that you are facing the same issue as I did.The solution is very simple and I guess it is a common mistake that newbies like us do at this point when they are trying to implement the Star Explorer.You need to include the rest of the code related to the asteroid creation inside the createAsteroid() function.

so your createAsteroid() function should look like this

[lua]

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

[/lua]

Rob thank you for your reply and your remarks on how to post code in here!

Thank you , stefan. That was kinda logical but , oh, well - newbie things. But I had trouble trying to make my code work. I copy-pasted your code and it all worked out fine. But when I do it with my hand-written code (which is exactly the same as yours I think I checked it 15 times repeatedly) it does not work. The same goes when i finished the whole game - with my code the game doesn’t even detect asteroids colliding with the ship. But with the project code that was provided at the end of the tutorial it works just fine. And this gets me confused.

Edit: It was a syntax error in my code. For the collisions problem M mean. For the first problem it was probably that as well but i got frustrated and went ahead so it’s too late to solve it.