如何将EventListener 添加到自定义动作脚本类?

发布于 2024-12-04 01:16:31 字数 1428 浏览 1 评论 0原文

我有一个扩展 NavigatorContent 的动作脚本类 MyClass。我将该类实例化为 Accordion 组件的自定义 MXML NavigatorContnent 组件。 MyClass 有一个 Button 组件,我尝试将事件侦听器附加到该组件。我希望事件冒泡,以便我可以在 Accordion 组件中拥有处理程序。

MyClass

package comp
{

  import flash.events.Event;
  import flash.events.MouseEvent;

  [Event(name="selectEvent", type="flash.events.Event")]

  public class MyClass extends NavigatorContent
  {

    public function MyClass()
    {
      super();
      btnSelect.addEventListener(MouseEvent.CLICK, selectClickDispatcher);
    }


    public function selectClickDispatcher(event:MouseEvent):void
    {
      event.currentTarget.dispatchEvent(new Event("selectEvent",true));
    }
  }
}

从这里我将实例化的组件嵌套在 Accordion 中。我非常确定问题出在此类定义中,因为当我在 selectClickHandler 处设置断点时,代码不会中断。如果我错了,我将发布其余的组件。

名为 MySubComp.mxml 的自定义组件

<comp:MyClass
    ...I have a few more spark components here and nothing else...
/>

Accordion

<mx:Accordion>

    <fx:Script> //omitted CDATA tags to save space
        protected function selectEventHandler(event:Event):void
        {
            Alert.show("Value Selected");   
        }
    </fx:Script>

        //custom components are in the navs package
        <navs:MySubComp selectEvent = "selectEventHandler(event)"/>

</mx:Accordion>

I have an actionscript class MyClass that extens NavigatorContent. I instantiate the class as a custom MXML NavigatorContnent component for an Accordion component. MyClass has a Button component that I have tried to attach an event listener to. I want the event to bubble so that I can have the handler in the Accordion component.

MyClass

package comp
{

  import flash.events.Event;
  import flash.events.MouseEvent;

  [Event(name="selectEvent", type="flash.events.Event")]

  public class MyClass extends NavigatorContent
  {

    public function MyClass()
    {
      super();
      btnSelect.addEventListener(MouseEvent.CLICK, selectClickDispatcher);
    }


    public function selectClickDispatcher(event:MouseEvent):void
    {
      event.currentTarget.dispatchEvent(new Event("selectEvent",true));
    }
  }
}

From here I have the instantiated component nested in the Accordion. I am pretty sure the problem is in this class definition because when I set a breakpoint at the selectClickHandler, the code does not break. In case I am wrong I will post the rest of the components.

Custom component named MySubComp.mxml

<comp:MyClass
    ...I have a few more spark components here and nothing else...
/>

Accordion

<mx:Accordion>

    <fx:Script> //omitted CDATA tags to save space
        protected function selectEventHandler(event:Event):void
        {
            Alert.show("Value Selected");   
        }
    </fx:Script>

        //custom components are in the navs package
        <navs:MySubComp selectEvent = "selectEventHandler(event)"/>

</mx:Accordion>

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

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

发布评论

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

评论(3

和影子一齐双人舞 2024-12-11 01:16:32

您的类需要扩展 DisplayObject 类,或者直接从 EventDispatcher 继承,以便能够使用事件。忘记实现 IEventDispatcher 吧,因为某处有一段特殊的黑色代码,这意味着 EventDispatcher 是唯一可以设置 target 属性的类。 Event 类(我之前尝试过)。

考虑使用其他替代方案。 Flash 中的事件往往很慢并且一直在创建对象。如果您需要简单的东西,回调是一个很好的系统。

public class A
{
    public var onSomething:Function = null;
    public function foo():void
    {
        if( this.onSomething != null )
            this.onSomething();
    }
}

public class B
{
    public function B()
    {
        var a:A = new A;
        a.onSomething = this._somethingCalled; // set the callback
        a.init();
    }

    private function _somethingCalled():void
    {
        trace( "hello there" );
    }
}

您还可以查看 Signals 项目: https://github.com/robertpenner/as3- Signals/wiki

信号远远优于普通的 Flash 事件,并且对可以使用它们的对象类型没有限制(即非 DisplayObject 对象仍然可以添加事件侦听器和分派事件)。它也更快并且内存占用更小。

Your class either needs to extend a DisplayObject class, or directly inherit from EventDispatcher in order to be able to use events. Forget about implementing IEventDispatcher as there's a special piece of black code somewhere that means that EventDispatcher is the only class that can set the target property of the Event class (I've tried it before).

Consider using other alternatives. Events in Flash tend to be slow and create objects all the time. Callbacks are a good system if you need something simple.

public class A
{
    public var onSomething:Function = null;
    public function foo():void
    {
        if( this.onSomething != null )
            this.onSomething();
    }
}

public class B
{
    public function B()
    {
        var a:A = new A;
        a.onSomething = this._somethingCalled; // set the callback
        a.init();
    }

    private function _somethingCalled():void
    {
        trace( "hello there" );
    }
}

You can also take a look at the Signals project: https://github.com/robertpenner/as3-signals/wiki

Signals are vastly superior to normal Flash events, and there's no restriction on the type of object that can use them (i.e. non-DisplayObject objects can still add event listeners and dispatch events). It's also faster and has a smaller memory footprint.

鱼忆七猫命九 2024-12-11 01:16:32

如果有人需要使用真正的 Actionscript-3 事件调度>>> 这个 <<<非常有帮助。我不知道它是否真的很慢,但它符合 AS-3 标准。

In case someone needs the real Actionscript-3 event dispatching to be used >>> this <<< is very helpful. I don't know if it is really slow but it meets the AS-3 standards.

仅冇旳回忆 2024-12-11 01:16:31

您已将元数据添加到类定义中

[Event(name="selectEvent", type="flash.events.Event")]

,因此您需要在 mxml 中做的就是

<comp:MyClass selectEvent="event_handler(event)"
..... />

添加事件侦听器

myClass.addEventListener("selectEvent", event_handler);

在 AS3 中,通过P.S 。你的类必须扩展 EventDispatcher

You have added the metadata to the class definition

[Event(name="selectEvent", type="flash.events.Event")]

so all you need to do in mxml is

<comp:MyClass selectEvent="event_handler(event)"
..... />

In AS3, you add an event listener by

myClass.addEventListener("selectEvent", event_handler);

P.S. Your class will have to extend EventDispatcher

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