Can someone please help me shorten my code?

In my game, I’m spawning 18 objects via “timer.performWithDelay” It’s a side scrolling game where you must collide with 15 objects that vary +/- points, and 3 objects as power-ups.

I tried my best making the spawns unclustered but there’s many times where 3-4 objects will spawn at the same time making it clustered and dull gameplay. I’ve looked at just about every forum, table tutorial, and sample code there was and tried making tables many times and ended up confusing myself. I also tried using “enterFrame” but that spawned “object1” about 100 times at the same time. What I’m trying to do is create a table for objects1-15 and have them spawn one at a time. I’ve been stuck on this for a long time, can anyone please help me out?

Timers1- 18 are like this from object1 spawning every 0350 to object18 spawning every 91175 milliseconds

[code]
function object1Spawn()
object1 = display.newImage(“object1.png”)
object1.x = 510
object1.y = math.random(0,280)
localGroup:insert(object1)
transition.to (object1, {time=1500, x=-50})
physics.addBody(object1, {bounce=0.6})
object1.angularVelocity = math.random(-200,200)
object1.myName = “object1”
object1.collision = onobject1Collision
object1:addEventListener(“collision”, object1)
end
timer1 = timer.performWithDelay(0350, object1Spawn, 0 )
[import]uid: 114389 topic_id: 28533 reply_id: 328533[/import]

There are a few things you want to do to shorten your code. Tables are quite easy, let me try explaining in your case to hopefully help you understand better.

objects = {}  
i = 1  
  
function spawnObjects()  
 objects[i] = display.newImage("object1.png")  
 objects[i].x = 510  
 objects[i].y = math.random(0,280)  
 localGroup:insert(objects[i])  
 transition.to (objects[i], {time=1500, x=-50})  
 physics:addBody(objects[i], {bounce=0.6})  
 objects[i].angularVelocity = math.random(-200,200)   
 objects[i].myName = "objects[i]"   
-- you can change myName to be "objects" so that you if you handle your collision by myName they -- all get the same thing  
 objects[i].collision = onobject1Collision  
 objects[i]:addEventListener("collision", objects[i])   
 i = i + 1  
end  
 timer1 = timer.performWithDelay(350, spawnObjects, 0 )   

Try this and let me know how it works. Each time the timer runs it creates an object with the value of i. i starts off as 1 and is increased by 1 each time it runs. So all your objects will be objects[1], objects[2] etc. If you need more explaining feel free to ask

[import]uid: 77199 topic_id: 28533 reply_id: 115077[/import]

Sorry didn’t have time to thoroughly explain like I said I would, so I came back when I had some more time. So the way this works is:

objects = {}  

With this you just created a table! Easy enough. The table is just a bunch of variables separated by comma’s. For example

objects = { 550, 235, 851, 352 }  
--In this table 550 is the first variable in objects, 235 is the second, 851 is the third and 352 is the fourth  
--so we could do this  
test1 = objects[1]  
--test 1 would be equal to 550, because 550 is the first variable in the table objects. the number in the " [] " is what variable in the table you are calling. so for example  
test = objects[3]  
--this would be like you guessed 851   

So now in the code above that I wrote, every 350 milliseconds it will create a new variable in the objects table with the image you want, random y position and everything else you wrote there. This is really good because for more advanced things you can delete things precisely. Say you want the fifth one to be removed you’d be able to call objects[5]:removeself and objects[5] = nil at one point, and it would know exactly which one you are talking about without removing any of the others. Hope this all made more sense. It’s not exactly textbook definition of table, it’s the way I see it and my fail attempt at trying to put it in words. Best of luck [import]uid: 77199 topic_id: 28533 reply_id: 115085[/import]

So with your example table, will all 5 objects spawn by the spawnObjects() function? Or do I still need a separate variables in for each object with [1]-[5] all in the spawnObjects() function since I’d missing the png images?

