显示对象平滑移动--本站转载
http://www.iteye.com/topic/162463#
?
用flex开发互动应用的时候经常会遇到显示对象与键盘鼠标的互动响应,通常的做法是添加侦听器来检测输入事件:
?
package {import flash.display.Sprite;import flash.events.Event;import flash.events.KeyboardEvent;import flash.ui.Keyboard;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(0x0080c0); circle_sprite.graphics.drawCircle(100,100,20); circle_sprite.graphics.endFill(); addChild(circle_sprite); stage.addEventListener(KeyboardEvent.KEY_DOWN,key_down); stage.addEventListener(KeyboardEvent.KEY_UP,key_up); stage.addEventListener(Event.ENTER_FRAME,enter_frame);}private function key_down(event:KeyboardEvent):void { if(event.keyCode==Keyboard.UP){ isUp=true; } if (event.keyCode==Keyboard.DOWN) { isDown=true; } if(event.keyCode==Keyboard.LEFT){ isLeft=true; } if(event.keyCode==Keyboard.RIGHT){ isRight=true; } }private function key_up(event:KeyboardEvent):void{ if(event.keyCode==Keyboard.UP){ isUp=false; } if (event.keyCode==Keyboard.DOWN) { isDown=false; } if(event.keyCode==Keyboard.LEFT){ isLeft=false; } if(event.keyCode==Keyboard.RIGHT){ isRight=false; } } public function enter_frame(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; } } }}
?