Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 346
Mercy Avatar asked Dec 21 '25 05:12

Mercy


2 Answers

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 {}
like image 150
Mercy Avatar answered Dec 23 '25 15:12

Mercy


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.

like image 43
Taurayi Avatar answered Dec 23 '25 16:12

Taurayi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!