如何简化加载日冕中的某些文件?

时间:2015-05-06 22:22:03

标签: lua corona corona-storyboard

我是新来的电晕,所有的工作都是从别人的角度出发。让我们看看,我有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 个答案:

答案 0 :(得分: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

试试这个,应该可以正常工作。