开发者

AS3: Trigger artificial MouseEvent

开发者 https://www.devze.com 2023-01-20 10:32 出处:网络
I am converting a simple flash \'drumset\' application to support TUIO multitouch using the tuio as3 reference implementation from http://www.tuio.org/?flash

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:开发者_JAVA技巧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...
    }
}


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));


If I am understanding your question correctly, it seems like you're just trying to call the playS01 function from code? If so you can, anywhere in your class, call playS01(null). You need to pass it null if it's not coming from a mouse event so it doesn't bug you about not receiving an expected argument.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号