Alert/ Dialog box in AS3


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





You are mistaken. Alert has never been removed.
– RIAstar
May 16 '12 at 15:59


Alert





Maybe. can you please post some links or examples on Alert in AS3?
– 22kar
May 16 '12 at 16:21





Pure AS3 doesn't have alert it's a Flex component. After you import it it's simply Alert.show("this is an alert");
– Barış Uşaklı
May 16 '12 at 16:22





@RIAstar Unless he's building a mobile project. In that case, he can use a SkinnablePopUpContainer instead of an Alert. Or there is a mobile Alert class in the Apache Flex Repository.
– JeffryHouser
May 16 '12 at 21:15





Sorry I forgot to mention that I am developing a mobile project! Thanks Flextras.com! Your comment helped me to figure this out.
– 22kar
May 17 '12 at 14:05




3 Answers
3



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





I dont know where I am going wrong but the flash builder is not allowing me to import- import mx.controls.Alert. It says the import Alert could not be found.
– 22kar
May 16 '12 at 16:47





Could you specify what kind of project do you have? Are you really doing Flex/Flash project or a Flash project with Flash Professional CS builder? With previous one, you can not use Alert class.
– Jarno Lahtinen
May 16 '12 at 16:53




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:




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




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.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV