我如何简化在Corona中加载一些文件?

我是初学 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 个按钮怎么办。

任何帮助都将不胜感激。

点赞
用户2653067
用户2653067
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

options 数组中的三个字符串作为按钮的默认文件,创建了 priceTag 数组中的三个按钮。然后分别设置按钮的 id 和事件处理函数,以便在触摸按钮时调用 onTouchBuy 函数。最后添加到按钮组中并设置缩放比率为 (0.8, 0.8)

2015-05-07 05:32:01