dynamic name + objects with the same name

Hi

I’ve two questions about corona and lua. I’ve looked in all sample files and tried to find an answer on the page but I didn’t find anything :frowning:

How can I create a circle/rectangular/image with a dynamic name
I’ve tried many variations like this one but nothing worked :frowning:
[lua]var = 1
[“myCircle”…var] = display.newCircle( 100, 100, 30)[/lua]
2.
Why is it possible to create two circles with the same name and how can I access them?
[lua]myCircle= display.newCircle( 100, 100, 30)
myCircle= display.newCircle( 100, 200, 30)[/lua] [import]uid: 10820 topic_id: 3478 reply_id: 303478[/import]

Hi ChRu, this worked for my background image loader.

Since I had a variable number of images to stitch together into a panning display group, read in from a file, I needed different variable names. I figured an Array or something like this was the best way to go about it.

--Declare the Array  
local bgArray = {}  

--In Your Loop/Function bgArray[count] = display.newImage(backgroundMain, backgroundImageFileName, x, y, true) [import]uid: 11024 topic_id: 3478 reply_id: 10461[/import]

Bringing this topic from the dead, to keep things tidy! :smiley:

I have made a table with the following things:
[lua]local ball = {}
local ballIndex = 0

local function spawnBall(event)
ball[ballIndex] = display.newImage(“ball.png”)
ball[ballIndex].bonus = 1
–TONS of other parameters…
ballIndex = ballIndex + 1
end[/lua]
This is working beautifully!

Now, I wanted to test, on a function, for whatever members of the table (therefore objects created dynamically) in a certain area of the screen. What’s the best way to do this?

My first thought was to create a FOR with the ballIndex as limit, and test the position of each ball. Problem is these balls are constantly being removed from play, so it feels like it’s stupid. I mean, if ball #200, #206 and #210 are in play, I shouldn’t be testing ball #1, #2, #3

So, what you guys say? Any takes on this??

Thanks in advance!!! [import]uid: 11130 topic_id: 3478 reply_id: 12718[/import]

Hmmmm nobody? :frowning: [import]uid: 11130 topic_id: 3478 reply_id: 12887[/import]

i’ve been asking that here too
http://developer.anscamobile.com/forum/2010/11/22/scrolling-large-non-tiled-world-objects

you need a Spatial Index
http://www.gotoandplay.it/_articles/2005/07/patterns.php?PHPSESSID=cfd1f33d0c627e6cfe96bc80971d6861#spatialindex

SPATIAL INDEX

Intent

Speed searches by position in a Model Database.

Motivation

Almost all games need to search a Model Database quickly by location or region.

A few examples of spatial searches:

* the renderer (see View Pattern) needs to cull objects which can not be seen;
* a trigger (see Trigger) needs to determine if the player has passed over it;
* the UI needs to determine what object was clicked on by the mouse;
* the physics needs to detect collisions;
* a time bomb needs to determine what to damage.

Implementation

There are many implementations of spatial indices, each optimized for the particular kind of game. Without excellent implementations of these indices, whole categories of games could not exist.

Some common examples:

* Binary Space Partition Trees;

* X and Y Hashes with threaded collision resolution;

* Portals which link isolated spatial indices.

Indices must remain synchronized with the associated Model Database. It is common to see the spatial index implemented within the framework of a Model Database.

[import]uid: 6645 topic_id: 3478 reply_id: 12911[/import]

Hi gammabeam,
it is kind of simple. Have a working array of objects on the screen. If an object is placed on the screen add it to the array and when it is removed, remove it from the array. This secondary array will help you optimize than iterating through a million objects (if you have a million objects).

cheers,

Jayant C Varma [import]uid: 3826 topic_id: 3478 reply_id: 13476[/import]

Hi Jayant!

Thanks for the answer!
That’s what I was thinking about, I’m glad to know I was on the right track!
I’m new to programming so I often get stuck because I don’t know if the solution I come up is a good one :smiley:

I’ll try that out and if it works, I’ll post it here! [import]uid: 11130 topic_id: 3478 reply_id: 13920[/import]