'Alert/ Dialog box in AS3

I am working on a app using Adobe flash Builder which should pop up a Alert window once a particular event has been triggered. Another event needs to be called when the Alert box is closed. But I do not see the Alert class in mx.controls library. It seems like the class (which existed in AS2) has been removed from AS3. Is there any other way to accomplish the same functionality?

Thanks, Pritesh



Solution 1:[1]

Yo need to define closeHandler for your Alert control. Checkout the ActionScript 3.0 Reference api from here http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/Alert.html#show()

Solution 2:[2]

Use ExternalInterface.

import flash.external.ExternalInterface;

// tell flash what javascript function to listen for, and what flash function to call in response
ExternalInterace.addCallback('onAlertWindowClosed', onPopupClosed);

function openPopUp():void
{
    // this conditional prevents errors when running local (yes, this needs uploaded to work)
    if(ExternalInterface.available)
    {
        // this calls a javascript function in your html
        ExternalInterface.call('myJavascriptAlertFuntion');
    }
}

// this function is called from the javascript callback **onAlertWindowClosed**
function onPopupClosed():void
{
    // do something when your popup closes
}

and in the html:

<script type="text/javscript>

// this chunk gets the flash object so you can call its methods
function getFlashMovieObject(movieName)
{
    if (window.document[movieName])
    {
        return window.document[movieName];
    }

    if (navigator.appName.indexOf("Microsoft Internet") == -1)
    {
        if (document.embeds && document.embeds[movieName])

        return document.embeds[movieName];
    }
    else
    {
        return document.getElementById(movieName);
    }
}

// function that is called from flash
function myJavascriptAlertFuntion()
{
    alert("Hey!  Yeah you there!");
}

// call this when you want to tell flash you are closing your popup
function tellFlashMyPopupWindowClosed()
{
    // **flashContainer** should be replaced by the name parameter of your flash embed object
    var flashMovie = getFlashMovieObject("flashContainer");
    flashMovie.onAlertWindowClosed();
}

</script>

Solution 3:[3]

To have a popup alert in a Mobile project using MXML and AS3, you need to create a component based off of the SkinnablePopUpContainer from the Sparks components. (Since a simple alert has been conveniently uncluded.)

I learned alot reading up on the SkinnablePopUpContainer in the docs here:

The Spark SkinnablePopUpContainer container

To sum it up, I've created a component in MXML with SkinnablePopUpContainer as the base class. In the View that I want to have the popup to be added to, I create a new instance of the class in Actionscript. I listen to the custom events the buttons in the component will be firing on user response. To show the new popup component, simply call the static method open();. The open() method expects a parent container, and wether or not the popup should be Modal. Modal means that nothing under the component can receive user input.

var alert:SkinnablePopUpContainer = new SkinnablePopUpContainer;
alert.addEventListener( "OK", onOK );
alert.open( this, true );
function onOK(e:Event):void{ trace("User said OK") };

I'll put up some example files later when I can.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jarno Lahtinen
Solution 2
Solution 3 charlesclements