Tuesday, January 27, 2015

Creating Advanced Alert Window class Part 1

Today we will start working on an advanced alert manager class for AS3.

The idea behind this tutorial series is to create a flexible AS3 class for displaying alert messages to the users.

The closest thing to this is the Alert class for Flex - however, I think it still lacks a few things. This is why I am writing this class and these articles - to create an universal and customizable class that would provide the developers with ability to quickly implement alert/notice/warning windows in their application.

If you have any additions to the code I provide, have any suggestions or questions - leave a comment. I hope you will find this series useful!

So, lets begin.

Im using FlashDevelop for writing the class, but Ill also use Flash for debugging. The class itself is just a folder of as3 files, but they alone are not viewable without an environment. Thats why well create a new AS3 project as a testing area for our class.

Im using Flash to create a new project and set its main class path to "main" (located under Publish tab in the tool panel). You can, of course, use FlashDevelop or any other IDE to do this.

Next to the Flash file Ill create a new class main.as.

Make the class extend MovieClip and declare a new variable called AlertManager. Set its type to AdvAlertManager. Import com.kircode.AdvAlertManager class. In the constructor, add a listener for ADDED_TO_STAGE event, set its handler to init() function.

In init(), set AlertManager to a new instance of AdvAlertManager. Call its alert() method to display a simple message.

package  
{
import com.kircode.AdvAlertManager;
import flash.display.MovieClip;
import flash.events.Event;
/**
* ...
* @author Kirill Poletaev
*/
public class main extends MovieClip
{
private var AlertManager:AdvAlertManager;

public function main()
{
addEventListener(Event.ADDED_TO_STAGE, init);
}

private function init(evt:Event):void {
AlertManager = new AdvAlertManager();
AlertManager.alert("This is a test message!");
}

}

}

Now create a new folder "com", inside of it add a folder "kircode", and there add a new class callde AdvAlertManager.as:



The class for now will only have a constructor that traces a message that an instance of this class has been created, and a public function alert().

The alert() function will later open a real alert window, but for now let it just trace the message:

package com.kircode 
{
/**
* Advanced Alert window manager.
* @author Kirill Poletaev
*/
public class AdvAlertManager
{

public function AdvAlertManager()
{
trace(":AdvAlertManager instance created.");
}

public function alert(text:String):void {
trace("Message: " + text);
}

}

}

And thats all for today!

Its not much yet, but the basis is there.

Thanks for reading!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.