AS3 自定义对话框:如何将推送按钮标签发送到 Main.fla [英] AS3 Custom DialogBox: How to send pushed Buttons label to Main.fla

查看:24
本文介绍了AS3 自定义对话框:如何将推送按钮标签发送到 Main.fla的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了自己的 AS3-DialogBox 类.它有 2 个按钮(是"和否")

在类中有一个监听器,它在按下某个按钮时启动一个函数.

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

所以我的问题是:

如何将这些推送的值(是"或否")从类侦听器函数中返回到 Main.fla?

因为在 MAIN.fla 中,我想将 DialogBox 的调用放在 IF-ELSE-Statement 中,这将捕捉"推送的是"或否".

做这种事情的官方方法是什么?

我希望你能帮助我!提前致谢!

丁丁

解决方案

  1. 创建一个类DialogEvent extends Event.
  2. 添加 private var _choice:String = "";
  3. 添加public function getchoice():String { return _choice;} 从对象中检索值
  4. 复制 Event 类的构造函数,然后添加第一个参数,该参数采用所选值 public function DialogEvent (choice:String,....
  5. 在构造函数中设置 _choice = choice; 除了调用 super() 其余参数
  6. 为方便起见,添加 public static const CHOICE:String = "choice";,这是可选的.它有助于在编译时检查代码中的拼写错误.
  7. DialogBox 当其中一个按钮被按下时dispatchEvent(new DialogEvent("用按钮文本替换这个",DialogEvent.CHOICE)); 调度事件
  8. 在 main.fla 中(建议:文件名不要全部大写)等待要发生的事件 dialogBox.addEventListener(DialogEvent.CHOICE,onDialogChange);
  9. 私有函数onDialogChange(event:DialogEvent):void中,试试trace(event.choice);
  10. 添加 public override function clone():Event 调用你的修改构造函数以考虑您的附加变量克隆自定义事件对象时,如clone() 方法

<小时>

在这种情况下,对话框调度自定义事件.您也可以在上一级执行此操作,并让每个带标签的按钮调度一个自定义事件.

<小时>

关于评论中的问题:

<块引用>

[第 4 步中的重复"一词是什么意思?...我有一个可以看到和复制"(=复制)As3自己的"事件类代码的位置吗?

这是重复"的预期含义.您要查找的位置是文档.看看Event类的构造函数文档:

<块引用>

事件()构造函数

公共函数事件(类型:字符串,气泡:布尔=假,可取消:布尔=假)

为了明确这一点:您不必像这样复制构造函数.您可以为参数选择其他名称或不使用它们.但是,建议复制构造函数,因为您希望像使用任何其他事件一样使用自定义事件,并添加一些其他参数(选择).

public function DialogEvent(choice:String, type:String, bubbles:Boolean = false, cancelable:Boolean = false){超级(类型,气泡,可取消);_choice = 选择;

<小时><块引用>

声明这个常量的原因是什么.没有这个常量就不行吗?

常量是可选的.但是,在内置的 Event 类中通常有类似的常量,例如MouseEvent:

<块引用>

点击 : String = "点击"

CONTEXT_MENU : String = "contextMenu"

DOUBLE_CLICK : String = "doubleClick"

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

new DialogEvent ("用按钮文本替换它",DialogEvent.CHOICE));new DialogEvent ("用按钮文本替换它",选择"));

如果您正在侦听选择"类型的事件,则两者都可以编译,只有第一个按预期工作.如果作为字符串文字给出,则无法检查拼写错误的类型.但是可以在编译时检查类的属性:

new DialogEvent ("用按钮文本替换它",DialogEvent.CHOJCE));//抛出错误

此外,如果您使用支持它的编辑器,它还允许代码完成.

<小时>

只是为了完整性:

<块引用>

我是否应该像这样创建构造函数: public function DialogEvent (choice:String, receivedEvent:Event) { _choice = choice;超级(接收事件);}

正如您在文档中看到的,Event 类的构造函数不采用 Event 类型的单个参数.此代码无法编译.

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

解决方案

  1. Create a class DialogEvent extends Event.
  2. Add private var _choice:String = "";
  3. Add public function get choice():String { return _choice; } to retrieve the value from the object
  4. Duplicate the constructor of the Event class, then add a first parameter that takes the chosen value public function DialogEvent (choice:String,....
  5. Set _choice = choice; in the constructor in addition to calling the super() with the rest of the parameters
  6. Add public static const CHOICE:String = "choice"; for convenience, this is optional. It helps checking for typos in the code at compile time.
  7. In DialogBox when one of the Buttons was pushed dispatchEvent(new DialogEvent ("replace this with the button text", DialogEvent.CHOICE)); to dispatch the event
  8. In main.fla (suggestion: do not use all caps file names) wait for the event to happen dialogBox.addEventListener(DialogEvent.CHOICE, onDialogChange);
  9. In private function onDialogChange(event:DialogEvent):void, try trace(event.choice);
  10. 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.

这篇关于AS3 自定义对话框:如何将推送按钮标签发送到 Main.fla的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