AS3: Trigger artificial MouseEvent

Patrick Oscity picture Patrick Oscity · Oct 12, 2010 · Viewed 7.9k times · Source

I am converting a simple flash 'drumset' application to support TUIO multitouch using the tuio as3 reference implementation from http://www.tuio.org/?flash

As a quick and dirty solution, i am trying to trigger an artificial MouseEvent, but nothing seems to happen :( where is my error? is this even possible? thanks already!

here's the code:

package {

    import org.tuio.tuio.*;
    import org.tuio.osc.*;
    import flash.display.*;
    import flash.ui.*;
    import flash.events.*;
    import flash.media.*;

    public class drumsets2 extends MovieClip implements ITuioListener {

        private var tuio:TuioClient;

        var soundS01:Sound = new S01();
        // more sounds...

        public function drumsets2(){
            this.tuio = new TuioClient(new LCConnector());
            this.tuio.addListener(this);

            drum1.hitS01.addEventListener(MouseEvent.MOUSE_DOWN, playS01);
            // more event listeners for sounds...
        }


        // this is where the 'magic' is supposed to happen

        public function addTuioCursor(tuioCursor:TuioCursor):void {
            stage.dispatchEvent(
                new MouseEvent( MouseEvent.MOUSE_DOWN, true, false, tuioCursor.x*stage.stageWidth, tuioCursor.y*stage.stageHeight )
            );
        }


        function playS01(e:MouseEvent):void
        {
            var scS01:SoundChannel = soundS01.play();
        }

        // more play functions...
    }
}

Answer

Aaron picture Aaron · Oct 13, 2010

Your event listener is not on the stage, it is on drum1.hitS01, which I will assume is a DisplayObject as it is not defined anywhere in your attached code. All you should need to do is dispatch the event on that object, not on the stage:

drum1.hitS01.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN, true, false, tuioCursor.x * stage.stageWidth, tuioCursor.y * stage.stageHeight));