浏览 6211 次
锁定老帖子 主题:Flex研究——显示对象平滑移动
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (7)
|
|
---|---|
作者 | 正文 |
发表时间:2008-02-15
package { import flash.display.Sprite; 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(0xff0000); circle_sprite.graphics.drawCircle(100,100,20); circle_sprite.graphics.endFill(); addChild(circle_sprite); stage.addEventListener(KeyboardEvent.KEY_DOWN,key_down); } private function key_down(evt:KeyboardEvent):void{ if (evt.keyCode==Keyboard.LEFT){ circle_sprite.x-=MOVE_SPEED; } if (evt.keyCode==Keyboard.RIGHT){ circle_sprite.x+=MOVE_SPEED; } if (evt.keyCode==Keyboard.UP){ circle_sprite.y-=MOVE_SPEED; } if (evt.keyCode==Keyboard.DOWN){ circle_sprite.y+=MOVE_SPEED; } } } } 以上实现,在使用过程中用方向键控制MC移动的时候为没有那么流畅,MC的移动明显有点卡,这是因为键盘按键如果一直按住不放,它也是间隔开来不断的重复触发按键事件,键盘的键值扫描有一定的间隔频率,因此产生了断断续续的感觉。 要想得到平滑的移动效果,可以通过添加事件侦听器侦听Event.ENTER_FRAME事件来解决 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; } } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |