我如何简化地将一些文件加载到电晕中



我是Corona的新手,所有工作都是从其他的划痕中。让我看看,我有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个按钮,该怎么办。

任何帮助将不胜感激。

 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

尝试一下,应该正常工作。

最新更新