变化的小球

==========onBall文档类==========

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.display.StageScaleMode;
	public class onBall extends Sprite {
		private var left:uint=43;//黑球的半径为43
		private var right:uint=458;//舞台宽度-43
		private var top:uint=43;//黑球的半径为43
		private var bottom:uint=245;//舞台高度-43
		public function onBall():void {
			stage.scaleMode=StageScaleMode.NO_SCALE;
			//设置即使播放器窗口拉大,舞台大小也不拉伸
			init();
			addEventListener(Event.ENTER_FRAME,blackdong);
		}
		private function init():void {
			for (var i:int=0; i<5; i++) {
				var blackball:Ball=new Ball(0x000000,43);
				addChildAt(blackball,i);//黑色小球在显示列表的索引号0-5
			}
			for (var t:int=0; t<5; t++) {
				var whiteball:Ball=new Ball(0xffffff,40);
				whiteball.x=55*(t+1);
				whiteball.y=143;
				whiteball.yspeed=t+5;
				whiteball.xspeed=Math.random()*5+1;
				addChildAt(whiteball,t+5);//白色小球在显示列表的索引号5-9
				whiteball.addEventListener(Event.ENTER_FRAME,whitedong);
			}
		}

		private function whitedong(evt:Event):void {
			evt.target.x+=evt.target.xspeed;
			evt.target.y+=evt.target.yspeed;
			if (evt.target.x<=left||evt.target.x>=right) {
				evt.target.xspeed*=-1;
			}
			if (evt.target.y<=top||evt.target.y>=bottom) {
				evt.target.yspeed*=-1;
			}
		}
		private function blackdong(evt:Event):void {
			for (var h:int=0; h<5; h++) {
				getChildAt(h).x=getChildAt(h+5).x;
				getChildAt(h).y=getChildAt(h+5).y;
			}
		}
	}
}

==========Ball小球类==========

package {
	import flash.display.Shape;
	public class Ball extends Shape {
		private var _xspeed:int;
		private var _yspeed:int;
		//创建小球类,传入2个参数:颜色和半径,这是为了定义2种小球:黑色和白色
		public function Ball(_color:uint,_r:uint):void {
			init(_color,_r);
		}
		private function init(_color:uint,_r:uint):void {
			this.graphics.beginFill(_color);
			this.graphics.drawCircle(0,0,_r);
			this.graphics.endFill();
		}

		public function get xspeed():int {
			return _xspeed;
		}
		public function set xspeed(xp:int):void {
			_xspeed=xp;
		}
		public function get yspeed():int {
			return _yspeed;
		}
		public function set yspeed(yp:int):void {
			_yspeed=yp;
		}
	}
}
posted @ 2010-04-09 18:18  LT世纪  阅读(175)  评论(0编辑  收藏  举报