0

我正在尝试从 3 个不同的显示对象打印水龙头,但在终端中打印的水龙头不止一个。他们需要自己的特定功能,但目前还不可能。我应该怎么办?我是 Corona 和 LUA 的新手。

-- BOBLER 显示对象

本地运动1 = display.newImage("images/sport1.png")

运动1.id =“我的运动1”

运动1.x = 120

运动1.y = 90

运动1:规模(1,1)

本地 gossip1 = display.newImage("images/gossip1.png")

gossip1.id = "myGossip1"

八卦1.x = 400

八卦1.y = 120

八卦1:规模(1,1)

本地 kultur1 = display.newImage("images/kultur1.png")

kultur1.id = "myKultur1"

文化1.x = 250

文化1.y = 200

文化1:规模(1,1)

局部函数 onSport1Tap(self, event)

打印(self.id ..“被点击了。”)

结尾

本地函数 onGossip1Tap(self, event)

打印(self.id ..“被点击了。”)

结尾

本地函数 onKultur1Tap(self, event)

打印(self.id ..“被点击了。”)

结尾

-- 点击添加事件监听器

sport1.tap = onSport1Tap

运动1:addEventListener(“点击”,运动1)

gossip1.tap = onGossip1Tap

gossip1:addEventListener("点击", gossip1)

kultur1.tap = onKultur1Tap

kultur1:addEventListener("点击", kultur1)

4

2 回答 2

1

I suggest you do the following things:

  1. Use this function for tap/touch events:

    function sport1:touch(e)
        if e.phase == "ended" then
            print(self.id.." was tapped")
        end
    end
    
  2. Use tables to create your objects more effectively, especially if you plan on adding more objects.

Here is an example of how:

local objects = {}

object[ 1 ] = {id = mySport1, x = 120, y = 90} --by the way, the scale is (1,1) on default

object[ 2 ] = {...}

object[ 3 ] = {...}

Then one can easily create the information for all of them using for loops:

for i = 1, #objects do

local object[i].img = display.newImage("images/"..object[i].id..".png")

object[i].img.x, object[i].img.y = object[i].x, object[i].y

local shape = object[i].img

function shape:touch(e)

if e.phase == "ended" then

print(object[i].id.."was tapped")

end

shape:addEventListener("touch")

end

I hope this was not too advanced... It took me a while to understand the potency of tables, but they become very effective when you have a lot of paramaters or objects needed to be created. Regarding the touch function, I have not really worked with tap - I just believe touch is better and simpler to use.

于 2013-05-02T04:36:35.523 回答
0

return true在所有点击功能的末尾,如下所示:

local function onKultur1Tap( self, event )    
  print(self.id .. " was tapped." )
  return  true -- ** just it **
end
于 2015-05-25T04:05:03.437 回答