dynamic references to objects - syntax help required

Trying to simplify my code and tried to replace the following

  
allBallsGroup:insert( ball1 )  
allBallsGroup:insert( ball2 )  
allBallsGroup:insert( ball3 )  
allBallsGroup:insert( ball4 )  
allBallsGroup:insert( ball5 )  
allBallsGroup:insert( ball6 )  
allBallsGroup:insert( ball7 )  
allBallsGroup:insert( ball8 )  
allBallsGroup:insert( ball9 )  
allBallsGroup:insert( ball10)  
  

With this:

  
for i = 1, 10 do  
  
 allBallsGroup:insert( "ball" .. i)  
  
end  
  

And also tried the following:

  
for i = 1, 10 do  
  
 allBallsGroup:insert( \_G["ball" .. i])  
  
end  
  

Unfortunately neither worked so could do with some help.

I got the error

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

[import]uid: 7863 topic_id: 2750 reply_id: 302750[/import]

It depends on the scope of ‘ball’. _G is only used for global variables.

I would change these lines:

 local ball1 = display.newImage(..)  
 local ball2 = display.newImage(..)  
..  
etc  

to

 local ball\_table = {}  
 ball\_table[1] = display.newImage(..)  
 ball\_table[2] = displya.newImage(..)  

and then

for i = 1, 10 do  
  
 allBallsGroup:insert( ball\_table[i])  
  
end  

[import]uid: 8541 topic_id: 2750 reply_id: 8262[/import]

Thanks sanchan91 - this looks great though I am having problems making it work with my implementation.

My ball is made up of 2 layers so I have amended my code based on yours as follows:

  
local ballTable = {}  
  
---------  
-- Ball 1  
---------  
  
ballTable[1] = display.newGroup()  
  
local ball1Layer1 = display.newImage("ballLayer1.png")  
local ball1Layer2 = display.newImage("ballLayer2.png")  
  
ballTable[1]:insert( ball1Layer1 )  
ballTable[1]:insert( ball1Layer2 )  
  
-- ------  
-- Ball 2  
-- ------  
  
ballTable[2] = display.newGroup()  
  
local ball2Layer1 = display.newImage("ballLayer1.png")  
local ball2Layer2 = display.newImage("ballLayer2.png")  
  
ballTable[2]:insert( ball2Layer1 )  
ballTable[2]:insert( ball2Layer2 )  
  
etc...  
for i = 1, 10 do  
  
 allBallsGroup:insert( ballTable[i] )  
  
end  
  
for i = 1, 10 do  
  
 allBallsGroup[i].x = i + 30  
 allBallsGroup[i].y = i + 120  
  
end  
  

I am not getting any error however I am not getting any balls displayed either so I suspect there is something not quite right.

Any ideas?

Thanks

Paul [import]uid: 7863 topic_id: 2750 reply_id: 8265[/import]

Actually, the top part can be simplified to:

local ballTable = {}  
  
---------  
-- Ball 1 .. n  
-- If you dont need to reference ball1Layer1..x  
-- elsewhere in the code, you really dont even need  
-- ball1Layer1..x local variables, since you can   
-- assign the group to add the new object to directly in the  
-- display.newImage() call.  
---------  
   
for i = 1, 10 do  
ballTable[i] = display.newGroup()  
   
 local ball1Layer1 = display.newImage(ballTable[i],"ballLayer1.png")  
 local ball1Layer2 = display.newImage(ballTable[i],"ballLayer2.png")  
allBallsGroup:insert( ballTable[i] )  
end  

Now, this next part, Im not quite sure of. I dont know if moving the group actually moves the objects inside of it:

for i = 1, 10 do  
   
 allBallsGroup[i].x = i + 30  
 allBallsGroup[i].y = i + 120  
   
end  

You may have do put the x,y coordinates on ball1Layer1 and ball1Layer2 up above where you declair ball1Layer1…x:

ball1Layer1.x = i + 30  
ball1Layer2.x = i + 30  
ball1Layer1.y = i + 120  
ball1Layer2.y = i + 120  

or you can put the coords directly in the newImage call:

local ball1Layer1 = display.newImage(ballTable[i],"ballLayer1.png", i+30, i + 120 )  
local ball1Layer2 = display.newImage(ballTable[i],"ballLayer2.png", i+30, i + 120 )  

Anyway, as for them not showing up, how is allBallsGroup defined? Is it perhaps isVisible = false? Does it belong to a group that perhaps is isVisible = false?

To test to see if the groups are invisible or something, remove the part where you actually add the display.newImage() to a group:

--Not assigning these display.newImages to a group,  
--so they should show up on the top of the screen,   
--unless there is something else rendering on top of   
--them later in the code.  
  
for i = 1, 10 do  
--ballTable[i] = display.newGroup()  
   
 local ball1Layer1 = display.newImage("ballLayer1.png",50,50)  
 local ball1Layer2 = display.newImage("ballLayer2.png",50,50)  
