我正在尝试在as3中编写一个简单的通知类,它只是两个按钮,一个用于“确定”,一个用于“取消”,因此每个按钮都有一个事件侦听器。
问题是我希望将变量从主类传递给通知类,然后在用户触发了其中一个eventListener之后将其返回给主类。
我尝试了不同的方法,但似乎没有人能奏效,我敢肯定有一种通用且简单的方法可以做到这一点,但是我是该语言的新手。
谢谢,对不起我的英语。
这是一个例子:
Main.as:
package {
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite {
[SWF(width="640",height="480",backgroundColor="#ffffff")]
public function Main():void {
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
var msg:Notification = new Notification();
addChild(msg);
}
}
}
Notification.as:
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormatAlign;
import flash.text.TextFieldAutoSize;
public class Notification extends Sprite {
private var background:Sprite;
private var button1:Sprite;
private var button2:Sprite;
private var buttonOk:TextField;
private var buttonCancel:TextField;
public function Notification() {
addBackground();
addMessage();
addOkButton();
addCancelButton();
}
private function addBackground():void {
background = new Sprite();
background.graphics.beginFill(0x4682b4, 1);
background.graphics.drawRoundRect(0, 0, 300, 200, 30, 30);
background.graphics.endFill();
addChild(background);
}
private function addMessage():void {
message = new TextField();
message.autoSize = TextFieldAutoSize.LEFT;
message.text = "My text";
message.scaleX = 1.2;
message.scaleY = 1.2;
message.textColor = 0xffffff;
background.addChild(message);
}
private function addOkButton():void {
button2 = new Sprite();
button2.graphics.beginFill(0x35733b, 1);
button2.graphics.drawRoundRect(0, 0, 90, 40, 10, 10);
button2.x = 175;
button2.y = 115;
button2.buttonMode = true;
button2.mouseChildren = false;
button2.alpha = 0.7;
background.addChild(button2);
button2.addEventListener(MouseEvent.ROLL_OVER, onButton2);
button2.addEventListener(MouseEvent.ROLL_OUT, outButton2);
button2.addEventListener(MouseEvent.CLICK, clickButton2);
buttonOk = new TextField();
buttonOk.autoSize = TextFieldAutoSize.LEFT;
buttonOk.text = "Ok";
buttonOk.scaleX = 1.2;
buttonOk.scaleY = 1.2;
buttonOk.textColor = 0x000000;
buttonOk.selectable = false;
buttonOk.x = button2.width/2 - buttonOk.width/2;
buttonOk.y = button2.height/2 - buttonOk.height/2;
button2.addChild(buttonOk);
}
private function addCancelButton():void {
button1 = new Sprite();
button1.graphics.beginFill(0x771d1d, 1);
button1.graphics.drawRoundRect(0, 0, 90, 40, 10, 10);
button1.x = 25;
button1.y = 115;
button1.buttonMode = true;
button1.useHandCursor = true;
button1.mouseChildren = false;
button1.alpha = 0.7;
background.addChild(button1);
button1.addEventListener(MouseEvent.ROLL_OVER, onButton1);
button1.addEventListener(MouseEvent.ROLL_OUT, outButton1);
button1.addEventListener(MouseEvent.CLICK, clickButton1);
buttonCancel = new TextField();
buttonCancel.autoSize = TextFieldAutoSize.LEFT;
buttonCancel.text = "Cancel";
buttonCancel.scaleX = 1.2;
buttonCancel.scaleY = 1.2;
buttonCancel.textColor = 0x000000;
buttonCancel.x = button1.width/2 - buttonCancel.width/2;
buttonCancel.y = button1.height/2 - buttonCancel.height/2;
button1.addChild(buttonCancel);
}
private function onButton1(e:MouseEvent):void {
button1.alpha = 1;
}
private function outButton1(e:MouseEvent):void {
button1.alpha = 0.7;
}
private function onButton2(e:MouseEvent):void {
button2.alpha = 1;
}
private function outButton2(e:MouseEvent):void {
button2.alpha = 0.7;
}
private function clickButton1(e:MouseEvent):void {
this.parent.removeChild(this);
}
private function clickButton2(e:MouseEvent):void {
this.parent.removeChild(this);
}
}
}
将任何变量发送到通知并返回它毫无意义。该通知仅应告诉您按下了哪些按钮。
想一想:如果您需要另一个通知来提供不同的值,那又只有一个“确定”和“取消”按钮,该怎么办?您会为此创建另一个通知类吗?
该通知不应对值或Main
类有任何了解。这是为了使其尽可能独立。通过通知将变量保存为往返,然后返回到main。
相反,请在中执行以下操作Main
:
您可能还需要执行其他辅助操作,例如:
此类操作超出了此问题的范围,因为这仅与信息流有关。
对于您的Notification
班级,最佳实践是调度自定义事件。此事件执行用户执行的操作,可能看起来像这样(未经测试的代码):
package
{
public class NotificationEvent extends Event
{
private var _action:String;
public function get action ()
{
return _action;
}
public NotificationEvent (action:String = "ok")
{
_action = action;
}
}
}
在您的Notification类中,调度该事件并将a传递String
给描述该动作的构造函数。(您可以仅允许某些值来进行详细说明,这将非常方便,但不会增加整体功能)
dispatchEvent(new NotificationEvent("cancel")); // when cancel button is clicked
在中Main
,添加到通知中的侦听器将接收Event作为参数,并可以通过action
我在类中定义的属性(例如在切换情况下)提取执行的操作:
function notificationHandler(e:NotificationEvent)
{
switch(e.action)
{
case "ok":
//do stuff
break;
case "cancel":
//do other stuff
break;
}
}
最初,自定义事件似乎过于复杂,但它为通知提供了清晰的界面,而没有暴露任何内部内容。如果要添加键盘控件,例如为“ ok”添加“ enter”,为“ cancel”添加“ esc”,则可以在Notification
该类中进行操作,而无需在外部进行任何更改。只要确保派遣了NotificationEvent
。
你经常会发现代码尝试特技一样notification.okBtn.addEventListener...
从Main
起初的工作,但如果应用变化快炸毁类。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句