Is there a way to name images in a table so that i can make use of different images accordingly? [import]uid: 99780 topic_id: 18645 reply_id: 318645[/import]
local myTable = {}
myTable["myvacation"] = display.newImage()
myTable["busTrip"] = display.newImage()
Something like that?
[import]uid: 41884 topic_id: 18645 reply_id: 71637[/import]
easily
[lua]local varTable = { image = “img.png”}
varTable[1].image = “different.png”[/lua]
btw, your explanation is pretty vague [import]uid: 16142 topic_id: 18645 reply_id: 71636[/import]
ok.Let me explain in details.I have 5 images in a table .Say for example
local animal={rat.png,mouse.png,elephant.png,cat.png,dog.png}
Now what I want is to give a name to different images.Like
rat.png=animal1
mouse.png=animal2
elephant.png=animal3
cat.png=animal4
dog.png=animal5
So that I can now use the elements of the table by their name.Say for example
if event.target==“animal 1” then
–do something
elseif event.target ==“animal2” then
–do something
etc…So What i asked was how can i give names to the table elements?
Hope you got my point… [import]uid: 99780 topic_id: 18645 reply_id: 71638[/import]
Something like this?
local animal = {}
animal[1] = { name="rat", image="rat.png" }
-- hence
if event.target.name == "rat" then
-- do something
That should work, assuming what you touch is actually animal[1]. (It’s probably not since you’d be touching a display object, though.)
A more realistic example is
[code]
local animal = {}
animal[“rat”] = display.newImage(“rat.png”, stuffgoeshere)
– hence
if event.target == animal[“rat”] then
– do stuff
[/code] [import]uid: 41884 topic_id: 18645 reply_id: 71641[/import]
local arguments ={}
arguments[“atta1”]=display.newImage(“btn1.png”)
arguments[“atta2”]=display.newImage(“btn2.png”)
arguments[“atta3”]=display.newImage(“btn3.png”)
How can i set the x and y co-ordinates for the elements in the tables?Moreover how can i add the touch event listener to the table coz when i am writing the following code it it giving me an error
[lua]attempt to index feild?[/lua]
[lua]arguments:addEventListener(“touch”,onTouch)
[import]uid: 99780 topic_id: 18645 reply_id: 71888[/import]
Please Somebody help me with this table problem?As I am really not comfortable with the TABLES?Please… [import]uid: 99780 topic_id: 18645 reply_id: 71915[/import]
Hey again. Sorry don’t have a lot of time to respond in the afternoon.
You can’t add a “touch” event listener to the table because the table isn’t a display object. You either need to attach to each display object…
arguments["atta1"]:addEventListener("touch",onTouch)
…or add the display objects to a displayGroup and add a listener to that.
X and Y can be easily set like this
arguments["atta1"].x = 100
Note that you don’t have to call your table rows “atta1” if you don’t want to.
myTable[“atta1”] means that it’s always the “atta1” row. So you could also call it myTable[“Fur”] or whatever. It’s text so the order doesn’t matter. This way is handy when you want to refer to something specific, like “pig”, “laptop”, or so on.
Most of the time though people use the standard order method myTable[1], myTable[2], etc. It’s not as descriptive but it allows you to iterate like this:
for i = 1, 3 do
myTable[i].x = 30
myTable[i].y = 50
end
[import]uid: 41884 topic_id: 18645 reply_id: 71918[/import]