How to detect two functions in the same button?

Hi everyone…

I have a problem, I hope I can get some help.

I have one button (white rect)

I add an event listener to that button.

when I “tap” the first time… I move a green rect somewhere else, or do something

a function that works fine

but then when I “tap” the “second” time I need to make the same button

do something else

here is a piece of code

    teclaFa1 = display.newRect( 0, 0, loAnchoDeLaTeclaEs, laAlturaDeLaTeclaBlancaEs )     sceneGroup:insert(teclaFa1)     teclaFa1.x = teclaDo.x + loAnchoDeLaTeclaEs \* 3     teclaFa1.y = teclaDo.y     teclaFa1:setFillColor( 1, 1, 1 )     teclaFa1.strokeWidth = 1     teclaFa1:setStrokeColor( 0, 0, 0 )     teclaFa1.isVisible = true     local function tocaFa1()     if isBeat1 == true and isCorrectFa1 == true and  pmViditas \>= 1 and isMoving == false then         locator.x = notaFa2.x         moveBoat()         isBeat1 = false         isBeat2 = true     else         destroyBoat()     end         audio.play(playFa)         return true     end     teclaFa1:addEventListener("tap", tocaFa1)

So the first time will move the locator to notaFa2.x

then next time it has to move it to notaFa3.x

and now is the second beat of music

I am just going crazy, trying to figure this out…

I try … if firstTime == true, or something like that, but I just don’t know

how to make the same button to understand

when I tap it for the first time, or the second time, or the third…

I hope I make sense and you can help me with this

Victor

As always I really appreciate your time in helping me out…

I don’t use “tap” events, I always use “touch” events, but I think something like this should be what you are after.

when you spawn your rect, add:

teclaFa1.Beat = 1

Then when you tap it look for the value and change it accordingly.

do your function something like this…

function lookForBeat(event)

    if event.target.Beat == 1 then

        teclaFa1.Beat = 2

        do something

    ellseif event.target.Beat == 2 then

       teclaFa1.Beat = 3

       do something else

    end

return true

end

Hope this helps,

Nail

Well i think you’ve got an idea of the logic that you need from reading the post.

2 points.

  1. in the above code there is nothing to indicate the reverse switch ie isbeat1 = true, isBeat2 = false … 

this all depends on how you want to use the switching.

If only two choices then a true/false works but if more then a number system is what i use with a counter - if x ==1 then …elseif x ==2 etc…

from the code the first touch will make isBeat1 to false so any further touch will go straight to destroyboat.

you could make things longer but simplier.

don’t concern yourself with the if…else.

Just do if …1 == true then …  ( …1 = false, …2 = true) end

and 

then  if …2 == true then … ( …2 = false, …3 = true)end

I always tend to go (with code) simple (not pretty ) first then once simple works the way i want - look for ways to make it pretty if i feel the need.

your layered if … and … and …and could be causing you problems it’s hard to say - the code you posted actually is not very helpful in trying to solve your problem.

what do you want to happen if isBeat2 = true??

T.

What you could do is pass the eventListener to another function when clicked or tapped

i.e.[LUA]

–forward declaration of variables:

local myButton = {} 

local function myFunc1 = {}

local function myFunc2 = {}

function myFunc1()

 – do some tricks and stuff

 myButton.tap = myFunc2 --when tapped, the button will now listen to myFunc2

end

function myFunc2()

– do some more tricks and stuff

myButton.tap = myFunc1 --when tapped, the button goes back to myFunc1 or a third function…

end

myButton:addEventListener(“tap”, myButton)
myButton.tap = myFunc1     --the first tap on the button is directed to myFunc1

[/LUA]

Remember that if display objects is placed on top of each other you may need to use a touch eventListener with display.getCurrentStage():setFocus() on the propriate phase (i.e. if event.phase == “began” then …) so you manage to hit the right eventListener. Keep in mind that this can´t be done with a tap listener. Read about this here: http://docs.coronalabs.com/api/type/StageObject/setFocus.html

I don’t use “tap” events, I always use “touch” events, but I think something like this should be what you are after.

when you spawn your rect, add:

teclaFa1.Beat = 1

Then when you tap it look for the value and change it accordingly.

do your function something like this…

function lookForBeat(event)

    if event.target.Beat == 1 then

        teclaFa1.Beat = 2

        do something

    ellseif event.target.Beat == 2 then

       teclaFa1.Beat = 3

       do something else

    end

return true

end

Hope this helps,

Nail

Well i think you’ve got an idea of the logic that you need from reading the post.

2 points.

  1. in the above code there is nothing to indicate the reverse switch ie isbeat1 = true, isBeat2 = false … 

this all depends on how you want to use the switching.

If only two choices then a true/false works but if more then a number system is what i use with a counter - if x ==1 then …elseif x ==2 etc…

from the code the first touch will make isBeat1 to false so any further touch will go straight to destroyboat.

you could make things longer but simplier.

don’t concern yourself with the if…else.

Just do if …1 == true then …  ( …1 = false, …2 = true) end

and 

then  if …2 == true then … ( …2 = false, …3 = true)end

I always tend to go (with code) simple (not pretty ) first then once simple works the way i want - look for ways to make it pretty if i feel the need.

your layered if … and … and …and could be causing you problems it’s hard to say - the code you posted actually is not very helpful in trying to solve your problem.

what do you want to happen if isBeat2 = true??

T.

What you could do is pass the eventListener to another function when clicked or tapped

i.e.[LUA]

–forward declaration of variables:

local myButton = {} 

local function myFunc1 = {}

local function myFunc2 = {}

function myFunc1()

 – do some tricks and stuff

 myButton.tap = myFunc2 --when tapped, the button will now listen to myFunc2

end

function myFunc2()

– do some more tricks and stuff

myButton.tap = myFunc1 --when tapped, the button goes back to myFunc1 or a third function…

end

myButton:addEventListener(“tap”, myButton)
myButton.tap = myFunc1     --the first tap on the button is directed to myFunc1

[/LUA]

Remember that if display objects is placed on top of each other you may need to use a touch eventListener with display.getCurrentStage():setFocus() on the propriate phase (i.e. if event.phase == “began” then …) so you manage to hit the right eventListener. Keep in mind that this can´t be done with a tap listener. Read about this here: http://docs.coronalabs.com/api/type/StageObject/setFocus.html