Friday, October 19, 2012

Creating EasyKeyboard class: Part 1

Today I will start creating a new free AS3 library, writing down and explaining everything step-by-step. The final product will be an open-source library that will hopefully be useful in different AS3 applications and games.

The library is called EasyKeyboard - its purpose is to allow the developer quickly add keyboard listeners and handlers to their program. The code that included several functions before will only require 1 line, with no loss of performance. On the contrary - the library will both save the developer's time and optimize the code.

Create a folder where you'll keep all the project related files. Add a file called main.as, which will be the document class of our example flash file for testing purposes. Then add a new directory "com", inside of it add "kircode", and inside of that add "EasyKeyboard". Here, add a new class EasyKeyboard.as.

Inside of EasyKeyboard.as, declare 1 private variable - "st", type Stage.

Receive a parameter "stage" in the constructor and then apply its value to st. Add 3 listeners to stage - KEY_DOWN, KEY_UP and ENTER_FRAME. Add their handler functions, leave them empty for now:

package com.kircode.EasyKeyboard 
{
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;

/**
 * Utility for easy keyboard listener management.
 * @author Kirill Poletaev
 */
public class EasyKeyboard 
{
private var st:Stage;

public function EasyKeyboard(stage:Stage) 
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, kDown);
stage.addEventListener(KeyboardEvent.KEY_UP, kUp);
stage.addEventListener(Event.ENTER_FRAME, frame);
st = stage;
}

private function frame(evt:Event):void {

}
private function kDown(evt:KeyboardEvent):void {

}

private function kUp(evt:KeyboardEvent):void {

}

}

}

Those are all the listeners we will have in the class (unless later we want to somehow expand the library, but even then it is unlikely that we will have to add more listeners). All the keyboard events are handled by kDown and kUp functions. The frame function will be used for more advanced features of the class, for instance, the ability to listen when a key is being held (execute an action every frame as long as a key is held).

Back in main.as class, we can create a basic class which extends MovieClip and currently does nothing useful. We'll add a keyboard variable though, and set it to an instance of EasyKeyboard class:

package  
{
import com.kircode.EasyKeyboard.EasyKeyboard;
import flash.display.MovieClip;
import flash.events.Event;
/**
 * Example of EasyKeyboard library usage.
 * @author Kirill Poletaev
 */
public class main extends MovieClip
{
public var keyboard:EasyKeyboard

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

private function init(evt:Event):void {
keyboard = new EasyKeyboard(stage);
}

}

}

That's all for today.

Next time we'll begin working on functionality!

Thanks for reading!

No comments:

Post a Comment