touch twice

i’m trying to do function,where one touch is a one thing like group.isVisible = true
and second touch will be image.isVisible = false

and then another touch will be .isVisible = true and so on…

any help is hugely appreciated

thats my trying to do something)

local function touchInventoryButton(e) if(e.phase == "ended") then group.isVisible = true elseif(e.phase == "ended") then group.isVisible = false end end [import]uid: 16142 topic_id: 11415 reply_id: 311415[/import]

@darkconsoles,
your code has
[lua]if e.phase==“ended” then
---- 1
elseif e.phase==“ended” then
---- 2
end[/lua]
this will always do the first bit as the condition is the same.

Now you want to do something when the button it touched and then when it is no longer touching, you need to use “began” and “ended”

you can use it in a short circuit logic (as it is called)
[lua]group.isVisible = (“began”==e.phase)[/lua]

and if you want it to be a toggle button, which is on and then off when touched again then you need

[lua] if “ended” == e.phase then
if 1==e.target.state then
e.target.state=0
else
e.target.state==1
end

group.isVisible = (0==e.target.state)
end[/lua]
cheers,

?:slight_smile: [import]uid: 3826 topic_id: 11415 reply_id: 41339[/import]

thanks a lot, working like a charm
i did not know about e.target.state thing…
anywhere i can read about this and learn?) [import]uid: 16142 topic_id: 11415 reply_id: 41340[/import]

you can read about it in the documentation.

However most books and other places will advice that you use a variable called toggleState (for example)and set that to either 0 or 1 depending on the state (numeric coz you can increase the states later, with true and false, you have just two states)

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 11415 reply_id: 41341[/import]

hello again, same problem, but now just a little different
im using ui module do display inventory on the bottom of the screen and a button in main.lua to do such a thing
but when i write it to be a toggle, its not working

  
local inventoryButton = display.newImage("1.png")  
inventoryButton.x = 10; inventoryButton.y = 10  
  
local function ShowInventory(e)  
if "ended" == e.phase then  
 if 1==e.target.state then  
 e.target.state=0  
 else  
 e.target.state=1  
 end  
 ui.ShowINV() -- seems it not working with functions the way its working with display groups...  
 end  
end  
  
inventoryButton:addEventListener("touch", ShowInventory)  

any advice?) [import]uid: 16142 topic_id: 11415 reply_id: 42278[/import]

Where is ui.showINV() and what does it do?

instead just for debugging replace that with print(“showINV”) and you will see that line 11 just works.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 11415 reply_id: 42279[/import]

i figure out this trick, but i think there must be a better way to do such things…

[code][lua]InvVisible = false

local inventoryButton = display.newImage(“1.png”)
inventoryButton.x = 10; inventoryButton.y = 10

local function ShowInventory(e)
if e.phase == “ended” and InvVisible == false then
ui.ShowINV()
InvVisible = true
elseif e.phase == “ended” and InvVisible == true then
ui.HideINV()
InvVisible = false
end
end
inventoryButton:addEventListener(“touch”, ShowInventory)
[/code][/lua] [import]uid: 16142 topic_id: 11415 reply_id: 42296[/import]