Hello, I am an absolute beginner to Corona and Lua and I am going through the getting started developer guide Chapter 3. I have gotten to a point where the guide wants me to save my code and re launch the simulator and when I do I am confronted with the following error:
ERROR: Runtime error
main.lua:104: ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ‘:’
stack traceback:
[C]: in function ‘addBody’
main.lua:104: in main chunk
I have proof read the code, deleted all the code and started over from scratch, deleted just that line of code (which resulted in further error), and even deleted it all and copied and pasted all of the code from the guide itself.
Can someone please tell me what is going wrong, I have tried everything I can think of to fix it and all I want to do is continue to the next part of the guide so I can learn more about using Corona and Lua.
Thank you so much.
The code:
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
–part2
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 } )
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 ) )
local function fireLaser()
local newLaser = display.newImageRect( mainGroup, objectSheet, 5, 14, 40 )
physics.addBody( newLaser, “dynamic”, { isSensor=true } )
newLaser.isBullet = true
end
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
} )
ship:addEventListener( “tap”, fireLaser )