how to add event "cancel" to this function

Hello…

I have a function that works great with “began” phase of a touch function.

I just can not add the cancel phase or ended phase.

It has so many – if’s  —  elseif’s — and – also “end” of functions

that I just can not do it.

Please would you help me out… Thanks.

I just want to make the – key49.isVisible = true

I want to make it – key49.isVisible = false

on ended. This is my function

    function primerPentagrama( event )            if event.phase == "began" and ( event.target.id ) == 1 then               audio.play(n49)               key49.isVisible = true            elseif event.phase == "began" and ( event.target.id ) == 3 then                audio.play(n46)            elseif event.phase == "began" and ( event.target.id ) == 5 then                audio.play(n49)            elseif event.phase == "began" and ( event.target.id ) == 6 then                audio.play(n49)            elseif event.phase == "began" and ( event.target.id ) == 7 then                audio.play(n46)            elseif event.phase == "began" and ( event.target.id ) == 9 then                audio.play(n49)            elseif event.phase == "began" and ( event.target.id ) == 10 then                audio.play(n49)            elseif event.phase == "began" and ( event.target.id ) == 11 then                audio.play(n46)            elseif event.phase == "began" and ( event.target.id ) == 12 then                audio.play(n51)            elseif event.phase == "began" and ( event.target.id ) == 13 then                audio.play(n49)            elseif event.phase == "began" and ( event.target.id ) == 14 then                audio.play(n49)            elseif event.phase == "began" and ( event.target.id ) == 15 then                audio.play(n46)         else         return true                     end     end

Thanks for all your help

Break down the events in separate ifs:

[lua]

if event.phase == “began” then

    if event.target.id == 3 then

        audio.play(xxxx)

    elseif event.target.id == 5 then

        audio.play(xxxxx)

    end if

elseif event.phase == “ended” then

    – ended stuff here

end if

[/lua]

This will also speed things up since you are not comparing the phase in every if.

Hope this helps.

It make sense… I just have too many if and else and ends

each of my id objects it’s showing an image on “began”

and hiding the image on “ended”

So I created a function before to hide all of then

function hideKeys()     key49.isVisible = false     key46.isVisible = false     key51.isVisible = false     key47.isVisible = false     key44.isVisible = false     key42.isVisible = false end

and then at the ended phase I call that function

so they all will hide all images regardless which one is visible

I hope this helps.

But I have ERROR in my function too many if and else and ends

function primerPentagrama( event )     if event.phase == "began" then         if event.target.id == 1 then             audio.play(n49)             key49.isVisible = true         elseif event.target.id == 3 then             audio.play(n46)             key49.isVisible = true         elseif event.target.id == 5 then             audio.play(n42)             key42.isVisible = true         end if     elseif event.phase == "ended" then         hideKeys()     else         return true                 end end

Please help me out to solve this problem

Thank you very much

Victor

I got it to WORK!!!

Looking at your code, and with the Help of Brent, and after thinking and thinking…

I got it… thanks to everyone…

The problem was on your line No. 9

end if

I use that an put it on my code

elseif event.target.id == 5 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; audio.play(n42) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; key42.isVisible = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end if -- \<------------------- RIGHT HERE &nbsp;&nbsp;&nbsp; elseif event.phase == "ended" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hideKeys()

and did not work

I did this

&nbsp; &nbsp;&nbsp;&nbsp; elseif event.phase == "began" and ( event.target.id ) == 14 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;audio.play(n44) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;key44.isVisible = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif event.phase == "began" and ( event.target.id ) == 15 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;audio.play(n42) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;key42.isVisible = true -- \<----- Right after the last .id &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif event.phase == "ended" or event.phase == "cancelled" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;hideKeys() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return true&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end

And now it works great!

Thanks to everyone for all your help

Victor

Oops, sorry about that.  :slight_smile:

I was mixing my programming languages.

it’s okay… I got it to work

thanks for your help!

Break down the events in separate ifs:

[lua]

if event.phase == “began” then

    if event.target.id == 3 then

        audio.play(xxxx)

    elseif event.target.id == 5 then

        audio.play(xxxxx)

    end if

elseif event.phase == “ended” then

    – ended stuff here

end if

[/lua]

This will also speed things up since you are not comparing the phase in every if.

Hope this helps.

It make sense… I just have too many if and else and ends

each of my id objects it’s showing an image on “began”

and hiding the image on “ended”

So I created a function before to hide all of then

function hideKeys() &nbsp;&nbsp;&nbsp; key49.isVisible = false &nbsp;&nbsp;&nbsp; key46.isVisible = false &nbsp;&nbsp;&nbsp; key51.isVisible = false &nbsp;&nbsp;&nbsp; key47.isVisible = false &nbsp;&nbsp;&nbsp; key44.isVisible = false &nbsp;&nbsp;&nbsp; key42.isVisible = false end

and then at the ended phase I call that function

so they all will hide all images regardless which one is visible

I hope this helps.

But I have ERROR in my function too many if and else and ends

function primerPentagrama( event ) &nbsp;&nbsp;&nbsp; if event.phase == "began" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if event.target.id == 1 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; audio.play(n49) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; key49.isVisible = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif event.target.id == 3 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; audio.play(n46) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; key49.isVisible = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif event.target.id == 5 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; audio.play(n42) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; key42.isVisible = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end if &nbsp;&nbsp;&nbsp; elseif event.phase == "ended" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hideKeys() &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return true&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; end end

Please help me out to solve this problem

Thank you very much

Victor

I got it to WORK!!!

Looking at your code, and with the Help of Brent, and after thinking and thinking…

I got it… thanks to everyone…

The problem was on your line No. 9

end if

I use that an put it on my code

elseif event.target.id == 5 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; audio.play(n42) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; key42.isVisible = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end if -- \<------------------- RIGHT HERE &nbsp;&nbsp;&nbsp; elseif event.phase == "ended" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hideKeys()

and did not work

I did this

&nbsp; &nbsp;&nbsp;&nbsp; elseif event.phase == "began" and ( event.target.id ) == 14 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;audio.play(n44) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;key44.isVisible = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif event.phase == "began" and ( event.target.id ) == 15 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;audio.play(n42) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;key42.isVisible = true -- \<----- Right after the last .id &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif event.phase == "ended" or event.phase == "cancelled" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;hideKeys() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return true&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end

And now it works great!

Thanks to everyone for all your help

Victor

Oops, sorry about that.  :slight_smile:

I was mixing my programming languages.

it’s okay… I got it to work

thanks for your help!