我想在Actionscript中编写一些警告框。实际上,我怀疑这样的东西是否真的有效(因为我特别注意到在更高版本的Actionscript中删除了mx.controls.Alert)。此外,在AS3.0中无法跳过代码(throw语句除外,通常用于错误通知)
所以,这是我写的最基本的代码。我想知道,如果这样的代码,将很好地集成到一个更大的项目中。可以看出,传递这样的函数甚至允许其他类访问类的私有成员。所以,在我看来,这似乎并不是一个好方法。编码它的更好方法是什么?
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() ;
}
}
}
答案 0 :(得分: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
类的自定义渲染器。
答案 1 :(得分:1)
您可以尝试以下简单代码。希望它会有所帮助
protected function img_clickHandler(event:MouseEvent):void {
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
}
}
答案 2 :(得分:1)
如果要避免传递函数,则应使用事件和事件侦听器。 MsgBox类应该是EventDispatcher的子类或实现IEventDispatcher接口。之后,您可以创建自定义事件类或只使用Event类。