Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make two objects touchable which were created with a function (LUA, Corona)

I gues this is a real newbie question,

but I have the following code:

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)

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 :)

like image 281
lornz Avatar asked Aug 16 '26 15:08

lornz


1 Answers

You MUST use tables. For eg.:

circles = {}
local function createCircle()
  --[[ MORE CODE ]]--
  table.insert( circles, display.newCircle( positionX, positionY, circleRadius ) )
  --[[ MORE CODE ]]--
end
function circle:touch( event )
   if event.phase == "ended" then
      scaleCircle(self,scaleUp)
   end
   return true;
end
for _, circle in ipairs( circles ) do
  circle:addEventListener("touch", circle)
end
like image 119
hjpotter92 Avatar answered Aug 18 '26 05:08

hjpotter92



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!