Box2D platform engine alternative
2009-10-18 00:00:00 来源:WEB开发网And this is the class handling the keyboard input
package {
import flash.events.KeyboardEvent;
import flash.events.FocusEvent;
public class keys {
private var press_left = false;
private var press_right = false;
private var press_up = false;
private var press_down = false;
private var press_space = false;
public var Keys:Array = new Array(200);
public function keys(movieclip) {
movieclip.stage.addEventListener(FocusEvent.FOCUS_OUT, focus, false, 0);
movieclip.stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down, false, 0);
movieclip.stage.addEventListener(KeyboardEvent.KEY_UP, key_up, false, 0);
}
public function focus(e:FocusEvent){
for(var i:int = 0;i<Keys.length;i++){
Keys[i] = false;
}
press_left = false;
press_right = false;
press_up = false;
press_down = false;
press_space = false;
}
public function is_left() {
return press_left;
}
public function is_right() {
return press_right;
}
public function is_up() {
return press_up;
}
public function is_down() {
return press_down;
}
public function is_space() {
return press_space;
}
public function isDown(key:int){
return Keys[key];
}
private function key_down(event:KeyboardEvent) {
Keys[event.keyCode] = true;
if (event.keyCode == 32) {
press_space = true;
}
if (event.keyCode == 37 || event.keyCode == 65) {
press_left = true;
}
if (event.keyCode == 38 || event.keyCode == 87) {
press_up = true;
}
if (event.keyCode == 39 || event.keyCode == 68) {
press_right = true;
}
if (event.keyCode == 40 || event.keyCode == 83) {
press_down = true;
}
}
private function key_up(event:KeyboardEvent) {
Keys[event.keyCode] = false;
if (event.keyCode == 32) {
press_space = false;
}
if (event.keyCode == 37 || event.keyCode == 65) {
press_left = false;
}
if (event.keyCode == 38 || event.keyCode == 87) {
press_up = false;
}
if (event.keyCode == 39 || event.keyCode == 68) {
press_right = false;
}
if (event.keyCode == 40 || event.keyCode == 83) {
press_down = false;
}
}
}
}
And this is the result:
本文示例源代码或素材下载
更多精彩
赞助商链接