2015-05-06 64 views
0

我是新來的電暈,所有的工作都是從別人的抓起。讓我看看,我有3張圖片,標題爲shop1price shop2price shop3price。現在,我希望它被簡化爲下面的代碼如何簡化在日冕中加載一些文件?

local options = 
{ 
    { defaultFile = 'images/shop1price.png' }, 
    { defaultFile = 'images/shop2price.png' }, 
    { defaultFile = 'images/shop3price.png' }, 
} 

local priceTag = {} 
for i = 1,3 do 
    priceTag[i] = widget.newButton{ 
     options[i], 
     overColor = {128,128,128,255}, 
     width = 73, 
     height = 38, 
     left = (centerX-155) + (i-1)*118, 
     top = centerY * 0.88, 
     id = i, 
     onEvent = function (e) 
      if e.phase == 'ended' then 
       onTouchBuy(e.target.id) 
      end 
      return true 
     end 
    } 
    -- priceTag[i] : setReferencePoint(display.CenterReferencePoint) 
    priceTag[i] : scale(0.8 , 0.8) 
    buttonGroup : insert(priceTag[i]) 
end 

但按鈕沒有出現,我認爲錯在options[i]。但問題總是我不知道怎樣纔是正確的。我知道我可以一個接一個地編碼,但肯定很累。如果我有例如100個按鈕怎麼辦?

任何幫助,將不勝感激。

回答

1
local options = {} 

    [#options+1] = 'images/shop1price.png' 
    [#options+1] = 'images/shop2price.png' 
    [#options+1] = 'images/shop3price.png' 


local priceTag = {} 
for i = 1,#options do 
    priceTag[i] = widget.newButton{ 
     defaultFile = options[i], 
     overColor = {128,128,128,255}, 
     width = 73, 
     height = 38, 
     left = (centerX-155) + (i-1)*118, 
     top = centerY*0.88, 
     id = i, 
     onEvent = function (e) 
     if e.phase == 'ended' then 
      onTouchBuy(e.target.id) 
     end 
     return true 
    end 
    } 
    -- priceTag[i] : setReferencePoint(display.CenterReferencePoint) 
    priceTag[i] : scale(0.8 , 0.8) 
    buttonGroup : insert(priceTag[i]) 
end 

試試這個,應該可以正常工作。

+0

Thanks Kumar :) –