probably silly question

I’m brand new to programming, sorry if this is lame :slight_smile: The idea of the code below is to hide the donkey by clicking its tail, then, if it is hidden, show it - also by clicking its tail. I can get click to hide working, but then cannot click to show. I wonder if someone can lend a hand, I would sure appreciate help.
local function hidedonk (event)
donkey.isVisible = false
end

local function showdonk (event)
donkey.isVisible = true
end

if donkey.isVisible == true then
tail:addEventListener (“touch”, hidedonk)

elseif donkey.isVisible == false then
tail:addEventListener (“touch”, showdonk)
end
[import]uid: 96383 topic_id: 16360 reply_id: 316360[/import]

how about this?

[lua]local function toggleDonkeyVisibility (event)
donkey.isVisible = not donkey.isVisible
end

tail:addEventListener (“touch”, toggleDonkeyVisibility)[/lua]

Raúl Beltrán
MIU Games [import]uid: 44101 topic_id: 16360 reply_id: 61001[/import]

Hi Raúl, thanks. That hides the donkey on mouse down but on release it reappears. I’m sure there is a way to rectify that issue. I wonder what I am doing wrong in my original code, if anyone can tell me. It would help me learn what the heck I am doing to figure out how I am screwing that up.

Thanks again extremely. [import]uid: 96383 topic_id: 16360 reply_id: 61017[/import]

Oops!!! you are right…!

this sholud work:

[lua]local function toggleDonkeyVisibility (event)
if event.phase == ‘up’ then
donkey.isVisible = not donkey.isVisible
end
end

tail:addEventListener (“touch”, toggleDonkeyVisibility)[/lua]

If there are other objects beneath the tail you might need to add a return true to the function, otherwise the listener will be triggered for all of them

Please let me know if this worked

Raúl Beltrán
MIU Games [import]uid: 44101 topic_id: 16360 reply_id: 61023[/import]

The problem with your original code is that you are adding a new listener to the tail with every touch event. If you keep touching the tail with this code you will eventually run out of memory (listeners are never removed).

My suggestion is to use a single listener and add it to the tail only once, that way you can keep clicking it for ever! :slight_smile:

Raúl Beltrán
MIU Games [import]uid: 44101 topic_id: 16360 reply_id: 61025[/import]

Hello, Raúl, thank you for the explanation!

The new code behaves the same, with dissapearance on mouse down but reappearance on release. Perhaps I am implementing it incorrectly? Sure do appreciate the help. I’d tinker with it but I don’t understand yet how

donkey.isVisible = not donkey.isVisible

works - I haven’t seen the “not” statement used anyplace yet.
Kevin [import]uid: 96383 topic_id: 16360 reply_id: 61049[/import]

Kevin:

I have tested the following code and it works properly now, the previous example was checking the event phase “up”, but the phase names I was looking for were “began” and “ended”

[lua]tail = display.newRect ( 100,100,50,50 )
donkey = display.newRect ( 200,100,200,200 )

local function toggleDonkeyVisibility (event)
print (event.phase)
if event.phase == ‘ended’ then
donkey.isVisible = not donkey.isVisible
end
end

tail:addEventListener (“touch”, toggleDonkeyVisibility)[/lua]

Run this code and take a look at the console to see how the different event phases are triggered.

The logic behind the line

[lua]donkey.isVisible = not donkey.isVisible[/lua]

is very simple: The operator not performs a logical negation of an expression, inverting it’s current value.

[lua] local night = true;
local day = not night; – not true -> false[/lua]

So you see, the function toggleDonkeyVisibility determines the new visibility of the donkey based on the current one, inverted.

I hope it’s more clear now :slight_smile:

Also, make sure you are adding the listener only once

Raúl Beltrán
MIU Games [import]uid: 44101 topic_id: 16360 reply_id: 61109[/import]

You rock, Raúl. Works great and I get the “not” logic, easy. By the way, downloaded your game Proteggo and I really like it. I sure appreciate you taking the time to answer my newbie questions when you’ve obviously got a lot of your own development going on.

The one thing I still don’t get is why the original code did not work. Obviously if it had worked I would have, as you pointed out, run out of memory eventually. But I never even got the chance to get there :slight_smile:

Kevin [import]uid: 96383 topic_id: 16360 reply_id: 61159[/import]

Kevin:

Your original code didn’t work in the first instance because the method showdonk is never assigned as a listener for tail because initially the donkey is visible. You’d have to remove the current listener and then add the new one inside the methods showdonk and hidedonk, something like:

[lua]local hidedonk, showdonk

hidedonk = function (event)
if event.phase == ‘ended’ then
donkey.isVisible = false

tail:removeEventListener (“touch”, hidedonk)
tail:addEventListener (“touch”, showdonk)
end
end

showdonk = function (event)
if event.phase == ‘ended’ then
donkey.isVisible = true

tail:removeEventListener (“touch”, showdonk)
tail:addEventListener (“touch”, hidedonk)
end
end

– donkey is initially visible, next action: hide it
tail:addEventListener (“touch”, hidedonk)[/lua]

I didn’t test this piece of code but you shuld get the idea, and in my opinion this approach (or some other similar) is not as clean as the single listener method.

Glad to help, and thank you for downloading Proteggo, I hope you enjoy it :slight_smile:

Raúl Beltrán
MIU Games [import]uid: 44101 topic_id: 16360 reply_id: 61202[/import]