2013-06-06 59 views
2

我正在嘗試在Lua中製作類似於糖果粉碎的遊戲。下面是代碼:在Lua(Corona SDK)中擁有一個圖像ID

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

你必須把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

我會把你的塊到表,以保持跟蹤他們每個人。但是,爲了回答您的具體問題,Lua中允許您添加任何方法或屬性的對象,所以你可以這樣做:

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

然後在你點擊/觸摸處理器,則「event.target」爲對象,所以你可以說:

print(event.target.gobbldygook)