AS3 键盘的事件与实现
时间:2010-09-27 来源:xiwang_2010
学习AS3 事件机制,看到一个简单的actionscript 游戏动画,比较简单,一个红球按照你方向键移动 as3 学习群:41394416 脚本娃娃
代码如下:
package
{
import flash.display.SpreadMethod;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class ball extends Sprite
{
private var balls:Sprite = new Sprite();
public function ball()
{
init();
}
public function init():void{
addChild(balls);
balls.graphics.beginFill(0xFF0000);
balls.graphics.drawCircle(0,0,30);
balls.graphics.endFill();
balls.x = stage.stageWidth/2;
balls.y = stage.stageHeight/2;
stage.addEventListener(KeyboardEvent.KEY_DOWN,ballrect);
}
public function ballrect(event:KeyboardEvent):void{
switch(event.keyCode){
case Keyboard.UP:
balls.y -=10;
break;
case Keyboard.DOWN:
balls.y +=10;
break;
case Keyboard.LEFT:
balls.x -=10;
break;
case Keyboard.RIGHT:
balls.x += 10;
break;
default:
break;
}
}
}
} 有学习extjs as3的朋友可以加入到脚本娃娃学习论坛官网
{
import flash.display.SpreadMethod;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class ball extends Sprite
{
private var balls:Sprite = new Sprite();
public function ball()
{
init();
}
public function init():void{
addChild(balls);
balls.graphics.beginFill(0xFF0000);
balls.graphics.drawCircle(0,0,30);
balls.graphics.endFill();
balls.x = stage.stageWidth/2;
balls.y = stage.stageHeight/2;
stage.addEventListener(KeyboardEvent.KEY_DOWN,ballrect);
}
public function ballrect(event:KeyboardEvent):void{
switch(event.keyCode){
case Keyboard.UP:
balls.y -=10;
break;
case Keyboard.DOWN:
balls.y +=10;
break;
case Keyboard.LEFT:
balls.x -=10;
break;
case Keyboard.RIGHT:
balls.x += 10;
break;
default:
break;
}
}
}
} 有学习extjs as3的朋友可以加入到脚本娃娃学习论坛官网
相关阅读 更多 +