dispatchEvent(new MouseEvent(MouseEvent.CLICK)) do not work in as3

1.7k views Asked by At

Im trying to call a mouse event of MovieClip not by clicking it. I wrote recButton.dispatchEvent(new MouseEvent(MouseEvent.CLICK)); but it does not work here is that part of the code:

   package 
{
    import flash.display.Sprite;
    import flash.media.Microphone;
    import flash.system.Security;
    import org.bytearray.micrecorder.*;
    import org.bytearray.micrecorder.events.RecordingEvent;
    import org.bytearray.micrecorder.encoder.WaveEncoder;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.events.ActivityEvent;
    import fl.transitions.Tween;
    import fl.transitions.easing.Strong;
    import flash.net.FileReference;

    public class Main extends Sprite
    {
        private var mic:Microphone;
        private var waveEncoder:WaveEncoder = new WaveEncoder();
        private var recorder:MicRecorder = new MicRecorder(waveEncoder);
        private var recBar:RecBar = new RecBar();
        private var tween:Tween;
        private var fileReference:FileReference = new FileReference();
        public function Main():void
        {
            recButton.stop();
            activity.stop();
        trace(recButton);
            mic = Microphone.getMicrophone();
            mic.setSilenceLevel(0);
            //mic.activityLevel = 50;
            mic.gain = 100;
            mic.setLoopBack(true);
            mic.setUseEchoSuppression(true);
            Security.showSettings("2");

            addListeners();

        }


        private function addListeners():void
        {
            recButton.addEventListener(MouseEvent.MOUSE_UP, startRecording);
            recorder.addEventListener(RecordingEvent.RECORDING, recording);
            recorder.addEventListener(Event.COMPLETE, recordComplete);
            activity.addEventListener(Event.ENTER_FRAME, updateMeter);
        }

        private function startRecording(e:MouseEvent = null):void
        {
            trace("GERE");
            if (mic != null)
            {
                recorder.record();
                e.target.gotoAndStop(2);

                recButton.removeEventListener(MouseEvent.MOUSE_UP, startRecording);
                recButton.addEventListener(MouseEvent.MOUSE_UP, stopRecording);

                addChild(recBar);

                tween = new Tween(recBar,"y",Strong.easeOut, -  recBar.height,0,1,true);
            }
        }

        private function updateMeter(e:Event):void
        {
            activity.gotoAndPlay(100 - mic.activityLevel);

            if(mic.activityLevel > 10 )
                recButton.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
        }


    }
}

what part of my code is wrong ?

1

There are 1 answers

0
bwroga On BEST ANSWER

The problem is that you are not listening for MouseEvent.CLICK anywhere, only MouseEvent.MOUSE_UP. Try listening for MouseEvent.CLICK.