Search code examples
luacoronasdkcorona-storyboard

How do I simplify to load some files in the corona?


I am new to corona, all the work is from a scratch of others. Let see, I have 3 images that entitled shop1price shop2price shop3price. Now I want it to be simplified as the code below

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

But the button does not appear, I think the wrong is in options[i]. But the problem always is I don't know how the right. I know I can make the code themselves one by one, but it is certainly very tiring. What if I have for example of 100 buttons.

Any help would be appreciated.


Solution

  •  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
    

    Try this , should work fine.