Hide and show a button by a variable

Hi everyone

i have this code

display.setStatusBar( display.HiddenStatusBar )  
local ui = require("ui");  
  
local myvar = 1;  
  
local touch = function( event )  
  
 if event.phase == "release" then  
  
 myvar = 1 -myvar  
  
  
 end  
end   
local button = ui.newButton{  
 defaultSrc = "face.png",  
 defaultX = 50,  
 defaultY = 50,  
 overSrc = "face.png",  
 overX = 30,  
 overY = 30,  
 onEvent = touch  
}  
  
button.x = 100;  
button.y = 100;  
if myvar == 1 then  
settings\_button.isVisible = false;  
elseif myvar == 0 then  
settings\_button.isVisible = false;  
end  

i need to show and hide the button that i touched, and i am not getting this results.
anyone can help? [import]uid: 26056 topic_id: 18665 reply_id: 318665[/import]

if visi == false then
button.isVisible = true;
elseif visi == true then
button.isVisible = false;
end

you dont put your code like this, it executes just once and you have no access to it anymore

learningcorona.com, read a tut about function scopes [import]uid: 16142 topic_id: 18665 reply_id: 71722[/import]

To toggle a variable between on and off you can do

[code]

local myVar = 1

–Toggle between 1 and 0
myVar = 1 -myVar
[/code] [import]uid: 84637 topic_id: 18665 reply_id: 71728[/import]

Thanks cool Danny, good to know [import]uid: 16142 topic_id: 18665 reply_id: 71729[/import]

it dont work danny :confused: i updated the code, it some like it that you are talking about? [import]uid: 26056 topic_id: 18665 reply_id: 71739[/import]

It seems to me that your if/else statement that handles setting the display objects isVisible variable should be in your touch event handler. [import]uid: 100558 topic_id: 18665 reply_id: 71868[/import]

it dont work inside the touch function, because the button is declared after the function event, and the settings.isVisible need to stay after the button declared, anyone know a idea to show and hide the object? i am some confused [import]uid: 26056 topic_id: 18665 reply_id: 72059[/import]

up [import]uid: 26056 topic_id: 18665 reply_id: 72486[/import]

showing and hiding button upon touch
if you want button to stay invisible just declare it as obj.isVisible = false inside touch function and it will stay invisible till you restart it
[lua]local obj = display.newRect(0,0,50,50)
obj.isHitTestable = true

local function onTouch(event)
if event.phase == “ended” then
obj.isVisible = not obj.isVisible
end
end

obj:addEventListener(“touch”, onTouch)[/lua]

if you want to listen for var outside touch function then you need to write enterFrame function for it like:

[lua]local function check()
if var == 1 then
obj.isVisible = true
else
obj.isVisible = false
end
end

Runtime:addEventListener(“enterFrame”, check)[/lua]

but i would not recommend this approach, i like to stay away from runtime functions) [import]uid: 16142 topic_id: 18665 reply_id: 72496[/import]

thanks for this code, i know it, but on my code i use button ui and the function is first than the button, and i cant put the obj.isVisible inside the function, because "obj.isVisible is after of the button, my problem is using the ui button how i can hide and show the button by touching on this button.

p.s i dont use runtime functions :slight_smile: [import]uid: 26056 topic_id: 18665 reply_id: 72514[/import]

you need to use forward declarations to deal with buttons, that was created after the functions

like:
[lua]local button
local touch[/lua]

at the start of the lua file

and you declare then your image and function:
[lua]function touch()

button = display.newRect(0,0,50,50)[/lua]

and once again, you need to change your visibility in touch function) [import]uid: 16142 topic_id: 18665 reply_id: 72516[/import]

yes you are right but it is if i use a normal image, i declare it on the start of lua file and the function “where i want” but with the ui buttons i declare the button and the function only works if stay up of the button image and the object.isVisible stay after the button declaration, some like:

[code]
local game_reset_touch = function( event )
if event.phase == “release” then

elseif event.phase == “release” then

end
end

local game_reset_button = ui.newButton{
defaultSrc = “img”,
defaultX = 100,
defaultY = 50,
overSrc = “img”,
overX = 100,
overY = 50,
onEvent = game_reset_touch
}

– and the game_reset_button.isVisible stay where, i cant put it up of game_reset_button declaration, i only can put it down of button declaration. with other buttons i know how i can hide and show using functions, variables etc, but with ui button i dont know, really want to understand
[/code] [import]uid: 26056 topic_id: 18665 reply_id: 72548[/import]

[lua]local game_reset_touch
local game_reset_button

game_reset_touch = function( event )
if event.phase == “release” then

elseif event.phase == “release” then

end
end

game_reset_button = ui.newButton{
defaultSrc = “img”,
defaultX = 100,
defaultY = 50,
overSrc = “img”,
overX = 100,
overY = 50,
onEvent = game_reset_touch
}[/lua] [import]uid: 16142 topic_id: 18665 reply_id: 72553[/import]