Make two objects touchable

Hey,

I gues this is a real newbie question,

but I have the following code:

[lua]
local function createCircle()
[…]
circle = display.newCircle( positionX, positionY, circleRadius )
[…]
end

function circle:touch( event )
if event.phase == “ended” then
scaleCircle(self,scaleUp)
end
return true;
end
circle:addEventListener(“touch”, circle)[/lua]

I cleaned it up a bit, to concentrate on the important things.

My problem right now is: I can touch one circle and scale it. But this works only for one of the circles (I want to create like 3 or 4 of them). And I guess it only works for the last circle which was created.

I guess the main problem here is, that all circles I create with “createCircle()” are named “circle”. So the evenListener only works for the “circle” I created.

Any ideas how I can select the other circles I created?

thank you :slight_smile: [import]uid: 228665 topic_id: 36283 reply_id: 336283[/import]

Hey lornz,

If you want to create multiples of the same object I would recommend using a table to store the objects.

Like so:

local function circleTable[i]:touch( event )  
 if event.phase == "ended" then  
 scaleCircle(self,scaleUp)  
 end  
 return true;  
end  
  
local circleTable = {}  
  
 local function createCircle()  
[...]  
local i = #circleTable+1  
circle = display.newCircle( positionX, positionY, circleRadius )  
circleTable[i] = circle --insert each object into the table  
circleTable[i]:addEventListener("touch", circleTable[i])  
[...]  
end  

I just quickly typed this together. Not too sure if “circleTable[i]” will push through to the touch function. If it doesn’t, just use the event.target within the touch function. [import]uid: 40731 topic_id: 36283 reply_id: 144204[/import]

Hey lornz,

If you want to create multiples of the same object I would recommend using a table to store the objects.

Like so:

local function circleTable[i]:touch( event )  
 if event.phase == "ended" then  
 scaleCircle(self,scaleUp)  
 end  
 return true;  
end  
  
local circleTable = {}  
  
 local function createCircle()  
[...]  
local i = #circleTable+1  
circle = display.newCircle( positionX, positionY, circleRadius )  
circleTable[i] = circle --insert each object into the table  
circleTable[i]:addEventListener("touch", circleTable[i])  
[...]  
end  

I just quickly typed this together. Not too sure if “circleTable[i]” will push through to the touch function. If it doesn’t, just use the event.target within the touch function. [import]uid: 40731 topic_id: 36283 reply_id: 144204[/import]

Hey lornz,

If you want to create multiples of the same object I would recommend using a table to store the objects.

Like so:

local function circleTable[i]:touch( event )  
 if event.phase == "ended" then  
 scaleCircle(self,scaleUp)  
 end  
 return true;  
end  
  
local circleTable = {}  
  
 local function createCircle()  
[...]  
local i = #circleTable+1  
circle = display.newCircle( positionX, positionY, circleRadius )  
circleTable[i] = circle --insert each object into the table  
circleTable[i]:addEventListener("touch", circleTable[i])  
[...]  
end  

I just quickly typed this together. Not too sure if “circleTable[i]” will push through to the touch function. If it doesn’t, just use the event.target within the touch function. [import]uid: 40731 topic_id: 36283 reply_id: 144204[/import]

Hey lornz,

If you want to create multiples of the same object I would recommend using a table to store the objects.

Like so:

local function circleTable[i]:touch( event )  
 if event.phase == "ended" then  
 scaleCircle(self,scaleUp)  
 end  
 return true;  
end  
  
local circleTable = {}  
  
 local function createCircle()  
[...]  
local i = #circleTable+1  
circle = display.newCircle( positionX, positionY, circleRadius )  
circleTable[i] = circle --insert each object into the table  
circleTable[i]:addEventListener("touch", circleTable[i])  
[...]  
end  

I just quickly typed this together. Not too sure if “circleTable[i]” will push through to the touch function. If it doesn’t, just use the event.target within the touch function. [import]uid: 40731 topic_id: 36283 reply_id: 144204[/import]