how to prevent a double tap

I have a function where I play a song. with :addEventListener

when users “tap” the button

– pennyImage:addEventListener(“tap”, pennyTap)

I hear the song fine.


Problem


if for accident I double click, or double tap

now I hear the song and a few Milli seconds I hear the song again.

so now I’m listening to the same song with an echo.


I want to tell the program

– if I “tap” once or the first time…

don’t let me “tap” again.

could you please help me to know how to do this?

I have writen before: if you want to play it once then remember that its playing

Just a note: the “numTaps” event value can also filter this down to the first tap only. But as @piotrz55 says, you should also check that the song is not already playing.

http://docs.coronalabs.com/api/event/tap/numTaps.html

Brent

Hi piotrz55.

I tried this

– if tapAlreadyDone == false then

end

and inside I put the function – no good.

I tried putting it in different places – no good

I know that you might have told me something similar before

but I still don’t understand it very well. Sorry, I am not as good as I should be.

I’m doing my best


Hi Brent. I read the page for numTaps

I did not understand it. I didn’t know how to apply it to my problem.


and when you said

“you should also check that the song is not already playing.”

how do I do that?

Hello, ‘i tried putting it in different places’ -stop there! Programing is not a magic trick, it’s strict and based on logical concepts tool to archive certain task. When programing you must be like engineer with screw driver. You know or not how to use it. If not you can try and learn but plisss… do not except it to work somehow when you put it everywhere you can blindly.

I know learning programing is hard and not always easy to catch. I understed making errors and learning. But, for your own good, do not: put it everywhere and wait something happened. I am aware you are rather humanist typs (music) but please be aware of it otherwhise you will hurt yourself and create needles mess and confusion.

Treat programing like building from lego blocks. There are always basic and constant components. There are many ways to connect but you won’t make good structures trying to connect them without obeying their properties.

Also you need to think flexible. For example we give you screw driver and you learn how to use but it doesn’t mean you use it for everything as is. You must sometimes change something, make useful for you - as to use hammrer for other problem and screw driver for other.

Ask - we will always try to help you but there are some things we can’t - you must overcome it yourself and we will try to guide you

As for posting for code: we won’t know the music if you only give us some parts of notes or say you play it high when we have no idea what you play. Always give us some code around asked parts not random notes.

Hi piotrz55, thanks for all your advices.

I liked when you said “You know or not how to use it.”

well, I guess I don’t know yet!

I’m learning, and thanks to all your advices and every one else

I doing some apps. I really like to learn all this, it’s wonderful.

But I do need a lot more help.

When I try something new, I have a little idea in what to do.

but I know that I do need a lot more.

this is the code

newImage = display.newImage ("start.png")     group:insert ( newImage )     newImage.x = 280; newImage.y = 500         local function pennyTap ()             local function playSong ()                 audio.play(pennySong)             end                     transition.to(ImageLarge, {time=200, x=465, y=756, alpha=1, onComplete=playSong})         end     newImage:addEventListener("tap", pennyTap)

very simple, but in double tap or even triple tap I hear 3 songs right after the other like echo

And believe me, I do honestly appreciate all the help you guys give me here.

if it wasn’t for you I would never be able to make the apps and work on this

it will take me time to learn, but I will, and I will share everything I know with

more people, I think programming it’s a wonderful art.

As stated in the docs,
event.numTaps - “The number of taps on the screen.”

i.w. you can get the current number of taps and use that for deciding whether or not something should happen; in your case playing an audio file.

Or if you don’t want to do it that way, you could simply create a variable “numTaps” (or whatever you want to name it) set it to zero and then when the object (you’re listening for) is tapped just increase ‘numTaps’ value by one. That way you can tell it Not to play the song if ‘numTaps’ equals a certain value - 2,3,4 etc…

Cheers!

-Saer

I know that you guys want me to think.

and that is good…

but today all day long I have thinking and thinking

and I just can’t.

maybe tomorrow…

I did this

local numTaps = 0 local songIsPlaying = false

and then this

local function play ()                 if numTaps == 0 then                     songIsPlaying = true                     songIsPlaying = audio.play(pennySong)                     numTaps = numTaps + 1                 elseif numTaps \> 0 then                     print("no song")                 end             end  

