[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

## Questions about Chapter 3 — Bringing it to Life and else Started by djemche Mar 03 2020 04:11 AM

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

### djemche

[GLOBAL: userInfoPane.html]
djemche
• Observer

• 3 posts
• Corona SDK

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

[TOPIC: post.html]
#2

### XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
• Corona Geek

• 1,246 posts
• Corona SDK

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.

• sporkfin and pixec like this

[topic_controls]

[/topic_controls]