krootki
krootki

Reputation: 3

flex button multiply clicks

In my project (mp3 player) when I click button 'Play' it starts amd plays normal, but when I click button 'next song' it multiplys my clicks,(2 clicks multiply 2 times, 3 clicks 3 times...) and it's starting 2 or 3 or more songs in background. Why it's multiply my button clicks? Someone can help me?

Buttons:

<s:Button id="PlayBtn" x="97" y="14" width="79" height="24" label="Play" 
          click="playButton_clickHandler(event)" enabled="true"/>
<s:Button id="nastepnyBtn" x="354" y="14" width="79" height="24" label="=&gt;&gt;" click="nastepnyBtn_clickHandler(event)"/>

Actions:

  protected function playButton_clickHandler(event:MouseEvent):void {Odtwarzaj();}

  protected function nastepnyBtn_clickHandler(event:MouseEvent):void {Odtwarzaj(1);}

PlayCode:

   function Odtwarzaj(tryb:Number=0):void{
            var wybrany:Object = dataGridId.selectedItem;

            if(wybrany!=null){
                switch(tryb){
                    case 0:
                        if(playing)
                        stop();

                        WybranyKawalek=dataGridId.selectedIndex;
                        break;
                    case 1:
                        if(!Normalnie.selected){
                            Alert.show("losuje");
                        }else
                        {
                            if(WybranyKawalek==tablica.length-1){
                                WybranyKawalek=0;
                            }else
                            {
                                WybranyKawalek=WybranyKawalek+1;
                            }

                        }
                        stop();
                        //playing=false;
                        break;
                    case 2:
                        if(!Normalnie.selected){
                            Alert.show("losuje");


                        }else
                        {
                            if(WybranyKawalek==0){
                                WybranyKawalek=tablica.length-1;
                            }
                            else{   

                                WybranyKawalek=WybranyKawalek-1;
                            }
                        }
                        stop();
                        //playing=false;
                        break;

                    }


                    Alert.show("test="+test+" tryb="+tryb+" wybrany:"+WybranyKawalek+" t.l="+tablica.length+" dg.si="+dataGridId.selectedIndex);


                    playingLable.text=tablica.getItemAt(WybranyKawalek).artysta+" - "+tablica.getItemAt(WybranyKawalek).tytul;
                    file = new File(tablica.getItemAt(WybranyKawalek).URL_PLIKU);

                    if(!playing){
                        muzyka=new Sound();
                        muzyka.addEventListener(IOErrorEvent.IO_ERROR, errorHandlerMusic);
                        muzyka.addEventListener(Event.COMPLETE, loadCompleteMusic);
                        muzyka.load(new URLRequest(file.url));//, context

                        }


            }else{
                Alert.show("Zaznacz plik do odegrania!");
            }

            test++;
        }

Playstart function:

      private function play(pos:Number=0):void{
            playing=true;
            kanal=muzyka.play(pos,trans);
            refresh();
            addEventListener(Event.ENTER_FRAME, displayProgressInSec);
        }

Upvotes: 0

Views: 103

Answers (1)

StephenNYC
StephenNYC

Reputation: 1264

The problem here is calling addEventListener(Event.ENTER_FRAME, displayProgressInSec) everytime you hit the button but never removing it. everytime you add the listener, it accumulates and all will responds to the enter-frame event.

Upvotes: 1

Related Questions