and no good.

there are so many ways of doing it

but I just can’t find one of them.

if you guys don’t want to actually write this piece of code

I will understand, you’re trying to help me to actually “think”

I’m just not that good yet.

Firstly, in your code you are telling the song to play if numTaps = 0 and then in the same conditional statement you’re increasing ‘numTaps’… so technically you’re not doing anything differently.

‘numTaps’ is just a way you can manually keep track of a value.
For example, say you had a bunch of cars moving throughout the display and you wanted something to happen when the current number of cars equaled 20.
Just create a variable - numCars = 0
Then every time you add a car to the screen/display you would increase it - numCars = numCars + 1

 - I’ll use your own code -

local numTaps = 0 local function play ()     if numTaps == 0 then        songIsPlaying = true        songIsPlaying = audio.play(pennySong)        numTaps = numTaps + 1      elseif numTaps \> 0 then          print("no song")     end end

What you’d need to do is increase ‘numTaps’ by one when you touch/tap the penny.

local numTaps = 0 local function play ()   -- increase 'numTaps' by one --      -- then if 'numTaps' equals 1 play the song -- -- other code here --      elseif numTaps \> 1 then          print("no song")     end end

Since you’re using ‘numTaps’ on a conditional basis, you’ll have to decrease its value after the event has completed - If you didn’t, the song would never play because every time you tap the penny ‘numTaps’ would keep getting increased.
I would consider either making your function an event so that on the ended phase of the tap you can decrease the value of ‘numTaps’ by one or make your ‘penny’ object a widget-button and then code what you want to occur in a function that you would reference in the ‘onRelease’ call.

-Saer

Thank you Saer…

on double click the – the first time –

works great I don’t hear 2 songs.

but I when I click the second time – I hear Nothing.

it works only once.

--------- today that’s it------ my brain is stuck!

I’m going to keep working on this tomorrow

I can try the widget-button option to see if I can make it work.


the other problem is the yellow rects…

I stop the music but the rects keep showing on/off

I will try to see if I can apply the same logic on the done button.

thanks for all your help guys.

I’m going to keep working and learning

if there is someone that never gives up… it’s me.

thanks

Are you reducing ‘numTaps’ value by 1 after the event has finished?
Like I said above “Since you’re using ‘numTaps’ on a conditional basis, you’ll have to decrease its value after the event has completed”
When you tap the penny ‘numTaps’ gets a 1 added to its current value, if it’s the first tap then it would be 0 + 1 making its current value 1, then if you go and try to play the song again you’d find, as you have, that it won’t play… that is because you never reduced ‘numTaps’ which means every subsequent tap is increasing the value (first tap sets the value to 1, second to 2 etc… etc…)
Obviously your song won’t play because you have
elseif numTaps > 1 then
  --don’t play the song–

Simply decrease ‘numTaps’ after the event/function has ended.

-Saer

I did this

local function play ()                 numTaps = numTaps + 1                 if numTaps == 1 then                     songIsPlaying = true                     songIsPlaying = audio.play(pennySong)                     numTaps = numTaps + 1                 elseif numTaps \> 1 then                     print("no song")                     numTaps = 0                 end         end  

I think that is what you said about – “Simply decrease ‘numTaps’ after the event/function has ended.”

but now weird things happens.

– if I play once fine – the first time

– I push the button done once

– I play again – no sound

– I push the button done

–Push play and it plays the song

done

play – nothing

done

play – yes


one yes and one no

— and also the yellow rects, they keep visible = true and false

here is the complete code if you please can get me out of this misery

