使使用功能创建的两个对象(LUA,Corona)可触摸 [英] Make two objects touchable which were created with a function (LUA, Corona)

查看:90
本文介绍了使使用功能创建的两个对象(LUA,Corona)可触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我猜这是一个真正的新手问题,

I gues this is a real newbie question,

但是我有以下代码:

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.

我现在的问题是:我可以触摸一个圆圈并缩放它.但这仅适用于其中一个圈子(我想创建其中的3个或4个圈子).而且我猜它仅适用于创建的最后一个圆.

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.

我想这里的主要问题是,我用"createCircle()"创建的所有圆都被命名为"circle".因此,evenListener仅适用于我创建的圆圈".

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?

谢谢:)

推荐答案

这是我解决的方法:

local function createCircle()
  --[[ MORE CODE ]]--
   table.insert(circleTable, display.newCircle( positionX, positionY, circleRadius ) )
   --[[ MORE CODE ]]--
end

function onObjectTouch(event)
   local self = event.target
   if event.phase == "ended" then
        --[[ MORE CODE ]]--
   end
   return true;
end

local function addTouchListeners()
   for _, circle in ipairs(circleTable) do
      circle:addEventListener("touch", onObjectTouch)
   end
end

createCircle()
addTouchListeners()

我想Dream Eaters解决方案也应该可以工作.但是在调用我的createCircle()函数时,我还有另一个错误.我通过为TouchListeners创建一个函数并在createCircle()函数之后调用它来解决了这个问题.

I guess Dream Eaters solution should work as well. But I had another mistake in calling my createCircle() function. I solved this with creating a function for the TouchListeners and call it AFTER the createCircle() function.

希望这可以帮助其他有类似问题的人.

Hope this helps other people with similar problems.

这篇关于使使用功能创建的两个对象(LUA,Corona)可触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