Globa; and local problem with "isVisible"

I would like to write a simple program that hides a rectangle when a button is pressed.

This is my code:

[lua]

local widget = require( “widget” )

local function handleButtonEvent( event )

myRect.isVisible = false

end

local button1 = widget.newButton

{

    left = 100,

    top = 200,

    id = “button1”,

    label = “Button”,

    onEvent = handleButtonEvent

}

local myRect = display.newRect( 0, 0, 150, 50, 12 )

myRect.isVisible = true

[/lua]

It doesn’t work because my function can’t see the local “myRect”. I can get it working by making myRect a global but I understand that is not recommended. Is there a better solution using just locals?

Try putting…

[lua]local myRect = display.newRect( 0, 0, 150, 50, 12 )
myRect.isVisible = true[/lua]

…before the “handleButtonEvent” method.

That worked! Thanks!

Try putting…

[lua]local myRect = display.newRect( 0, 0, 150, 50, 12 )
myRect.isVisible = true[/lua]

…before the “handleButtonEvent” method.

That worked! Thanks!