I think is way too much for me at this point

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local widget = require "widget" ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[LOAD SOUNDS]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ local pennySong = audio.loadSound ( "penny.mp3" ) ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[VARIABLES]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ local pennyImage local pennyImageLarge local done local measure1 local numTaps = 0 local songIsPlaying = false local distance = 245 local measure1Timer local measure2Timer local measure3Timer local measure4Timer local measure5Timer ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[FUNCTIONS]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ local function yellow1 ()     local function measure1Listener( event )         measure1.isVisible = true     end     measure1Timer = timer.performWithDelay( 200, measure1Listener ) end local function yellow2 ()         local function measure2Listener( event )         measure1.isVisible = false         measure2.isVisible = true     end     measure2Timer = timer.performWithDelay(3130, measure2Listener ) end local function yellow3 ()         local function measure3Listener( event )         measure2.isVisible = false         measure3.isVisible = true     end     measure3Timer = timer.performWithDelay(6260, measure3Listener ) end local function yellow4 ()         local function measure4Listener( event )         measure3.isVisible = false         measure4.isVisible = true     end     measure4Timer = timer.performWithDelay(9390, measure4Listener ) end local function yellow5 ()         local function measure5Listener( event )         measure4.isVisible = false         measure5.isVisible = true     end     measure5Timer = timer.performWithDelay(12400, measure5Listener ) end ---------------------------------------------------------------WIDGET FUNCTION------------ local function pennyImageHandler()         local function play ()                 numTaps = numTaps + 1                 if numTaps == 1 then                     songIsPlaying = true                     songIsPlaying = audio.play(pennySong)                     numTaps = numTaps + 1                 elseif numTaps \> 1 then                     print("no song")                     numTaps = 0                 end         end             transition.to(pennyImageLarge, {time=200, x=display.contentWidth / 2, y=display.contentHeight / 2, xScale=1, yScale=1, alpha=1, onComplete=play})         transition.to(done, {time=200, y=700})         yellow1()         yellow2()         yellow3()         yellow4()         yellow5()         return true end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[CREATE SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:createScene( event )     local group = self.view     local background = display.newImage ("backgroundBook.png")     group:insert ( background )     pennyImage = widget.newButton{         defaultFile="pennyImage.png",         onRelease = pennyImageHandler     }     group:insert ( pennyImage )     pennyImage.x = 280; pennyImage.y = 500          local tx1 = display.newText("The song for piano is just an example so I can make this beautiful book, and I know that once I finish this. I will be doing okay. Corect!. Now I can type more text. And then I can play more songs, I just want to see if I can make it work", 0, 0, 440, 200, native.systemFont, 16)     group:insert ( tx1 )     tx1.x = 290; tx1.y = 170     tx1:setTextColor(0, 0, 0)          pennyImageLarge = display.newImage ("penny.png")     group:insert ( pennyImageLarge )     pennyImageLarge.x = pennyImage.x; pennyImageLarge.y = pennyImage.y     pennyImageLarge.xScale = .1     pennyImageLarge.yScale = .1     pennyImageLarge.alpha=0          done = display.newImage ("done1.png")     group:insert ( done )     done.x = display.contentWidth / 2; done.y = 900         local function doneStop ()             audio.stop()             transition.to(done, {time=200, y=900})             transition.to(pennyImageLarge, {time=200, x=pennyImage.x, y=pennyImage.y, xScale=.1, yScale=.1, alpha=0})             measure1.isVisible = false             measure2.isVisible = false             measure3.isVisible = false             measure4.isVisible = false             measure5.isVisible = false             timer.cancel(measure1Timer)             timer.cancel(measure2Timer)             timer.cancel(measure3Timer)             timer.cancel(measure4Timer)             timer.cancel(measure5Timer)         end     done:addEventListener("tap", doneStop)          musicList = display.newImage ("buttonMusic.png")     group:insert ( musicList )     musicList.x = 100; musicList.y = 730         local function listListener ()             storyboard.gotoScene( "page2", "fromRight", 100 )             audio.stop()             timer.cancel(measure1Timer)             timer.cancel(measure2Timer)             timer.cancel(measure3Timer)             timer.cancel(measure4Timer)             timer.cancel(measure5Timer)         end     musicList:addEventListener("tap", listListener) end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[ENTER SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:enterScene( event )     local group = self.view          measure1 = display.newRect(0, 0, 160, 106)     group:insert ( measure1 )     measure1.x = distance; measure1.y = 305     measure1:setFillColor(255, 255, 0)     measure1.alpha = .2     measure1.isVisible = false          measure2 = display.newRect(0, 0, 166, 106)     group:insert ( measure2 )     measure2.x = distance \* 1.66; measure2.y = 305     measure2:setFillColor(255, 255, 0)     measure2.alpha = .2     measure2.isVisible = false          measure3 = display.newRect(0, 0, 161, 106)     group:insert ( measure3 )     measure3.x = distance \* 2.345; measure3.y = 305     measure3:setFillColor(255, 255, 0)     measure3.alpha = .2     measure3.isVisible = false          measure4 = display.newRect(0, 0, 164, 106)     group:insert ( measure4 )     measure4.x = distance \* 3.02; measure4.y = 305     measure4:setFillColor(255, 255, 0)     measure4.alpha = .2     measure4.isVisible = false          measure5 = display.newRect(0, 0, 160, 106)     group:insert ( measure5 )     measure5.x = distance \* 3.7; measure5.y = 305     measure5:setFillColor(255, 255, 0)     measure5.alpha = .2     measure5.isVisible = false end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[EXIT SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:exitScene()      end ------------------------------------------------------------------------------------------ -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[DESTROY SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ------------------------------------------------------------------------------------------ function scene:destroyScene( event )     local group = self.view                  end ---------------------------------------------------------------------------------- scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

Thanks you for everything

my wife it’s waiting for me outside

see you tomorrow

thanks

The reason it doesn’t sometimes works is because of the following:
First tap numTaps = 1 – plays song – > second tap numTaps = 2 – does not play song – numTaps is greater than 1 set to 0 > third tap numTaps = 1 – plays song – Etc… etc…

What you need to do, is make this whole thing an event and on the ‘ended’ phase reduce numTaps by 1.

-Saer

I really don’t know how to make this in to an event

would you please show me the code to make it into an event?

I know I should think or try to do it on my own, but honestly

it’s too much. You will show me and teach me a lot

if you can actually show me the actual code.

Thanks for everything.

This should be very helpful for you (it’s one of the better structured document pages)
http://docs.coronalabs.com/api/type/Event.html

-Saer

 

Does something like this work?

[lua]
local pennyChannel = 0

local function play()

  if ( pennyChannel ) then

    if not ( audio.isChannelPlaying( pennyChannel ) ) then

      pennyChannel = audio.play( pennySong )

    end

  end

end
[/lua]

Thanks Saer…

I read that before, about a month ago.

I read that today 2 times, really slow, an trying to understand what to do…

–Like this line “Hit events propagate until they are handled. You can stop propagation to the next object (all listeners of the current object still get the event) by telling the system that the event was handled.”

– by telling the system that the event was handled. – yes… but how?


or this one “This is very difficult to achieve using the default dispatch behavior and propagation rules of hit events.”

– I know it’s very difficult, that is why I do need help

why they don’t actually put

– sample code in a simple app

– or pictures

– or better yet, a little video, with real life examples

–in plain English words, not computer terms


do they do it like that to make it on purpose difficult,

so only a very few privileged ones can actually make nice apps?


or they make it difficult, because that is the way they learned,

after a lot of time, and they know no other way to teach?


or they just don’t have time and money to add videos

graphics, pictures, sample code, and all that to their web-site?



Just look on top of this post…

how many post, back and forth…

and still I can’t do it.


I think it’s me, I just want to do something that I am not prepared for, yet!

I guess I’m asking too much, to actually see a piece of code.

I just wish I could see the code to actually learn and study it.

----like the code piotrz55. made for me, on physics

when I was trying to learn collision

he gave me this – if event.other.name == “crate” then

where I had to write the “name” of the object

and of course the – event.other.name

without those few lines in an actual little app

I would probably still be looking for the answer today.

I will keep looking, studying and waiting for the code…

I hope someone will be so kind, to gave me that piece of information

(my birthday is on October 8th – I’ll be 53)

In your code replace this:

[lua]

        local function play ()
                numTaps = numTaps + 1
                if numTaps == 1 then
                    songIsPlaying = true
                    songIsPlaying = audio.play(pennySong)
                    numTaps = numTaps + 1
                elseif numTaps > 1 then
                    print(“no song”)
                    numTaps = 0
                end
        end [/lua]

With this:

[lua]

local pennyChannel = 0

local function play()

  if ( pennyChannel ) then

    if not ( audio.isChannelPlaying( pennyChannel ) ) then

      pennyChannel = audio.play( pennySong )

    end

  end

end

[/lua]

And see if it helps at all.