Questions about Chapter 3 — Bringing it to Life and else

I’m absolutely newbie. I’m looking the code here and I don’t understand how we set how much asteroids to be created.

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

I’m trying to write similar function in my project, for creating multiple, static objects from one image file. My code so far.

local ballTable = {} math.randomseed( os.time() ) local Ball = display.newImageRect( "ball.png", 50, 50 ) local function createBall() local newBall = Ball newBall.x = math.random(display.contentWidth) newBall.y = math.random(display.contentHeight) for i = 1, 30 do table.insert(ballTable, newBall) end end

But the result is that only one Ball appears, in the same place after relaunch and a part of it is outside the screen. What I do wrong? How to create many balls and all of them to be in the visible area?

Corona display objects are just fancy Lua tables and there are a few things to remember about tables in Lua.

For instance,

local table1 = { "a", "b", "c" } local table2 = table1 table.remove( table2, 1 ) print( table1[1], table2[1] ) --\> Output: b, b

If you write code like that, i.e. table2 = table1, or newBall = Ball, then all you are doing is creating a reference to the original table. This means that if you modify either table, then both tables change.

In other words, you are just creating new references to the original Ball display object with your function. If you wish to create new display objects, then you need to use the function call.

local ballTable = {} math.randomseed( os.time() ) local function createBall() local newBall = display.newImageRect( "ball.png", 50, 50 ) -- You could already set the x and y value on the previous row by replacing the 50's there. newBall.x = math.random(display.contentWidth) newBall.y = math.random(display.contentHeight) table.insert( ballTable, newBall ) -- You are making only a single new display object, so there's no sense in adding 30 references of it to the table. -- for i = 1, 30 do -- table.insert(ballTable, newBall) -- end end

Whenever you need a new ball, you’ll just need to call that createBall function. One function call = one new ball.

We have two separate issues going on here:

  1. Ball appearing off the screen - This was in the original code on purpose (I wrote it). The reason is that I wanted the asteroids to drift onto the screen and not just suddenly appear.
    In your code you have the ball showing up at 0 to max screen size. The location of each object is based on the center of the object, so, yes, if the value was close to 0 or the screen dimensions, it would appear partially off the screen.
  2. Only one ball is appearing - This is being caused by the location of your for loop. It should be outside of the function and then call the function to create the new balls.
    -Dr. B