当前位置: 代码迷 >> Web前端 >> 展示对象平滑移动-本站转载
  详细解决方案

展示对象平滑移动-本站转载

热度:367   发布时间:2012-09-01 09:33:03.0
显示对象平滑移动--本站转载

http://www.iteye.com/topic/162463#

?

用flex开发互动应用的时候经常会遇到显示对象与键盘鼠标的互动响应,通常的做法是添加侦听器来检测输入事件:

?

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;
                    }
            }	                

}

}

?

  相关解决方案