Code template

Hello,

Is it possible to create a code template for reuse?

I have lots of  buttons on my app that all basically look the same except for the test, so I tried to create a template like below:

btnTemplate = widget.NewButton()…

and then reuse it as

btnNext = btnTemplate

btnNext.id = “btnNext”…

You get the idea… but this doesn’t seem to work. 

Anyone knows a way of doing this?

Thanks.

Hi there,

you try something like a prototype object system.

That won’t work in Corona cause you cannot copy display objects (widgets are display objects too).

With your btnNext = btnTemplate you only set a refference to the already existing button.

I would try something like the code below. It contains a function which creates a new button and gives him all the functions and information you want.

local function create\_btn(id)    local button = widget.NewButton()    button.id = id      function button:touch(event)    end      button:addEventListener("touch")      return button end   btn1 = create\_btn(1) btn2 = create\_btn(2)

This is a really simple example, but you can add more functions and parameters as you like.

Thank you this is a really good idea. I’ll try that.

Hi there,

you try something like a prototype object system.

That won’t work in Corona cause you cannot copy display objects (widgets are display objects too).

With your btnNext = btnTemplate you only set a refference to the already existing button.

I would try something like the code below. It contains a function which creates a new button and gives him all the functions and information you want.

local function create\_btn(id)    local button = widget.NewButton()    button.id = id      function button:touch(event)    end      button:addEventListener("touch")      return button end   btn1 = create\_btn(1) btn2 = create\_btn(2)

This is a really simple example, but you can add more functions and parameters as you like.

Thank you this is a really good idea. I’ll try that.