package  {
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.media.Sound;
	import flash.net.URLRequest;	
	
	public class BallMove  extends Sprite {
  
        private const BALLRADIUS:Number=15; 
		private var ball:Ball;
		private var vx:Number;
		private var vy:Number;
		private var bounce:Number = -0.7;
		private var boing:Sound;        //定义声音对象
		
		public function BallMove() {
			init();			 
		}
		
		private function init():void {
			//设置舞台播放器窗口发生变化时同时自动地缩放
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			//创建一个声音文件的实例
			boing = new Sound(new URLRequest("boing.mp3"));
			
			//设置小球的基本运动
			ball = new Ball();
			ball.x = stage.stageWidth/2;
			ball.y = stage.stageHeight/2;
			vx = Math.random()*10-5;     //设置小球的x,y轴速度
			vy = -10;
			addChild(ball);
			addEventListener(Event.ENTER_FRAME,onEnterFrame);			
		}
		
		private function onEnterFrame(event:Event):void {
			ball.x += vx;
			ball.y += vy;
			//设置小球环境边界
			var leftline:Number = 0;
			var rightline:Number = stage.stageWidth;
			var topline:Number = 0;
			var bottomline:Number = stage.stageHeight; 
			//测试小球是否到边界，并做相关处理
			if (ball.x + BALLRADIUS > rightline)  {
				boing.play();     //播放小球碰墙音效
				ball.x = rightline - BALLRADIUS;      //执行屏幕折回
				vx *= bounce;
			}
			else if (ball.x - BALLRADIUS < leftline)  {
				boing.play();
				ball.x = leftline + BALLRADIUS;
				vx *= bounce;
			}
			else if (ball.y + BALLRADIUS > bottomline ) {
				boing.play();
				ball.y = bottomline - BALLRADIUS;
				vy *= bounce;
			}
			else if (ball.y - BALLRADIUS <topline )  {
				boing.play();
				ball.y = topline + BALLRADIUS;
				vy *= bounce;
			}
			
		}


	}
	
}
