AS3:触发人工MouseEvent

发布于 2024-09-27 08:10:32 字数 1373 浏览 11 评论 0原文

我正在使用 http://www 的 tuio as3 参考实现转换一个简单的 flash 'drumset' 应用程序以支持 TUIO 多点触控.tuio.org/?flash

作为一个快速而肮脏的解决方案,我试图触发一个人工 MouseEvent,但似乎什么也没有发生:(我的错误在哪里?这可能吗?已经谢谢了!

这是代码:

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...
    }
}

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...
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最偏执的依靠 2024-10-04 08:10:32

您的事件侦听器不在舞台上,而是在鼓1.hitS01上,我假设它是一个 DisplayObject,因为它没有在附加代码中的任何地方定义。您需要做的就是在该对象上调度事件,而不是在舞台上:

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

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));
心安伴我暖 2024-10-04 08:10:32

如果我正确理解你的问题,那么你似乎只是想从代码中调用 playS01 函数?如果是这样,您可以在班级的任何位置调用 playS01(null)
如果它不是来自鼠标事件,则需要将其传递为 null,这样它就不会因未收到预期参数而困扰您。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文