自定义事件调度位置

发布于 2024-10-09 15:22:14 字数 2806 浏览 0 评论 0原文

我研究自定义事件(监听器)已经有一段时间了,但从未成功制作过。有这么多不同的方法,扩展Event类,又扩展EventDispatcher类,非常混乱! 我想一劳永逸地解决这个问题并学习适当的技术。

package{

 import flash.events.Event;
 public class CustomEvent extends Event{

 public static const TEST:String = 'test'; //what exac is the purpose of the value in the string?
 public var data:Object;

 public function CustomEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false, data:Object = null):void
 {
 this.data = data;
 super();
 }
 }
}

据我所知,必须创建一个自定义类,您可以在其中设置要调度的事件的要求:

package 
{
    import flash.display.MovieClip;

    public class TestClass extends MovieClip 
    {

        public function TestClass():void {


            if (ConditionForHoldToComplete == true) {

                dispatchEvent(new Event(CustomEvent.TEST));
            }

        }

    }

}

我不确定这是否正确,但应该与此类似。

现在我想要的是类似 mouseevent 的东西,它可以应用于目标并且不需要特定的类。

它必须像这样工作:

package com.op_pad._events{
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.EventDispatcher;
import flash.events.Event;

public class HoldEvent extends Event
     {
          public static const HOLD_COMPLETE:String = "hold completed";
          var timer:Timer;

          public function SpriteEvent(type:String, bubbles:Boolean=true, cancelable:Boolean=false)
          {
                super( type, bubbles, cancelable );

                timer = new Timer(1000, 1);
                //somehow find the target where is event is placed upon -> target.addEventlistener
                target.addEventListener(MouseEvent.MOUSE_DOWN, startTimer);
                target.addEventListener(MouseEvent.MOUSE_UP, stopTimer);
          }

          public override function clone():Event
          {
                return new SpriteEvent(type, bubbles, cancelable);
          }
          public override function toString():String
          {
                return formatToString("MovieEvent", "type", "bubbles", "cancelable", "eventPhase");
          }



            //////////////////////////////////
            /////   c o n d i t i o n s  /////
            //////////////////////////////////

            private function startTimer(e:MouseEvent):void 
          {
              timer.start();
              timer.addEventListener(TimerEvent.TIMER_COMPLETE, complete);
          }

          private function stopTimer(e:MouseEvent):void 
          {
              timer.stop()
          }

          public function complete(e:TimerEvent):void {

              dispatchEvent(new HoldEvent(HoldEvent.HOLD_COMPLETE));
          }
     }

}

这显然行不通,但应该让您了解我想要实现的目标。这应该是可能的,因为 mouseevent 可以应用于几乎所有事情。主要问题是我不知道应该在哪里设置要执行的事件的要求,以便能够将其应用于影片剪辑和精灵。

I've been looking into custom event (listeners) for quite some time, but never succeeded in making one. There are so many different methods, extending the Event class, but also Extending the EventDispatcher class, very confusing!
I want to settle with this once and for all and learn the appropriate technique.

package{

 import flash.events.Event;
 public class CustomEvent extends Event{

 public static const TEST:String = 'test'; //what exac is the purpose of the value in the string?
 public var data:Object;

 public function CustomEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false, data:Object = null):void
 {
 this.data = data;
 super();
 }
 }
}

As far as I know a custom class where you set the requirements for the event to be dispatched has to be made:

package 
{
    import flash.display.MovieClip;

    public class TestClass extends MovieClip 
    {

        public function TestClass():void {


            if (ConditionForHoldToComplete == true) {

                dispatchEvent(new Event(CustomEvent.TEST));
            }

        }

    }

}

I'm not sure if this is correct, but it should be something along the lines of this.

Now What I want is something like a mouseevent, which can be applied to a target and does not require a specific class.

It would have to work something like this:

package com.op_pad._events{
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.EventDispatcher;
import flash.events.Event;

public class HoldEvent extends Event
     {
          public static const HOLD_COMPLETE:String = "hold completed";
          var timer:Timer;

          public function SpriteEvent(type:String, bubbles:Boolean=true, cancelable:Boolean=false)
          {
                super( type, bubbles, cancelable );

                timer = new Timer(1000, 1);
                //somehow find the target where is event is placed upon -> target.addEventlistener
                target.addEventListener(MouseEvent.MOUSE_DOWN, startTimer);
                target.addEventListener(MouseEvent.MOUSE_UP, stopTimer);
          }

          public override function clone():Event
          {
                return new SpriteEvent(type, bubbles, cancelable);
          }
          public override function toString():String
          {
                return formatToString("MovieEvent", "type", "bubbles", "cancelable", "eventPhase");
          }



            //////////////////////////////////
            /////   c o n d i t i o n s  /////
            //////////////////////////////////

            private function startTimer(e:MouseEvent):void 
          {
              timer.start();
              timer.addEventListener(TimerEvent.TIMER_COMPLETE, complete);
          }

          private function stopTimer(e:MouseEvent):void 
          {
              timer.stop()
          }

          public function complete(e:TimerEvent):void {

              dispatchEvent(new HoldEvent(HoldEvent.HOLD_COMPLETE));
          }
     }

}

