RSS

Actionscript 3 Keyboard Events

Actionscript 3 | Posted on Feb 14 2009

A simple keyboard events allow our applications to react to a key being hit on the keyboard. This can help increase the usability of your application and is especially handy if you are creating a game which requires direction with the arrow keys. Here is a simple way of capturing keyboard events responding back on the textfield.

stage.addEventListener (KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener (KeyboardEvent.KEY_UP, keyUpHandler);
 
function keyDownHandler (e:KeyboardEvent):void {
    keyDownText.text =
    "Key code: " + e.keyCode + "\n" +
    "Ctrl status: " + e.ctrlKey + "\n" +
    "Key location: " + e.keyLocation + "\n" +
    "Shift key: " + e.shiftKey + "\n";
}
 
function keyUpHandler (e:KeyboardEvent):void {
    keyUpText.text = "Key code: " + e.keyCode;
}

Download the source code

navigateToURL in AS 3

Actionscript 3 | Posted on Feb 14 2009

The example below show how to use navigateToURL to create external links to other pages, and more from within your flash file. A means to allow visitors to access or download special files. Set Variables, create functions, and add event listeners.

my_btn.addEventListener(MouseEvent.CLICK, myBtnClicked);

function myBtnClicked(e:MouseEvent):void {
    //create an instance of a URLRequest object and set it's url property
    var url:String="http://www.mouseup.me";
    var request:URLRequest=new URLRequest(url);
   
    try {
        //the new navigateToURL() function is a close approximation to to getURL();
        navigateToURL(request, '_blank');
    } catch (e:Error) {
        trace("Error occurred!");

    }
}

Tic Tac Toe Game

Actionscript 3 | Posted on Feb 08 2009

Tic-tac-toe used to be our childish pencil-and-paper game. The player who succeeds in placing three respective marks in a horizontal, vertical or diagonal row wins the game. To make a tic tac toe in flash, first must understand the logic to determine the location of where to put the current symbol in randomize. It is not as easy as it sound but it is also not as hard. It just take some time to master and it help find the need to use object oriented programming which make life much easier.

Play Tic Tac Toe Game

Loading an External swf in AS3

Actionscript 3 | Posted on Feb 07 2009

Flash movies allow you to load external SWF files (or image files including JPG, PNG, or GIF) into the Adobe Flash Player to play along with the content of the original. There are several advantages to loading external movies into the Flash Player instead of using a larger, single SWF file. Here’s a simple example of loading a .swf in AS3.

var myFile:Loader = new Loader();
var url:URLRequest;

url = new URLRequest("footer.swf");
myFile.load(url);
myFile.x = 100;
myFile.y = 50;
addChild(myFile);

//As you already have addChild that allows your MovieClip to be displayed at run time
//RemoveChild can be set to these Objects along with Event Handlers to removes them from the stage
btn_close.addEventListener(MouseEvent.MOUSE_DOWN, removeFile);

function removeFile(e:MouseEvent):void {
    removeChild(myFile);
}

This allows you to have an accurate snapshot of how much of your data has been downloaded and how much more is left. Once all of your data has been downloaded, then your Complete event fires letting you know that.

var myFile:Loader = new Loader();
var url:URLRequest;

url = new URLRequest("footer.swf");
myFile.load(url);
myFile.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
myFile.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
myFile.x = 100;
myFile.y = 50;
addChild(myFile);

//As you already have addChild that allows your MovieClip to be displayed at run time
//RemoveChild can be set to these Objects along with Event Handlers to removes them from the stage
btn_close.addEventListener(MouseEvent.MOUSE_DOWN, removeFile);

function removeFile(e:MouseEvent):void {
    removeChild(myFile);
}

function progressHandler(event:ProgressEvent):void {
     var prog:Number = Math.floor(((event.bytesLoaded/event.bytesTotal)*100));
     trace(prog)
 }
 
function completeHandler(event:Event):void {
    trace("done")
}

Download the source code

Play sound from the library in AS3

Actionscript 3 | Posted on Feb 07 2009

The script below is the example to import the music from library with play and pause button. You must assign your sound clip a class name by right clicking the sound in the library and checking the export for ActionScript option. In the example below the class name was set to “importedSound”.

var myAudio:importedSound = new importedSound();
var myChannel:SoundChannel = new SoundChannel();
var myPausePosition:uint;

function onComplete(event:Event):void {
    trace('download complete');
    btn.visible = true;
    pre.visible = false;
}

myChannel = myAudio.play();

play_mc.buttonMode = true;
play_mc.gotoAndStop(2);

//an event listener to check if the button is clicked, and a function to check if the sound is playing or not, and do the opposite
play_mc.addEventListener(MouseEvent.MOUSE_UP, myStartSound);

function myStartSound(event:MouseEvent):void {
        //continue play the music if the button is in pause mode or pause the music
    if (play_mc.buttonMode == false) {
        play_mc.buttonMode = true;
        myChannel = myAudio.play(myPausePosition);
        play_mc.gotoAndStop(2);
    } else {
        myPausePosition = myChannel.position;
        play_mc.buttonMode = false;
        myChannel.stop();
        play_mc.gotoAndStop(1);
    }
}

//an event listener on the channel when it ends trigger a function that will stat it again from the beginning.
myChannel.addEventListener(Event.SOUND_COMPLETE, myPlaybackComplete);

function myPlaybackComplete(event:Event):void {
    myChannel = myAudio.play();
}

Another way to load the mp3 file from a url.

var snd:Sound = new Sound();
snd.load(new URLRequest("my.mp3"));

//preloading the music
snd.addEventListener(Event.COMPLETE, onComplete);
snd.addEventListener(ProgressEvent.PROGRESS, progressHandler);
function progressHandler(event:ProgressEvent):void {
    var percent:int=Math.round(event.bytesLoaded/event.bytesTotal*100);
    trace(percent+" %");
}

function onComplete(event:Event):void {
    trace('download complete');
}

snd.play();

Download the source code