拥有Lua中的图像ID(Corona SDK)

时间:2013-06-06 00:30:45

标签: lua corona

我正在尝试制作类似于Lua中的糖果粉碎的游戏。这是代码:

local images = {
    "images/beer.png",
    "images/beef.png",
    "images/canned_food.png",
    "images/cup_ice_cream.png",
    "images/french_fries.png",
    "images/pepper.png"
}



local rowcount = 8
local colcount = 4

local blockWidth = display.contentWidth / (colcount*4)
local blockHeight = display.contentWidth / (rowcount*2)

local row
local col
local pan = 3
for row = 1, rowcount do
    for col = 1, colcount do
        local x = (col - 1) * blockWidth + pan
        local y = (row + 1) * blockHeight + pan
        local block = display.newImage(images[math.random(1, 6)], x, y)
        block:addEventListener("touch", blockTouch)
    end
end

我需要知道哪个图像正在移动,要知道他们是否在新的位置上排成3行。

所以我的问题是,如何知道用户在表格中移动哪个图像的ID或标识符?

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

您必须在您创建的每个对象中添加ID,例如:

local function getID(event)
   t = event.target
   print(t.id)
end


local beef = display.newImage("images/beef.png",)
beef.id = "beef"

local canned_food= display.newImage("images/canned_foods.png",)
canned_food.id = "cannedfoods"

local fries = display.newImage("images/fench_fries.png",)
fries.id = "fries"

beef:addEventListener("tap", getID())
canned_food:addEventListener("tap", getID())
fries:addEventLister("tap", getID())

希望这有助于:)

答案 1 :(得分:1)

我会将您的块放入表中以跟踪每个块。但是为了回答您的具体问题,Lua允许您向对象添加任何方法或属性,因此您可以这样做:

 block.name = "Beer"
 block.color = "Green"
 block.gobbldygook = 400

然后在您的点击/触摸处理程序中,您的“event.target”是对象,因此您可以说:

 print(event.target.gobbldygook)