Actionscript 3 [事件(名称=“;”;)]:如何使用它以及自定义事件如何工作?

Actionscript 3 [事件(名称=“;”;)]:如何使用它以及自定义事件如何工作?,actionscript-3,flash,flash-builder,custom-events,Actionscript 3,Flash,Flash Builder,Custom Events,我必须做一个别人开始的项目,但不能联系他,因为他现在不在国外。无论如何有一个主mxml和一个名为“admin”的自定义组件 在admin中,他宣布了如下事件: <fx:Metadata> [Event(name="sluitFrame")] </fx:Metadata> dispatchEvent(new Event("sluitFrame")); [Event(name="MyCustomEvent.closeWindow", type="com.w

我必须做一个别人开始的项目,但不能联系他,因为他现在不在国外。无论如何有一个主mxml和一个名为“admin”的自定义组件

在admin中,他宣布了如下事件:

    <fx:Metadata>
    [Event(name="sluitFrame")]
</fx:Metadata>
dispatchEvent(new Event("sluitFrame"));
[Event(name="MyCustomEvent.closeWindow", type="com.website.events.MyCustomEvent")]
此事件sluitFrame使用一些管理工具关闭框架。我需要改变它的工作方式,以便找到相应的代码。主mxml上有以下代码:

                <comp:Admin id="compAdmin" creationPolicy="none"
                         sluitFrame="verbergAdminComponent(event)"/>

所以,如果我理解正确,sluitFrame调用一个定制的甚至称为“verbergAdminComponent(event)”的组件。所以我想我需要这个事件来改变关闭管理框架的方式,等等。但是这个事件没有找到。所以我不明白“verbergAdminComponent(event)”是如何工作的,也不知道在哪里可以对这个事件进行更改


非常欢迎并非常需要任何帮助:)

verbergAdminComponent将是事件处理程序的名称。它应该是mxml、included.as或该mxml的基类中的方法。

verbergAdminComponent将是事件处理程序的名称。它应该是mxml、included.as或该mxml的基类中的一个方法。

[Event…
行只让编译器和IDE知道组件/类(在本例中,
Admin
)可以按该名称发送事件。这很重要,因为当有人在MXML标记中声明Admin实例时,编译器知道该事件(在本例中,
sluitFrame
)是有效属性。换句话说,它让编译器知道可以在MXML标记中设置事件侦听器。在您的情况下,每次
Admin
对象分派
sluitFrame
事件时,都会调用函数
verbergAdminComponent
,并将sluitFrame事件传递给它。

[Event…
行只是让编译器和IDE知道组件/类(在本例中,
Admin
)可能会以该名称发送事件。这一点很重要,因为当有人在MXML标记中声明Admin实例时,编译器知道该事件(在本例中,
sluitFrame
)是有效属性。换句话说,它让编译器知道可以在MXML标记中设置事件侦听器。在您的情况下,每次
Admin
对象分派
sluitFrame
事件时,都会调用函数
verbergAdminComponent
,并将sluitFrame事件传递给它。

在事件中键入 如果您在Flash中使用
事件
,您可以分派任何命名类型,因为它是字符串。因此,无论您如何调用它,只要侦听器侦听的是完全相同的类型。这就是它工作的原因。没有魔法进化。但是,在这种情况下,我会选择使用自定义事件

自定义事件如何工作 看一看自定义事件的工作原理,这样您就可以了解元数据的作用

package com.website.events
{
    import flash.events.Event;

    /**
     * @author ExampleUser
    */
    public class MyCustomEvent extends Event
    {
        /** 
         * 
         */
        public static const CLOSE_WINDOW:String = "MyCustomEvent.closeWindow";


        public function MyCustomEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false):void 
        { 
            super(type, bubbles, cancelable);
        }

        override public function clone():Event 
        { 
            return new MyCustomEvent(this.type, this.bubbles, this.cancelable);
        } 

        override public function toString():String 
        { 
            return formatToString("MyCustomEvent", "type", "bubbles", "cancelable", "eventPhase"); 
        }
    }
 }
我使用静态常量而不是直接使用字符串(如您的示例),因此它的类型更严格

在actionscript中,您可以像这样分派自定义事件。假设您创建了一个名为Window的类

package com.website.ui
{
   /**
    * @eventType com.website.events.MyCustomEvent.closeWindow
   */
   [Event(name="MyCustomEvent.closeWindow", type="com.website.events.MyCustomEvent")]

import flash.display.Sprite;
import flash.events.MouseEvent;
import com.website.events.MyCustomEvent;

/**
 * @author ExampleUser
*/
public class Window extends Sprite
{
    public var mcCloseButton:Sprite;

    public function Window():void 
    { 
        this.mcCloseButton.addEventListener(MouseEvent.CLICK, handleCloseButtonClick);
    }

    private function handleCloseButtonClick(event:MouseEvent):void 
    { 
        this.dispatchEvent(new MyCustomEvent(MyCustomEvent.CLOSE_WINDOW));
    }

}
}
这是元数据应该位于的类。此类正在调度事件。其他调度同一事件的类也可能具有元数据

因此,当用户单击“关闭”按钮时,Windows正在调度类型为
CLOSE\u Window
的事件。在另一个文件中,您将收听该事件并对其进行处理

package com.website
{
import flash.display.Sprite;
import com.website.events.MyCustomEvent;
import com.website.ui.Window;

/**
 * @author ExampleUser
*/
public class Main extends Sprite
{
    private var _window:Window;

    public function Main():void 
    { 
        this._window:Window = new Window();
                    // a smart code-editor would give you a hint about the possible events when you typed "addEventListener" 
        this._window.addEventListener(MyCustomEvent.CLOSE_WINDOW, handleWindowClosed);
        this.addChild(this._window);
    }

    private function handleWindowClosed(event:MyCustomEvent):void 
    { 
        // do something
        this._window.visible = false;
    }
}
}
这应该行得通。 当然,在现实世界中,
MyCustomEvent
将被命名为
WindowEvent

事件元数据 元数据可以用来给编译器提供提示,现在的智能代码编辑器(FDT、FlashDevelop、FlashBuilder、IntelliJ等)可以完成代码。它基本上是描述类可以调度什么类型的事件,因此您知道可以使用什么侦听器

即使元数据被删除,代码也应该工作

事件元有一个
名称
和一个
类型
。名称应该是类型的确切值。在我们的示例中,它应该是
CLOSE\u WINDOW
的值,因此这就是
MyCustomEvent.closeWindow
。 类型应该是包含完整包的类名,在我们的示例中,它应该是“com.website.events.MyCustomEvent”

最后,元数据如下所示:

    <fx:Metadata>
    [Event(name="sluitFrame")]
</fx:Metadata>
dispatchEvent(new Event("sluitFrame"));
[Event(name="MyCustomEvent.closeWindow", type="com.website.events.MyCustomEvent")]
顺便说一句,我对你的代码有一些建议:

  • 我建议使用英语函数名和参数,而不是荷兰语
  • verbergAdminComponent
    不是处理程序的好名字,它应该类似于
    handleCloseWindow(event)
    ,它应该调用
    verbergAdminComponent
    函数
事件中的类型 如果您在Flash中使用
事件
,您可以分派任何命名类型,因为它是字符串。因此,无论您如何调用它,只要侦听器侦听的是完全相同的类型。这就是它工作的原因。没有魔法进化。但是,在这种情况下,我会选择使用自定义事件

自定义事件如何工作 看一看自定义事件的工作原理,这样您就可以了解元数据的作用

package com.website.events
{
    import flash.events.Event;

    /**
     * @author ExampleUser
    */
    public class MyCustomEvent extends Event
    {
        /** 
         * 
         */
        public static const CLOSE_WINDOW:String = "MyCustomEvent.closeWindow";


        public function MyCustomEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false):void 
        { 
            super(type, bubbles, cancelable);
        }

        override public function clone():Event 
        { 
            return new MyCustomEvent(this.type, this.bubbles, this.cancelable);
        } 

        override public function toString():String 
        { 
            return formatToString("MyCustomEvent", "type", "bubbles", "cancelable", "eventPhase"); 
        }
    }
 }
我使用静态常量而不是直接使用字符串(如您的示例),因此它的类型更严格

在actionscript中,您可以像这样分派自定义事件。假设您创建了一个名为Window的类

package com.website.ui
{
   /**
    * @eventType com.website.events.MyCustomEvent.closeWindow
   */
   [Event(name="MyCustomEvent.closeWindow", type="com.website.events.MyCustomEvent")]

import flash.display.Sprite;
import flash.events.MouseEvent;
import com.website.events.MyCustomEvent;

/**
 * @author ExampleUser
*/
public class Window extends Sprite
{
    public var mcCloseButton:Sprite;

    public function Window():void 
    { 
        this.mcCloseButton.addEventListener(MouseEvent.CLICK, handleCloseButtonClick);
    }

    private function handleCloseButtonClick(event:MouseEvent):void 
    { 
        this.dispatchEvent(new MyCustomEvent(MyCustomEvent.CLOSE_WINDOW));
    }

}
}
这是元数据应该位于的类。此类正在调度事件。其他调度同一事件的类也可能具有元数据

因此,当用户单击“关闭”按钮时,Windows正在调度类型为
CLOSE\u Window
的事件。在另一个文件中,您将收听该事件并对其进行处理

package com.website
{
import flash.display.Sprite;
import com.website.events.MyCustomEvent;
import com.website.ui.Window;

/**
 * @author ExampleUser
*/
public class Main extends Sprite
{
    private var _window:Window;

    public function Main():void 
    { 
        this._window:Window = new Window();
                    // a smart code-editor would give you a hint about the possible events when you typed "addEventListener" 
        this._window.addEventListener(MyCustomEvent.CLOSE_WINDOW, handleWindowClosed);
        this.addChild(this._window);
    }

    private function handleWindowClosed(event:MyCustomEvent):void 
    { 
        // do something
        this._window.visible = false;
    }
}
}
这应该行得通。 当然,在现实世界中,
MyCustomEvent
将被命名为
WindowEvent