AS3自定义DialogBox的:如何发送推按钮标签Main.fla自定义、按钮、标签、DialogBox

2023-09-09 21:40:23 作者:作業是青春的墳墓

我做我自己的AS3-DialogBox的类。它有2个按钮(是和否)

在类中存在着启动时的一个按钮被按下一个功能的监听器。

和这个监听器功能中,我可以通过调用event.currentTarget.MyButtonTextfield.text获取和跟踪按钮标签(是或否)。

所以我的问题是:

我怎样才能返回这些推值(是或否)从类侦听器函数内的Main.fla?

由于MAIN.fla内我要把DialogBox的号召在IF-ELSE-声明,应抓住推是或否。

什么是官方的方式做这样的事情?

我希望你能帮助我! 在此先感谢!

解决方案 创建一个类 DialogEvent扩展事件。 添加私人变种_choice:字符串=; 添加公共职能得到选择():字符串{返回_choice; } 检索对象价值 复制的事件类的构造函数,然后再添加需要选择的值公共职能DialogEvent(选择第一个参数:字符串,.. .. 设置 _choice =选择; 中除了调用超()用的,其余的构造参数 添加公共静态常量的选择:字符串=选择; 为了方便,这是可选的。它有助于检查在code在编译时错别字。 在 DialogBox的的当其中一个按钮被按下的 则dispatchEvent(新DialogEvent(与按钮文本替换, DialogEvent.CHOICE)); 指派该事件 在main.fla(建议:不要使用全部大写的文件名)等 事件发生 dialogBox.addEventListener(DialogEvent.CHOICE, onDialogChange); 私有函数onDialogChange(事件:DialogEvent):无效,尝试 跟踪(event.choice); 添加公众覆盖功能的clone():事件调用您  修改后的构造函数把你的额外的变量考虑在内  克隆您的自定义事件对象时所显现出的  的克隆文档()方法

在这种情况下,对话框将调度定义事件。您也可以做到这一点的previous水平,让每个标签按钮发送自定义事件。

对于在注释中的问题:

  

你怎么用这个词重复[在第4步是什么意思? ... I]那里有一个位置,我可以看到和复制(=副本)AS3的自己的事件类code?

Dialog Box Assistant Dialog Box Assistant 文件管理软件 v2.0汉化绿色版 ucbug下载站

这是重复的本意。 你正在寻找的位置的文档。 看看事件类的构造函数的文档:

  

事件()构造

     

公共职能事件(类型:字符串,气泡:布尔=假,取消:布尔= FALSE)

要明确这一点:你不必重复这样的构造。您可以选择其他名称的参数或离开他们。但是,建议复制构造函数,因为你想用你的自定义事件像任何其他事件增加一些其他的参数(选择)的。

 公共职能DialogEvent(选择:字符串,键入:字符串,气泡:布尔=假,取消:布尔= FALSE)
{
    超(类型,泡沫,取消);
    _choice =选择;
 

  

什么是宣告这个常数的原因。不就没有这种持续的工作?

常数是可选的。然而,这是常见的有常量这样之间内置的事件类,例如按的MouseEvent 定义的常量:

  

请:字符串=点击

     

CONTEXT_MENU :字符串=文本菜单

     

DOUBLE_CLICK :字符串=双击

使用它们的原因是为了允许在编译时检查的事件类型。 比较这两种调用构造函数:

 新DialogEvent(与按钮文本替换,
    DialogEvent.CHOICE));

新DialogEvent(与按钮文本替换,
    chojce));
 

这两种编译,只有第一个按预期工作,因为你正在听的类型选择的事件。有没有办法来检查拼写错误的类型,如果它是作为一个字符串文字。一类的属性但是可以在编译时检查:

 新DialogEvent(与按钮文本替换,
    DialogEvent.CHOJCE)); //抛出一个错误
 

此外,它允许code完成后,如果您使用的是支持它的编辑器。

只是为了完整性:

  

