我想在as3中編寫一個簡單的通知類,它只是兩個按鈕,一個用於「ok」,一個用於「取消」,因此有一個事件偵聽器用於每個按鈕。在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);
}
}
}
爲什麼你想要返回相同的值?將它發送給通知有什麼意義? – null
因爲一旦將值返回給Main,我希望它被平衡(按下按鈕)。它不好嗎? – Bogdan