Por: Zguillez + 20.04.2008
Código :
package com.zguillez.events
{
public class Teclado extends EventDispatcher
{
public static var _instancia:Teclado;
//-------------------------------------------
public function Teclado(s:Singleton,clip) {}
//-------------------------------------------
public static function getInstancia(clip:MovieClip):Teclado
{
if (_instancia == null)
{
_instancia = new Teclado(new Singleton(),clip);
}
return _instancia;
}
//-------------------------------------------
}
}
//-----------------------------------------
class Singleton {}
Código :
public function Teclado(s:Singleton,clip)
{
_clip = clip;
_funciones = new Array();
_teclas = new Array();
_clip.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}
Código :
public function addTecla(n:uint, f:Function):void
{
_teclas.push(n);
_funciones.push(f);
}
Código :
private function onKeyDown(tecla:KeyboardEvent):void
{
var total:uint = _teclas.length
if (total > 0) {
for (var i:uint = 0; i < total ; i++) {
if (tecla.keyCode == _teclas[i])
{
_funciones[i](tecla.keyCode);
}
}
}
_tecla = tecla.keyCode;
dispatchEvent(new Event(Teclado.KEY_PRESS));
}
Código :
import com.zguillez.events.Teclado;
var teclado:Teclado = Teclado.getInstancia(this);
teclado.addTecla(70,traceTecla);
teclado.addTecla(71,traceTecla);
teclado.addTecla(72,traceTecla);
function traceTecla(keycode)
{
trace("Presioné la tecla " + keycode);
}
Código :
package com.zguillez.events
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.EventDispatcher;
//-------------------------------------------
public class Teclado extends EventDispatcher
{
public static var _instancia:Teclado;
public static var KEY_PRESS:String = "onKeyPress";
private var _clip:MovieClip;
private var _funciones:Array;
private var _teclas:Array;
private var _tecla:uint;
//-------------------------------------------
public function Teclado(s:Singleton,clip)
{
_clip = clip;
_funciones = new Array();
_teclas = new Array();
_clip.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}
//-------------------------------------------
public static function getInstancia(clip:MovieClip):Teclado
{
if (_instancia == null) {
_instancia = new Teclado(new Singleton(),clip);
}
return _instancia;
}
//-----------------------------------------
public function addTecla(n:uint, f:Function):void
{
_teclas.push(n);
_funciones.push(f);
}
//-------------------------------------------
private function onKeyDown(tecla:KeyboardEvent):void
{
var total:uint = _teclas.length
if (total > 0) {
for (var i:uint = 0; i < total ; i++) {
if (tecla.keyCode == _teclas[i])
{
_funciones[i](tecla.keyCode);
}
}
}
_tecla = tecla.keyCode;
dispatchEvent(new Event(Teclado.KEY_PRESS));
}
//-------------------------------------------
public function get tecla():uint
{
return _tecla;
}
//-------------------------------------------
}
}
//-----------------------------------------
class Singleton
{
}