best way to position multiple objects

Hi,
*EDITED FOR CLARITY*

If we can imagine positioning a row of objects across the screen.
The objects should transition to a certain x,y then transition on a timer after however many seconds.

My point is I have a single object, I want multiples of the object to transition to certain x,y co-ords .

I have a table of x,y values.

here is my code.

How would I spawn the object at the points defined in the x,y tables?
I can do it randomly. I am sure there is a simple solution, if I remove the math.random obviously, but what in its place?

[code]
local xTable = {150,200,250,300,350}
local yTable ={100,100,100,100,100}

function enemySpawn()
local sheet = graphics.newImageSheet( “images/enemyring.png”, { width=24, height=30, numFrames=10 } )
local enemy = display.newSprite( sheet, { name=“enemy”, start=1, count=9, time=1000 } )
xpoint = xTable[math.random(#xTable)]
ypoint = yTable[math.random(#yTable)]

enemy.x = xpoint
enemy.y = ypoint

enemy:play()

transition.from(enemy, { delay = 0, y = enemy.y -150,})

end
enemySpawnTimer = timer.performWithDelay(100, enemySpawn, 5)
[/code] [import]uid: 127675 topic_id: 32165 reply_id: 332165[/import]

Hi @bubblebobble,
This is relatively simple. When you execute a timer, the destination function can receive the count (iteration) of that timer… that would be event.count (of course, be sure that you add “event” in the parentheses after enemySpawn).

Armed with the count, you then pick the proper x/y by index number from xTable and yTable, like this (within the spawn function):

local count = event.count  
local posX = xTable[count]  
local posY = yTable[count]  
enemy.x = posX  
enemy.y = posY  

That should do it!

Also, very important! Don’t create your image sheet within the spawn function! Create it above and outside somewhere… it is meant to be declared just once and accessed many times.

Brent Sorrentino [import]uid: 9747 topic_id: 32165 reply_id: 128070[/import]

@Brent,

thank you for your reply.
What makes these replies so important is that you have taken time to explain the code; this makes it easy to understand. This is a much better approach to just giving someone the code.
Thanks also for the tip on creating the image sheet outside the function, I did read that yesterday but forget where.
Your a credit to the community.

BB [import]uid: 127675 topic_id: 32165 reply_id: 128135[/import]

Hi @bubblebobble,
This is relatively simple. When you execute a timer, the destination function can receive the count (iteration) of that timer… that would be event.count (of course, be sure that you add “event” in the parentheses after enemySpawn).

Armed with the count, you then pick the proper x/y by index number from xTable and yTable, like this (within the spawn function):

local count = event.count  
local posX = xTable[count]  
local posY = yTable[count]  
enemy.x = posX  
enemy.y = posY  

That should do it!

Also, very important! Don’t create your image sheet within the spawn function! Create it above and outside somewhere… it is meant to be declared just once and accessed many times.

Brent Sorrentino [import]uid: 9747 topic_id: 32165 reply_id: 128070[/import]

@Brent,

thank you for your reply.
What makes these replies so important is that you have taken time to explain the code; this makes it easy to understand. This is a much better approach to just giving someone the code.
Thanks also for the tip on creating the image sheet outside the function, I did read that yesterday but forget where.
Your a credit to the community.

BB [import]uid: 127675 topic_id: 32165 reply_id: 128135[/import]