--allBallsGroup:insert( ballTable[i] )  
end  

[import]uid: 8541 topic_id: 2750 reply_id: 8270[/import]

Love the slim line code but unfortunately still not displaying anything.

I declare allBallsGroup as follows:

  
local allBallsGroup = display.newGroup()  
  

And then the other code as follows:

  
local ballTable = {}  
for i = 1, 10 do  
  
 ballTable[i] = display.newGroup()  
   
 local ball1Layer1 = display.newImage(ballTable[i],"ballLayer1.png")  
 local ball1Layer2 = display.newImage(ballTable[i],"ballLayer2.png")  
  
  
 allBallsGroup:insert( ballTable[i] )  
end  
for i = 1, 10 do  
   
 allBallsGroup[i].x = i \* 30  
 allBallsGroup[i].y = 120  
   
end  
  

I have previously moved groups fine so the above code to position them should be fine.

Hmm any ideas? [import]uid: 7863 topic_id: 2750 reply_id: 8289[/import]

Huh, where did you got that syntax from?

[lua]display.newImage(ballTable[i],“ballLayer1.png”)[/lua] [import]uid: 5712 topic_id: 2750 reply_id: 8294[/import]

From the previous post by sanchan91.

Hope you can help me crack this Mike - been trying to sort it all afternoon:) [import]uid: 7863 topic_id: 2750 reply_id: 8296[/import]

I was surprised that an image showed up on the screen. the syntax is:
[lua]display.newImage( filename [, baseDirectory] [, left, top] )[/lua]

so Corona might used the baseDirectory parameter for the filename.

Let’s look at your code

[lua]–Create a group
local allBallsGroup = display.newGroup()

–Here you create a table, so fine so good. It’s empty
local ballTable = {}

–Now lets loop 10 times
for i = 1, 10 do
–Each table entry will now be a group. Useness???
ballTable[i] = display.newGroup()
–That syntax is wrong to my best guess. what do you want to do?
local ball1Layer1 = display.newImage(ballTable[i],“ballLayer1.png”)
local ball1Layer2 = display.newImage(ballTable[i],“ballLayer2.png”)

–Now you insert the former created group (which is empty?)
allBallsGroup:insert( ballTable[i] )
end

–Loop 10 times
for i = 1, 10 do
–you set the X/y property each children group (ballTable[i])
allBallsGroup[i].x = i * 30
allBallsGroup[i].y = 120

end[/lua]

The funny thing is that it works somehow, but I coudl swear it should not. [import]uid: 5712 topic_id: 2750 reply_id: 8297[/import]

Found that parentGroup syntax hidden away in the reference:
http://developer.anscamobile.com/content/display-objects#display.newImage_

display.newImage( [parentGroup,] filename [, baseDirectory] [, x, y] [,isFullResolution] )
Hrm… cant say I see anything wrong with that code. I guess try this to see if you see any balls at all on the screen.

local ballTable = {}  
   
   
for i = 1, 10 do  
   
  
 local ball1Layer1 = display.newImage("ballLayer1.png",(i\*10)+50,(i\*10)+50)  
 local ball1Layer2 = display.newImage("ballLayer2.png",(i\*10)+50,(i\*10)+50)  
  
end  

Anything in the logs?

[import]uid: 8541 topic_id: 2750 reply_id: 8299[/import]

What was the problem again, I lost you somewhere? [import]uid: 5712 topic_id: 2750 reply_id: 8300[/import]

Thanks Mike

Essentially my game has varying numbers of balls for each level with the number decreasing per level.

So at the outset I want to create the maximum number of balls so I can use these for each level by populating the allBallsGroup group.

My code was really lengthy but worked - hence the desire to make it more dynamic and slimline. Unfortunately I don’t seem to be able to concatenate to create references. [import]uid: 7863 topic_id: 2750 reply_id: 8302[/import]

Me too, I see something on the screen.

Thanks Sanchan91, you never stop learning. Cool feature I think. [import]uid: 5712 topic_id: 2750 reply_id: 8301[/import]

I use this maximum approach with my particles too. At the beginning I load a maximum of particle images into a group. They have a custom property called .isActive. At the beginning it is set to false. Also they are invisible.
When I span a particle, I set isActive and isVisible to true and then react inside my game logic only on particles which are active. [import]uid: 5712 topic_id: 2750 reply_id: 8303[/import]

Sorry, there seems to be some conflict with my code. Your code works fine when standalone. I will work though it to identify the conflict [import]uid: 7863 topic_id: 2750 reply_id: 8305[/import]

Doh I feel really dumb! My game background image was being displayed after the balls so it was on top - oops! [import]uid: 7863 topic_id: 2750 reply_id: 8308[/import]

I thought of using an active state as an alternative to creating a list of active balls for each level and figured it would be one extra if statement (one of many!) so decided to try a solution without this. Having said that I may need an active state anyway still to prevent collision detections of balls out of the game area. [import]uid: 7863 topic_id: 2750 reply_id: 8310[/import]