Images, Tables and Arrays. Oh my!!

In my project I was able to set up a for loop to tell some images to go in t1 table of x and y cords…

I followed what was done in this link for reference:

http://developer.anscamobile.com/code/tables-lua-dictionaries-and-arrays

You can see my code here:

[lua]–[[
PROJECT OBJECTIVES

OBJECTIVE 1:

Slash weeds and make them appear to be choped up.

OBJECTIVE 2:

Spreed good seeds to grow

OBJECTIVE 3:

Water plants and watch them grow

]]
display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR
local loqsprite = require(‘loq_sprite’)
math.randomseed(os.time())
math.random()

local _W = display.contentWidth
local _H = display.contentHeight
local objects = {}
local speed = 5
local i
local x0 , y0, x1, x2

local t1 = {

{ x = 650, y = 365 },
{ x = 350, y = 380 },
{ x = 600, y = 480 },
{ x = 400, y = 550 },
{ x = 700, y = 600 },

}

local background = display.newImageRect(“background.png”, 1024, 768)
background.x = _W/2; background.y = _H/2

function tapped(event)
event.target:removeEventListener(“tap”, tapped)
event.target:removeSelf()
objects[event.target] = nil
print(“objects”… tapped)
end
for i = 1,5 do
objects[i] = display.newImageRect(“weed.png”, 200, 150 )
objects[i].x = t1[i].x; objects[i].y = t1[i].y
objects[i]:addEventListener(“tap”, tapped)
end[/lua]

Can anybody show me how to print a statement telling me which objects have been touched… Thanks!!
[import]uid: 51459 topic_id: 16962 reply_id: 316962[/import]

My true issue is that I don’t know how to set an ID for the object

I have been studying this tutorial

http://howto.oz-apps.com/2011/10/bug-catcher-game-tutorial.html

I also tried something like this

objects[object] = object

and it just gave me an error…

Thanks for any help!

[import]uid: 51459 topic_id: 16962 reply_id: 63605[/import]

Ok I figured out how to make a print statement out of values for the objects in table by doing this

[lua] for k,v in ipairs(objects) do
print(k,v)
end[/lua]

Now on to making some IDs

Corona is so much fun! [import]uid: 51459 topic_id: 16962 reply_id: 63613[/import]

Couple of things:

function tapped(event)  
 event.target:removeEventListener("tap", tapped)  
 event.target:removeSelf()  
 objects[event.target] = nil  
 print("objects".. tapped)  
end  

if you tap on the 3rd item, then event.target is the same as objects[3]. So your objects[event.target] = nil is like saying:

objects[objects[3]] = nil

when you really wanted to say:

objects[3] = nil

So what you really want is something like:

function tapped(event)  
 local id = event.target.id  
 event.target:removeEventListener("tap", tapped)  
 event.target:removeSelf()  
 objects[id] = nil  
 print("objects".. tapped)  
end  
  
 for i = 1,5 do  
 objects[i] = display.newImageRect("weed.png", 200, 150 )  
 objects[i].x = t1[i].x; objects[i].y = t1[i].y  
 objects[i].id = i  
 objects[i]:addEventListener("tap", tapped)  
 end  

In your for loop, you have to add a value named “id” that holds the index value of the object (the i value from the loop). Then in the event handler, since your deleting the target, you need to preserve the value of the ID before you delete it which is where the:

local id = event.target.id

comes from.

Then in the last line before the print, I use that “id” to reference the spot in the table that needs nil’ed.
[import]uid: 19626 topic_id: 16962 reply_id: 63622[/import]

Thanks Rob I run some test after class!! But just from a glance it looks like you nailed it like all ways!!! [import]uid: 51459 topic_id: 16962 reply_id: 63632[/import]

I was able to run some tests during class and got this error…

Runtime error
…ZS-a189GNQo1k+++TI/-Tmp-/TemporaryItems/458/main.lua:52: attempt to concatenate global ‘tapped’ (a function value)
stack traceback:
[C]: ?
…ZS-a189GNQo1k+++TI/-Tmp-/TemporaryItems/458/main.lua:52: in function <…zs-a189gnqo1k>
?: in function <?:215>

So I tried to set a local varable on top called tapped but it didn’t work… Do I have my code right?

[lua]display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR
local loqsprite = require(‘loq_sprite’)
math.randomseed(os.time())
math.random()

local _W = display.contentWidth
local _H = display.contentHeight
local objects = {}
local speed = 5
local i
local x0 , y0, x1, x2
local t1 = {

{ x = 650, y = 365 },
{ x = 350, y = 380 },
{ x = 600, y = 480 },
{ x = 400, y = 550 },
{ x = 700, y = 600 },

}

local background = display.newImageRect(“background.png”, 1024, 768)
background.x = _W/2; background.y = _H/2

function tapped(event)
local id = event.target.id
event.target:removeEventListener(“tap”, tapped)
event.target:removeSelf()
objects[id] = nil
print(“object”… tapped)
end
for i = 1,5 do
objects[i] = display.newImageRect(“weed.png”, 200, 150 )
objects[i].x = t1[i].x; objects[i].y = t1[i].y
objects[i].id = i
objects[i]:addEventListener(“tap”, tapped)
end[/lua]

THANKS [import]uid: 51459 topic_id: 16962 reply_id: 63635[/import] </…zs-a189gnqo1k>

main.lua:52: attempt to concatenate global 'tapped' (a function value)

What does the error tell you?

Break it down… Line 52 (which I’m pretty sure is: )

print("object".. tapped)  

Concatenate means to attach two strings together. The string print and the value of the variable tapped (and oh, by the way, “tapped” is global). What type of data does “tapped” contain? Perhaps a “function”? Are you allowed to attach a function to a string?
[import]uid: 19626 topic_id: 16962 reply_id: 63647[/import]

I got it!! Replaced tapped with id and it worked great!! THANKS Rob… Your a true miracle!!! [import]uid: 51459 topic_id: 16962 reply_id: 63658[/import]