package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class FlexTest extends Sprite
{
private var circle_sprite:Sprite;
private var isUP:Boolean;
private var isDown:Boolean;
private var isLeft:Boolean;
private var isRight:Boolean;
private const MOVE_SPEED:int=2;
public function FlexTest()
{
circle_sprite = new Sprite();
circle_sprite.graphics.beginFill(0x000000);
circle_sprite.graphics.drawCircle(100,100,20);
circle_sprite.graphics.endFill();
this.addChild(circle_sprite);
stage.addEventListener(KeyboardEvent.KEY_DOWN,getKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,getKeyUp);
stage.addEventListener(Event.ENTER_FRAME,getEnterFrame);
}
/**
*侦听键盘按下事件
*/
private function getKeyDown(event:KeyboardEvent):void
{
if(event.keyCode == 38){
isUP = true;
}
if(event.keyCode == 40){
isDown = true;
}
if(event.keyCode == 37){
isLeft =true;
}
if(event.keyCode ==39){
isRight = true;
}
}
/**
*侦听键盘松开事件
*/
private function getKeyUp(event:KeyboardEvent):void
{
if(event.keyCode == 38){
isUP = false;
}
if(event.keyCode == 40){
isDown = false;
}
if(event.keyCode == 37){
isLeft =false;
}
if(event.keyCode ==39){
isRight = false;
}
}
/**
*改变坐标
*/
private function getEnterFrame(event:Event):void
{
if(isUP){
circle_sprite.y -=MOVE_SPEED;
}
if(isRight){
circle_sprite.x +=MOVE_SPEED;
}
if(isDown){
circle_sprite.y +=MOVE_SPEED;
}
if(isLeft){
circle_sprite.x -=MOVE_SPEED;
}
}
}
}
PV3D入门程序-图像移动实例
最新推荐文章于 2025-08-10 12:26:43 发布
本文介绍了一个使用ActionScript 3.0 (AS3) 编写的简单示例,通过键盘事件来控制屏幕上的圆形对象移动。具体实现包括监听键盘按键、更新圆形位置等关键步骤。
12万+

被折叠的 条评论
为什么被折叠?



