Cuadro de alerta para codificar en Actionscript

I want sort of alert box coded in Actionscript. Actually, i doubt if something like that really works (as i particularly noticed that mx.controls.Alert was removed in later versions of Actionscript ). Additionally, there is no way to skip the code ( except throw statement, which is generally used for error notifications) in AS3.0

So, here is the most basic code, i wrote. I want to know, if such code, would be really good to be integrated into a bigger project. As it can be seen that passing functions like this even allow other classes to access the private members of the class. So, naturally it seems to me, as it's not a good way. What is the better way to code it ?

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() ;
        }
    }
}

preguntado el 27 de noviembre de 13 a las 05:11

3 Respuestas

It's a good approach, to have a pure as3 Popup/Alert class. I have my own basic class Popup class as well that is maintained for several years and its inheritors (skinning and custom layout implemented via extending here) were a part of many projects - RIA applications and games.

Just some hints, if you wand to create your own - it's useful to have static methods for info, error, confirm, etc. me gusta:

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
...

and non static:

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

so you will be able to override them and include to the main application business logic.

It's also worth to think about creating instance of popup using renderer technique, where renderer is static, like:

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;
}

It allows you to use custom renderers that inherits Popup class in projects.

respondido 27 nov., 13:09

but let's say, i want to trigger some particular function on "yes" button click, and another function on no button click.. Are you using eventlisteners for that ? - Vishwas

Yes, event handlers, where you can pass handler directly to the factory method (parameter action:Function). If action is passed popup adds it as listeners to all events (POPUP_OK, POPUP_CANCEL, POPUP_CLOSE (by close button)) if no, you can manually add event handlers, as every factory method (info, error, etc.) devoluciones Emergente object instance. - fsbprincipal

Btw, i was intrigued to know, why in your opinion, pure as3 is a better way to code. I mean, what's the problem with mxml approach ? ( As i think, by pure as3 you mean, not using mxml ) - Vishwas

Yo uso este Emergente only for pure as3 project, for project with flex ui I use flex Alert class ) Of course, It's also possible to use Popup in flex Project, for example if you want to keep this part marco independiente like in your case, when some of flex components aren't available by some reason. - fsbprincipal

ya..that makes sense. Thnx! - Vishwas

You can try simple code like following . Hope it will help

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
            }
        }

respondido 27 nov., 13:08

i don't wanna use mx package, as it's not a part of Flash CS6.0 now - Vishwas

If you want to avoid passing functions, you should use events and event listeners. MsgBox class should subclass EventDispatcher or implement IEventDispatcher interface. Afterwards you can create custom event class or just use Event class.

respondido 27 nov., 13:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.