难道我只是创造了构造函数,如:公共职能DialogEvent(选择:字符串,receivedEvent:事件){_choice =选择;超(receivedEvent); }

正如你可以在文档中看到的,事件类的构造函数并不需要一个单一类型的参数事件。这code不能编译。

I made my own AS3-DialogBox-class. It has 2 Buttons ("Yes" and "No")

Within the class there is a listener that starts a function when one of the Buttons was pushed.

And within this listener-function I can get and trace the Buttons label ("Yes" or "No") by calling event.currentTarget.MyButtonTextfield.text.

So my question is:

How can I RETURN these pushed values ("Yes" or "No") from within the classes listener-function to the Main.fla ?

Because within the MAIN.fla I want to put the call of the DialogBox in an IF-ELSE-Statement, which shall "catch" the pushed "Yes" or "No".

What is the official way to do something like that?

I hope you can help me! Thanks in advance!

Tine

解决方案

Create a class DialogEvent extends Event. Add private var _choice:String = ""; Add public function get choice():String { return _choice; } to retrieve the value from the object Duplicate the constructor of the Event class, then add a first parameter that takes the chosen value public function DialogEvent (choice:String,.... Set _choice = choice; in the constructor in addition to calling the super() with the rest of the parameters Add public static const CHOICE:String = "choice"; for convenience, this is optional. It helps checking for typos in the code at compile time. In DialogBox when one of the Buttons was pushed dispatchEvent(new DialogEvent ("replace this with the button text", DialogEvent.CHOICE)); to dispatch the event In main.fla (suggestion: do not use all caps file names) wait for the event to happen dialogBox.addEventListener(DialogEvent.CHOICE, onDialogChange); In private function onDialogChange(event:DialogEvent):void, try trace(event.choice); Add public override function clone():Event that calls your modified constructor to take your additional variable into account when cloning your custom event object as demonstrated in the documentation of the clone() method

In this case the dialog dispatches the custom event. You can also do this on the previous level and let each labeled button dispatch a custom event.

Regarding the questions in the comment:

What do you mean with the word "duplicate" [in step 4.? ... I]s there a location where i can see and "copy" (= duplicate) the code of As3's "own" Event-class?

This is the intended meaning of "duplicate". The location you are looking for is the documentation. Take a look at the documentation of the constructor of the Event class:

Event () Constructor

public function Event(type:String, bubbles:Boolean = false, cancelable:Boolean = false)

To make this clear: you do not have to duplicate the constructor like this. You can choose other names for the parameters or leave them out. However, it is recommended to duplicate the constructor, because you want to use your custom event like any other event with the addition of some other parameter (the choice).

public function DialogEvent(choice:String, type:String, bubbles:Boolean = false, cancelable:Boolean = false)
{
    super(type, bubbles, cancelable);
    _choice = choice;

What is the reason for declaring this constant. Doesn't it work WITHOUT this constant?

The constant is optional. However, it is common to have constants like that among the built in Event classes, e.g. the constants defined by MouseEvent:

CLICK : String = "click"

CONTEXT_MENU : String = "contextMenu"

DOUBLE_CLICK : String = "doubleClick"

The reason to use them is to allow checking the event types at compile time. Compare these two calls to the constructor:

new DialogEvent ("replace this with the button text",
    DialogEvent.CHOICE));

new DialogEvent ("replace this with the button text",
    "chojce"));

Both compile, only the first works as intended, given that you are listening for an event of type "choice". There's no way to check for a wrong spelled type if it is given as a String literal. Properties of a class however can be checked at compile time:

new DialogEvent ("replace this with the button text",
    DialogEvent.CHOJCE)); // throws an error

Additionally, it allows for code completion if you are using an editor that supports it.

Just for completeness:

Shall I just create the constructor like: public function DialogEvent (choice:String, receivedEvent:Event) { _choice = choice; super(receivedEvent); }

As you can see in the documentation, the constructor of the Event class does not take a single argument of type Event. this code does not compile.