标签:as3.0 键盘事件小实例 as3.0 键盘事件 as3.0 实例
下面的代码实现键盘操作图片左右走动
import flash.events.KeyboardEvent;
import flash.display.Loader;
import flash.net.URLRequest;
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyUpFunction);
var loader:Loader = new Loader();
var urlRequest:URLRequest = new URLRequest(‘image553.png‘);
loader.load(urlRequest);
//对左右进行数值定义
const LEFT:int = -1;
const RIGHT:int = 1;
//记录图片image553.png宽度
const IMGWIDTH = 100;
//记录前一个动作
var currentAction = 1;
//loader的初始化
loader.scaleX = -1;
loader.x+=IMGWIDTH;
loader.y = 25;
this.addChild(loader);
const SPEED:int = 10;
function keyUpFunction(event:KeyboardEvent):void{
switch(event.keyCode){
case 37:
if(currentAction == -1){
loader.x-=SPEED;
}else{
currentAction = -1;
loader.scaleX *= currentAction;
loader.x-=IMGWIDTH;
}
break;
case 38:
break;
case 39:
if(currentAction == 1){
loader.x+=SPEED;
}else{
currentAction = 1;
loader.scaleX *= -currentAction;
loader.x+=IMGWIDTH;
}
break;
case 40:
break;
}
}执行结果如下图,通过键盘的左右键来控制小图片的左右移动
标签:as3.0 键盘事件小实例 as3.0 键盘事件 as3.0 实例
原文地址:http://quietnight.blog.51cto.com/7163892/1666010