# Want to use Widget events with a for loop

**URL:** <https://forums.solar2d.com/t/want-to-use-widget-events-with-a-for-loop/324475>\
**Category:** Lua Language\
**Created:** [April 11, 2014, 12:52pm UTC](https://forums.solar2d.com/t/want-to-use-widget-events-with-a-for-loop/324475 "2014-04-11T12:52:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![russm305](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@russm305](https://forums.solar2d.com/u/russm305)\
**Post date:** [April 11, 2014, 12:52pm UTC](https://forums.solar2d.com/t/want-to-use-widget-events-with-a-for-loop/324475/1 "2014-04-11T12:52:09Z")

</div>

Never Mind figured it out by myself 🙂 &nbsp;!!!

I have 80 widget buttons that will do the same thing based on their number. &nbsp;I am trying to reduce the amount of code I am writing so a tried a loop like so. &nbsp;So when&nbsp;onEvent1 or&nbsp;onEvent2 &nbsp;I am getting error iWARNING: Failed to find image(NumbersB/"…i…".png)

The file I use is ‘NumbersB/1’, ‘NumbersB/2’,’ NumbersB/3’,etc

&nbsp;[lua]

local theOwner = \_G

for i = 1,4 do

&nbsp; theOwner[“onEvent”…i]= function (event)

&nbsp; &nbsp; if event.phase == “ended” then

&nbsp; &nbsp; &nbsp; &nbsp; if theOwner[“flag”…i] == false then

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;theOwner[“B”…i] = display.newImageRect(‘NumbersB/"…i…".png’,50,50)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;theOwner[“B”…i].x= &nbsp;theOwner[“myButton”…i].x

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;theOwner[“B”…i].y= &nbsp;theOwner[“myButton”…i].y

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;screenGroup:insert(theOwner[“B”…i])

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print (‘theOwner[“flag”…i] on’)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;theOwneWidget[“flag”…i] =true

&nbsp; &nbsp; &nbsp; elseif theOwner[“flag”…i] == true then&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display.remove(theOwner[“B”…i])

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; theOwner[“B”…i] = nil

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; theOwner[“flag”…i] = false

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print (‘theOwner[“flag”…i] off’)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp;end

&nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

end

&nbsp; &nbsp; myButton1 = widget.newButton{

&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;defaultFile = “NumbersA/1.png”,

&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;left = w/2,

&nbsp; &nbsp; &nbsp; &nbsp; top = h/2,

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;width =w1, height = h1,

&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onEvent = onEvent1

&nbsp; &nbsp; }

&nbsp;&nbsp;&nbsp;&nbsp;screenGroup:insert(myButton1)

&nbsp; &nbsp; &nbsp;myButton2 = widget.newButton{

&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;defaultFile = “NumbersA/2.png”,

&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;left = w/2+70,

&nbsp; &nbsp; &nbsp; &nbsp; top = h/2,

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;width =w1, height = h1,

&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onEvent = onEvent2

&nbsp; &nbsp; }

&nbsp;&nbsp;&nbsp;&nbsp;screenGroup:insert(myButton2)

&nbsp; &nbsp; &nbsp;myButton3 = widget.newButton{

&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;defaultFile = “NumbersA/3.png”,

&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;left = w/2+140,

&nbsp; &nbsp; &nbsp; &nbsp; top = h/2,

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;width =w1, height = h1,

&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onEvent = onEvent3

&nbsp; &nbsp; }

&nbsp;&nbsp;&nbsp;&nbsp;screenGroup:insert(myButton3)

&nbsp; &nbsp; &nbsp;myButton4 = widget.newButton{

&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;defaultFile = “NumbersA/4.png”,

&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;left = w/2+210,

&nbsp; &nbsp; &nbsp; &nbsp; top = h/2,

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;width =w1, height = h1,

&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onEvent = onEvent4

&nbsp; &nbsp; }

&nbsp;&nbsp;&nbsp;&nbsp;screenGroup:insert(myButton4)

[/lua]
