Introduction to Corona - Chapter 3

Hi, 

I’m working on chapter 3 - Bringing it to Life, I’m half way down at the Action! part just before Moving the Ship.  I just saved the main.lua and received an error message, I looked over the area of code, but I really don’t see what’s wrong.  I have a snippet.

I appreciate any help, 

Thanks.

Did you do

local physics = require(“physics”)

At the top of the code?

Thanks for responding, 

After main.lua the first 3 lines are: 

local physics = require( “physics” )

physics.start()

physics.setGravity( 0, 0 )

I’m at the Corona page now, I copied everything exactly.  I don’t understand what’s wrong.

Appreciate your help.

I see the problem. It is called scoping you need to put lines 94, 95,96 inside create asteroid

https://youtu.be/2ATlcGP2zMY

I watched the scoping, I moved the lines that I think you meant, and now have a new error.
 
Have another snippet, but I don’t see anything for attachments.
 
 
Thanks.

local function create Asteroid() – another function entered —

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”

end

I received a new error message…attempt to index global ‘newAsteroid’ ( a nil value) stack traceback: main.lua:115: in main chunk.

Anytime you use the variable newAstroid it should be moved to createAstroid function. New astroid only exists in createAstroid, any time you use reference newAstroid outside createAstroid you are going to get an error

newAsteroid is entered 10 times in the if statement.

local whereFrom = math.random( 3 )

if ( whereFrom == 1 ) then

newAsteroid.x = -60

newAsteroid.y = math.random( 500 )

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

elseif ( whereFrom == 2 ) then

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

newAsteroid.y = -60

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

elseif ( whereFrom == 3 ) then

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

Will this entire block have to be moved into the createAsteroid function?  

I didn’t see this written anywhere, is this an error I should report on the website?

Yes,

It says on the site “Like our previous functions, we begin with local function and the name of the function, createAsteroid().” it assumes that you should inside that function.

It is up to you want to report it.

It works!

The error messages were very confusing, I would never have figured this out.  

I really appreciate all the help you have given me.

Thank you.

Did you do

local physics = require(“physics”)

At the top of the code?

Thanks for responding, 

After main.lua the first 3 lines are: 

local physics = require( “physics” )

physics.start()

physics.setGravity( 0, 0 )

I’m at the Corona page now, I copied everything exactly.  I don’t understand what’s wrong.

Appreciate your help.

I see the problem. It is called scoping you need to put lines 94, 95,96 inside create asteroid

https://youtu.be/2ATlcGP2zMY

I watched the scoping, I moved the lines that I think you meant, and now have a new error.
 
Have another snippet, but I don’t see anything for attachments.
 
 
Thanks.

local function create Asteroid() – another function entered —

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”

end

I received a new error message…attempt to index global ‘newAsteroid’ ( a nil value) stack traceback: main.lua:115: in main chunk.

Anytime you use the variable newAstroid it should be moved to createAstroid function. New astroid only exists in createAstroid, any time you use reference newAstroid outside createAstroid you are going to get an error

newAsteroid is entered 10 times in the if statement.

local whereFrom = math.random( 3 )

if ( whereFrom == 1 ) then

newAsteroid.x = -60

newAsteroid.y = math.random( 500 )

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

elseif ( whereFrom == 2 ) then

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

newAsteroid.y = -60

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

elseif ( whereFrom == 3 ) then

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

Will this entire block have to be moved into the createAsteroid function?  

I didn’t see this written anywhere, is this an error I should report on the website?