2013-11-27 26 views
0

我想要在Actionscript中編碼的警報框。其實,我懷疑這樣的事情是否真的起作用(因爲我特別注意到在更高版本的Actionscript中刪除了mx.controls.Alert)。此外,沒有辦法跳過AS3.0中的代碼(除了throw語句,通常用於錯誤通知)要在Actionscript中編碼的警報框

所以,這是我寫的最基本的代碼。我想知道,如果這樣的代碼能夠很好地融入到一個更大的項目中。可以看出,像這樣傳遞函數甚至允許其他類訪問類的私有成員。所以,在我看來,這當然不是一個好方法。什麼是更好的編碼方式?

package{ 
    public class SomeClass 
    { 
     private secret = 007 
     public function SomeClass() 
     { 
      var msgBox:MsgBox = new MsgBox() 
      msgBox.show (" Tell, yes or no ? " , onYes, onNo) 
     } 

     private function onYes():void 
     { 
      trace ("yes") 
      trace (secret) ; 
     } 

     private function onNo():void 
     { 
      trace ("no ") 
      trace (secret) 
     } 
    } 
} 

package 
{ 
    public class MsgBox 
    { 
     public function MsgBox():void 
     { 

     } 

     public function show(val_str:String, onYes:Function, onNo:Function) 
     { 
      // we assume that yes button is cliked ; 
      onYes() ; 
     } 
    } 
} 

回答

2

這是一個很好的做法,有一個純AS3 Popup/Alert類。我有我自己的基本類Popup類以及保持了幾年,其繼承者(剝皮和自定義佈局實現通過擴展在這裏)是許多項目的一部分 - RIA應用程序和遊戲。

只是一些提示,如果你的魔杖創建自己的 - 這是非常有用的靜態方法info, error, confirm, etc.,如:

public static function info(msg:String, title:String = null, hideButtons:Boolean = false, btnlabel:String = null, closeBtn:Boolean = false, action:Function = null, content:DisplayObject = null):Popup 
... 

和非靜態:

protected function createOneButtonDialog(title:String, msg:String, label:String, content:DisplayObject = null, closeBtn:Boolean = false):void 
protected function createTwoButtonDialog(title:String, msg:String, label1:String, label2:String, content:DisplayObject = null, closeBtn:Boolean = false):void 

這樣你就可以覆蓋它們幷包含到主應用程序業務邏輯中。

這也是值得考慮使用renderer技術,其中renderer是靜態的,如創建彈出的實例:

protected static function msgOneButtonDialog(title:String, msg:String, label:String, content:DisplayObject = null, closeBtn:Boolean = false):Popup 
{ 
    var popup:Popup = new popupRenderer(); 
    popup.createOneButtonDialog(title, msg, label, content, closeBtn); 
    return popup; 
} 

它允許你使用繼承的項目Popup類自定義渲染。

+0

的一部分,但我們可以說,我想在「是」按鈕點擊時觸發某個特定功能,並且在沒有按鈕點擊時觸發另一個功能。你在使用eventlisteners嗎? –

+1

是的,事件處理程序,您可以將處理程序直接傳遞給工廠方法(參數_action:Function_)。如果動作被傳遞,那麼彈出框將它作爲偵聽器添加到所有事件(POPUP_OK,POPUP_CANCEL,POPUP_CLOSE(通過關閉按鈕)),如果不是,則可以手動添加事件處理程序,因爲每個工廠方法(_info,error等)返回_Popup_對象實例。 – fsbmain

+0

順便說一句,我很想知道,爲什麼在你看來,純as3是一種更好的編碼方式。我的意思是,mxml方法有什麼問題?(我認爲,通過純as3你的意思是,不使用mxml) –

1

你可以嘗試像下面簡單的代碼。希望這將有助於

保護功能img_clickHandler(事件:MouseEvent)方法:無效 {

  var confirmMsg:String = "Are you sure you are about to delete "; 
      var myAlert:Alert = Alert.show(confirmMsg,"Status",Alert.OK|Alert.CANCEL,this,alertListener,null,Alert.OK); 

     } 
     private function alertListener(evt:CloseEvent):void 
     { 
      Alert.okLabel = null; 
      Alert.cancelLabel = null; 

      if (evt.detail == Alert.OK) 
      { 
       //To do action 
      } 
     } 
+0

我不想使用MX包,因爲它不是現在的Flash CS6.0 –

1

如果你想避免傳遞函數,你應該使用事件和事件監聽器。 MsgBox類應該子類EventDispatcher或實現IEventDispatcher接口。之後,您可以創建自定義事件類或僅使用Event類。