简体   繁体   中英

How to Create Alert Dialog in ActionScript3?

I'm working on a project with Actionscript 3.0 in Flash Pro (CS5). I want to create a confirmation box. If I were using the Flex SDK I could do this using the Alert class in the mx.controls package. However, it seems that no similar control exists in the standard Flash library and any amount of Googling just leads me to Flex references.

Try this Class

package com.whatever {

//Imports
import flash.display.Shape;
import flash.display.Sprite;
import flash.geom.Rectangle;
import flash.events.MouseEvent;

//Class
public class AlertBox extends Sprite {

    //Vars
    protected var box:Shape;
    protected var yesBtn:Sprite;

    //Constructor
    public function AlertBox($:Rectangle):void {

        //Initialise
        box = new Shape()
        yesBtn = new Sprite()
        addChild(box)
        addChild(yesBtn)

        //Render
        with (box.graphics) {
            lineStyle(1)
            beginFill(0, 0.4)
            drawRect($.x, $.y, $.width, $.height)
            endFill()
        }

        with (yesBtn.graphics) {
            lineStyle(1, 0x00FF00)
            beginFill(0x00FF00, 0.4)
            drawRect($.x+$.width-100, $.y$.height-40, 80, 20)
            endFill()
        }

        //Events
        yesBtn.addEventListener(MouseEvent.CLICK, yesClickHandler, false, 0, true) 
        yesBtn.addEventListener(MouseEvent.MOUSE_OVER, yesOverHandler, false, 0, true) 

    }

    //Handlers
    protected function yesClickHandler($):void {}
    protected function yesOverHandler($):void {}

You said that you can't import mx.Controls in AS3 but the following should work in a flex 4 project:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               creationComplete="init()">

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <fx:Script>
        <![CDATA[
        import mx.controls.Alert;

        private function init():void
        {
            Alert.show("This is an Alert!!!");

        }// end function

        ]]>
    </fx:Script>

</s:Application>

[UPDATE]

After realizing that I misunderstood the question, I looked on the internet for a Alert component for AS3 projects and found the following:

http://developer.yahoo.com/flash/astra-flash/alertmanager/

I'm going try my hand at creating a replica of the flex framework's Alert control and then update my answer again.

If your final swf is going to run in the browser and have script access, you could just use one of the JavaScript PopUp Boxes :

if(ExternalInterface.available) {

  if (ExternalInterface.call("confirm", "Should I trace 'Yes'?")) {

    trace("Yes"); // user clicked Okay

  } else {

    trace("User canceled or the call failed");

  }
}

I'm pretty sure this will freeze the Flash UI loop until the JavaScript function returns, so make sure you call it when it's all you want to be doing.

I believe that you can continue to use the same Alert class

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/Alert.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM