D, I don’t use Graphics 2, this is for Graphics 1, but it should give you an idea how to spawn the objects with each different color object doing it’s own function.
[lua]
local Table = {}
local ArrowGroup = display.newGroup()
local ButtonTarget
local function spawnArrows()
local function onEventListener(event)
if event.phase == “began” then
ButtonTarget = event.target --The ButtonTarget will now take on all the attributes of the touched object
ButtonTarget.xScale = 1.2
ButtonTarget.yScale = 1.2
elseif event.phase == “moved” then
ButtonTarget.xScale = 1
ButtonTarget.yScale = 1
elseif event.phase == “ended” then
ButtonTarget.xScale = 1
ButtonTarget.yScale = 1
if ButtonTarget.Function == 1 then
Table[ButtonTarget.ID].isVisible = false --make the object disappear or do something
print("ButtonTarget.Function == ", ButtonTarget.Function)
print("ButtonTarget.ID == ", ButtonTarget.ID)
elseif ButtonTarget.Function == 2 then
Table[ButtonTarget.ID].isVisible = false
print("ButtonTarget.Function == ", ButtonTarget.Function)
print("ButtonTarget.ID == ", ButtonTarget.ID)
elseif ButtonTarget.Function == 3 then
Table[ButtonTarget.ID].isVisible = false
print("ButtonTarget.Function == ", ButtonTarget.Function)
print("ButtonTarget.ID == ", ButtonTarget.ID)
end
end
return true
end
display.remove(ArrowGroup)
ArrowGroup = nil
ArrowGroup = display.newGroup()
local Count = 0
Table = {}
for i = 1, 3 do --spawn 3 different object sizes
if i == 1 then
for k = 1, 3 do --spawn 3 of these objects at random locations
Count = Count + 1
Table[Count] = display.newRoundedRect( 0, 0, 20, 20, 5)
Table[Count]:setReferencePoint(display.TopLeftReferencePoint)
Table[Count].x = math.random(1, 300)
Table[Count].y = math.random(1, 460)
Table[Count].strokeWidth = 3
Table[Count]:setStrokeColor(79, 148, 205)
Table[Count]:setFillColor(0, 255, 0)
Table[Count].ID = Count --This will ID the object so it can be tracked and do something, be removed, explode or move for instance
Table[Count].Function = 1 – This will identify the objects function that will be read in the onEventListener function
Table[Count]:addEventListener(“touch”, onEventListener)
ArrowGroup:insert(Table[Count])
end
elseif i == 2 then
for k = 1, 5 do --spawn 5 of these objects
Count = Count + 1
Table[Count] = display.newRoundedRect( 0, 0, 30, 45, 5)
Table[Count]:setReferencePoint(display.TopLeftReferencePoint)
Table[Count].x = math.random(1, 290)
Table[Count].y = math.random(1, 450)
Table[Count].strokeWidth = 3
Table[Count]:setStrokeColor(79, 148, 205)
Table[Count]:setFillColor(225, 0, 0)
Table[Count].ID = Count
Table[Count].Function = 2
Table[Count]:addEventListener(“touch”, onEventListener)
ArrowGroup:insert(Table[Count])
end
elseif i == 3 then
for k = 1, 2 do --spawn 2 of these items
Count = Count + 1
Table[Count] = display.newRoundedRect( 0, 0, 40, 40, 5)
Table[Count]:setReferencePoint(display.TopLeftReferencePoint)
Table[Count].x = math.random(1, 280)
Table[Count].y = math.random(1, 440)
Table[Count].strokeWidth = 3
Table[Count]:setStrokeColor(79, 148, 205)
Table[Count]:setFillColor(0, 0, 255)
Table[Count].ID = Count
Table[Count].Function = 2
Table[Count]:addEventListener(“touch”, onEventListener)
ArrowGroup:insert(Table[Count])
end
end
end
return ArrowGroup
end
spawnArrows() --call the function
[/lua]
Hope this helps,
Nail