[code]
objects = { “object1”, “object2”, “object3”, “objcet4”, “object5”}
i = 1
function spawnObjects()
objects[i] = display.newImage(“object1.png”)
objects[i].x = 510
objects[i].y = math.random(0,280)
localGroup:insert(objects[i])
transition.to (objects[i], {time=1500, x=-50})
physics:addBody(objects[i], {bounce=0.6})
objects[i].angularVelocity = math.random(-200,200)
objects[i].myName = “objects[i]”
objects[i].collision = onobject1Collision
objects[i]:addEventListener(“collision”, objects[i])
i = i + 1
end
timer1 = timer.performWithDelay(350, spawnObjects, 0 )
[import]uid: 114389 topic_id: 28533 reply_id: 115188[/import]

You do not have to declare the variables in a table before you make them.

objects = { "object1", "object2", "object3", "objcet4", "object5"}  

When the function spawnObjects runs, it is declaring a new variable in the table and essentially creating it by itself.

This code right here should work I believe:

objects = {}  
i = 1   
function spawnObjects()  
 objects[i] = display.newImage( object[i].png)  
 objects[i].x = 510  
 objects[i].y = math.random(0,280)  
 localGroup:insert(objects[i])  
 transition.to (objects[i], {time=1500, x=-50})  
 physics:addBody(objects[i], {bounce=0.6})  
 objects[i].angularVelocity = math.random(-200,200)   
 objects[i].myName = "object1"   
 objects[i].collision = onobject1Collision  
 objects[i]:addEventListener("collision", objects[i])   
 i = i + 1  
end  
 timer1 = timer.performWithDelay(350, spawnObjects, 5 )   

If you want ONLY 5 variables then you do not have to declare the 5 before hand. Instead you need to make sure the function only runs 5 times. So at the end of timer1 you put a 5 instead of a 0. 0 means it runs forever, while means it will run 5 times.

I’m a bit confused by the question. I hope I understood it right. You want every object to have a different picture? Using my function all 5 objects will spawn, you do not need separate variables or anything but they will all have the same picture. However if you have 5 pictures named object1.png, object2.png, object3.png etc then you can easily get it to work by doing this

objects[i] = display.newImage( object[i].png )  

If there is no image file of the same name you will get an error, so in the timer you will want to add how many times to call the function. You can also make it so it resets the picture back to 1 after it hits 5 so it can run endlessly if you’d want it that way. Hope this answers your question [import]uid: 77199 topic_id: 28533 reply_id: 115249[/import]

Aha! Thank you very much! [import]uid: 114389 topic_id: 28533 reply_id: 115411[/import]

Also transition.to is an asynchronous function meaning it will return and continue processing while the transition is running. This will likely give odd results as you’ll be trying to move your object using physics while transition is trying to do move it somewhere different.

something like

[lua]objects = {}
i = 1
function spawnObjects()
objects[i] = display.newImage( object[i].png)
objects[i].x = 510
objects[i].y = math.random(0,280)
localGroup:insert(objects[i])
transition.to (objects[i], {time=1500, x=-50, oncomplete=addtophysics<.b>})
end

function addtophysics(poEvent)
physics:addBody(objects[i], {bounce=0.6})
objects[i].angularVelocity = math.random(-200,200)
objects[i].myName = “object1”
objects[i].collision = onobject1Collision
objects[i]:addEventListener(“collision”, objects[i])
i = i + 1
end

timer1 = timer.performWithDelay(350, spawnObjects, 5 ) [/lua] [import]uid: 74338 topic_id: 28533 reply_id: 115417[/import] </.b>

here a timer could go off while you’re spawning, especially on slower devices, so a cleaner approach would be to use closures which allows state to be remembered even in event calls

[lua]objects = {}
i = 1
function spawnObjects()
local fnClosure
objects[i] = display.newImage( object[i].png)
objects[i].x = 510
objects[i].y = math.random(0,280)
objects[i].myName = “object1”
localGroup:insert(objects[i])

fnClosure=function(poEvent) addtophysics(objects[i]) end
transition.to (objects[i], {time=1500, x=-50, oncomplete=fnClosure})
i = i + 1
end

function addtophysics(myobject)
physics:addBody(myobject, {bounce=0.6})
myobject.angularVelocity = math.random(-200,200)
myobject.collision = onobject1Collision
myobject:addEventListener(“collision”, myobject)
end

timer1 = timer.performWithDelay(350, spawnObjects, 5 ) [/lua] [import]uid: 74338 topic_id: 28533 reply_id: 115421[/import]