AS3 自定义对话框:如何将按下的按钮标签发送到 Main.fla
AS3 Custom DialogBox: How to send pushed Buttons label to Main.fla
我制作了自己的 AS3-DialogBox-class。它有 2 个按钮("Yes" 和 "No")
在 class 中有一个侦听器,它在按下其中一个按钮时启动一个函数。
在这个侦听器函数中,我可以通过调用 event.currentTarget.MyButtonTextfield.text.
来获取和跟踪按钮标签("Yes" 或 "No")
所以我的问题是:
如何从 classes 侦听器函数中 RETURN 这些推送值("Yes" 或 "No")到 Main.fla ?
因为在 MAIN.fla 中,我想将 DialogBox 的调用放在 IF-ELSE-Statement 中,它应该 "catch" 推送的 "Yes" 或 "No" .
做这样的事情的官方方法是什么?
希望你能帮帮我!
提前致谢!
小齿
- 创建 class
DialogEvent extends Event
.
- 添加
private var _choice:String = "";
- 添加
public function get choice():String { return _choice; }
从对象中检索值
- 复制
Event
class 的构造函数,然后添加采用所选值的第一个参数 public function DialogEvent (choice:String,....
- 在构造函数中设置
_choice = choice;
除了用其余参数调用super()
- 为方便起见添加
public static const CHOICE:String = "choice";
,这是可选的。它有助于在编译时检查代码中的拼写错误。
- 在
DialogBox
按下其中一个按钮时
dispatchEvent(new DialogEvent ("replace this with the button text",
DialogEvent.CHOICE));
发送事件
- 在main.fla(建议:文件名不要全大写)等待
即将发生的事件
dialogBox.addEventListener(DialogEvent.CHOICE,
onDialogChange);
- 在
private function onDialogChange(event:DialogEvent):void
中尝试
trace(event.choice);
- 添加
public override function clone():Event
调用您的
修改构造函数以考虑您的附加变量
当克隆您的自定义事件对象时,如
documentation of the clone()
method
在这种情况下,对话框会调度自定义事件。您也可以在上一级执行此操作,让每个带标签的按钮分派一个自定义事件。
关于评论中的问题:
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?
这是"duplicate"的本意。
您要查找的位置是文档。 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)
为了说明这一点:您不必像这样复制构造函数。您可以为参数选择其他名称或将其省略。但是,建议复制构造函数,因为您想像使用任何其他事件一样使用自定义事件并添加一些其他参数(选择)。
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?
常数是可选的。然而,在内置的 Event classes 中使用常量是很常见的,例如the constants defined by MouseEvent
:
CLICK : String = "click"
CONTEXT_MENU : String = "contextMenu"
DOUBLE_CLICK : String = "doubleClick"
使用它们的原因是允许在编译时检查事件类型。
比较这两个对构造函数的调用:
new DialogEvent ("replace this with the button text",
DialogEvent.CHOICE));
new DialogEvent ("replace this with the button text",
"chojce"));
两者都编译,只有第一个按预期工作,前提是您正在侦听 "choice" 类型的事件。如果以 String 文字给出,则无法检查拼写错误的类型。 class 的属性可以在编译时检查:
new DialogEvent ("replace this with the button text",
DialogEvent.CHOJCE)); // throws an error
此外,如果您使用支持它的编辑器,它还允许代码完成。
为了完整起见:
Shall I just create the constructor like: public function DialogEvent (choice:String, receivedEvent:Event) { _choice = choice; super(receivedEvent); }
如您在文档中所见,Event
class 的构造函数不采用 Event
类型的单个参数。此代码无法编译。
我制作了自己的 AS3-DialogBox-class。它有 2 个按钮("Yes" 和 "No")
在 class 中有一个侦听器,它在按下其中一个按钮时启动一个函数。
在这个侦听器函数中,我可以通过调用 event.currentTarget.MyButtonTextfield.text.
来获取和跟踪按钮标签("Yes" 或 "No")所以我的问题是:
如何从 classes 侦听器函数中 RETURN 这些推送值("Yes" 或 "No")到 Main.fla ?
因为在 MAIN.fla 中,我想将 DialogBox 的调用放在 IF-ELSE-Statement 中,它应该 "catch" 推送的 "Yes" 或 "No" .
做这样的事情的官方方法是什么?
希望你能帮帮我! 提前致谢!
小齿
- 创建 class
DialogEvent extends Event
. - 添加
private var _choice:String = "";
- 添加
public function get choice():String { return _choice; }
从对象中检索值 - 复制
Event
class 的构造函数,然后添加采用所选值的第一个参数public function DialogEvent (choice:String,....
- 在构造函数中设置
_choice = choice;
除了用其余参数调用super()
- 为方便起见添加
public static const CHOICE:String = "choice";
,这是可选的。它有助于在编译时检查代码中的拼写错误。 - 在
DialogBox
按下其中一个按钮时dispatchEvent(new DialogEvent ("replace this with the button text", DialogEvent.CHOICE));
发送事件 - 在main.fla(建议:文件名不要全大写)等待
即将发生的事件
dialogBox.addEventListener(DialogEvent.CHOICE, onDialogChange);
- 在
private function onDialogChange(event:DialogEvent):void
中尝试trace(event.choice);
- 添加
public override function clone():Event
调用您的 修改构造函数以考虑您的附加变量 当克隆您的自定义事件对象时,如 documentation of theclone()
method
在这种情况下,对话框会调度自定义事件。您也可以在上一级执行此操作,让每个带标签的按钮分派一个自定义事件。
关于评论中的问题:
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?
这是"duplicate"的本意。 您要查找的位置是文档。 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)
为了说明这一点:您不必像这样复制构造函数。您可以为参数选择其他名称或将其省略。但是,建议复制构造函数,因为您想像使用任何其他事件一样使用自定义事件并添加一些其他参数(选择)。
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?
常数是可选的。然而,在内置的 Event classes 中使用常量是很常见的,例如the constants defined by MouseEvent
:
CLICK : String = "click"
CONTEXT_MENU : String = "contextMenu"
DOUBLE_CLICK : String = "doubleClick"
使用它们的原因是允许在编译时检查事件类型。 比较这两个对构造函数的调用:
new DialogEvent ("replace this with the button text",
DialogEvent.CHOICE));
new DialogEvent ("replace this with the button text",
"chojce"));
两者都编译,只有第一个按预期工作,前提是您正在侦听 "choice" 类型的事件。如果以 String 文字给出,则无法检查拼写错误的类型。 class 的属性可以在编译时检查:
new DialogEvent ("replace this with the button text",
DialogEvent.CHOJCE)); // throws an error
此外,如果您使用支持它的编辑器,它还允许代码完成。
为了完整起见:
Shall I just create the constructor like:
public function DialogEvent (choice:String, receivedEvent:Event) { _choice = choice; super(receivedEvent); }
如您在文档中所见,Event
class 的构造函数不采用 Event
类型的单个参数。此代码无法编译。