This obviously won't work, but should give you an idea of what I want to achieve. This should be possible because mouseevent can be applied to about everything.The main problem is that I don't know where I should set the requirements for the event to be executed to be able to apply it to movieclips and sprites.

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

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

发布评论

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

评论(2

沉默的熊 2024-10-16 15:22:14

实际上,您已经快到了,只是最后一部分,这不是更多的是与 OOP 相关的问题,而不是严格意义上对使用自定义事件的方式的混淆吗?

通常,AS3 中的事件是值对象,其唯一职责是将信息从事件调度程序传输到侦听器。每次达到定义的动量时,调度程序都会调度事件,并且侦听器在发生这种情况时可能会做出反应,也可能不会作出反应。

在上面的示例中,我想当检测到鼠标按下时,由侦听器启动计时器等。在更复杂的上下文中,事件可以独立触发多个侦听器执行单独的任务,调度程序和事件本身都不必担心,这可能就是为什么值得避免使用任何逻辑修改调度程序或事件本身。

对于您的示例,您可以创建一个处理程序来检查鼠标是否已被按住?

下面只是伪代码,显然还有很多其他方法可以达到相同的结果:

public class MouseDownHandler
{
    // ...

    public function( target:Sprite ) {
         this.target = target;
         start();
    }

    public function start():void{
        // Listen for the target's mouseUp event
    }

    public function dispose():void{
        // Stop listeners and eventually the timer
    }

    private function onMouseDown(e:MouseEvent):void{
       // Start timer + listening for the stage's mouse up event (target.stage)
    }

    private function onMouseUp(e:Event):void{
        // Cancel timer
    }

    private function onTimerComplete(e:TimerEvent):void {
        dispatchEvent(new HoldEvent(HoldEvent.HOLD_COMPLETE));
    }
}

可以重用,例如这样:

var mc:MovieClip = new MovieClip(); ...
var mouseHandler:MouseDownHandler = new MouseDownHandler(mc);
mouseHandler.addEventListener(HoldEvent.HOLD_COMPLETE, onMcHoldComplete);

...或这样:

public class TestMovieClip extends MovieClip
{
    private var mouseHandler:MouseDownHandler;

    public function TestMovieClip() {
        mouseHandler = new MouseDownHandler(this);
        mouseHandler.addEventListener(HoldEvent.HOLD_COMPLETE, onMouseHoldComplete);
    }

    private function onMouseHoldComplete(e:HoldEvent):void {
        // Do something
    }
}

You are almost there actually, just for the last part, wouldn't this be more of an OOP related issue than stricly a confusion about the way of using custom events ?

Usually, Events in AS3 are value objects whose sole responsibility is to transport information from the event dispatcher to the listener(s). The dispatcher dispatches the event each time a defined momentum is reached, and the listener(s) may or may not react when this happens.

In the example above, I guess it is up to the listener to start a timer and so on when a mouse-down has been detected. In a more sophisticated context the Event could independently trigger more than one listeners actioning separate tasks which neither the Dispatcher nor the Event itself should have to bother about, that is probably why it's worth avoiding amending the dispatcher or the event itself with any soever logic.

For your very example, you could maybe create a handler checking if the mouse has been held down?

The following is just pseudocode, and there are obviously tons of other ways to get to the same result:

public class MouseDownHandler
{
    // ...

    public function( target:Sprite ) {
         this.target = target;
         start();
    }

    public function start():void{
        // Listen for the target's mouseUp event
    }

    public function dispose():void{
        // Stop listeners and eventually the timer
    }

    private function onMouseDown(e:MouseEvent):void{
       // Start timer + listening for the stage's mouse up event (target.stage)
    }

    private function onMouseUp(e:Event):void{
        // Cancel timer
    }

    private function onTimerComplete(e:TimerEvent):void {
        dispatchEvent(new HoldEvent(HoldEvent.HOLD_COMPLETE));
    }
}

Which could be reused for example this way:

var mc:MovieClip = new MovieClip(); ...
var mouseHandler:MouseDownHandler = new MouseDownHandler(mc);
mouseHandler.addEventListener(HoldEvent.HOLD_COMPLETE, onMcHoldComplete);

... or this way :

public class TestMovieClip extends MovieClip
{
    private var mouseHandler:MouseDownHandler;

    public function TestMovieClip() {
        mouseHandler = new MouseDownHandler(this);
        mouseHandler.addEventListener(HoldEvent.HOLD_COMPLETE, onMouseHoldComplete);
    }

    private function onMouseHoldComplete(e:HoldEvent):void {
        // Do something
    }
}
格子衫的從容 2024-10-16 15:22:14

我只是使用强盗彭纳斯信号。非常容易使用。

http://github.com/robertpenner/as3-signals

I just use robber penners signals. Very easy to use.

http://github.com/robertpenner/as3-signals

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