as3.0 随机不重叠
package com{ import flash.display.MovieClip; import flash.geom.Rectangle; import flash.display.Sprite; import flash.utils.Timer; import flash.events.TimerEvent; public class RoundFun extends MovieClip { public static var emptyMc:MovieClip; public static var totalNum:Number=8; public static var arr:Array=[]; public static var speed:Number=2 public static var dis:Number=20;//间距 public static var len:int=10;//对象数量 public static var bound:Rectangle=new Rectangle(200,150,1500,300);//显示范围 public static var fesh:MovieClip public static var timer:Timer=new Timer(25) public function RoundFun(_emptyMc:MovieClip) { emptyMc=_emptyMc reset(); timer.addEventListener(TimerEvent.TIMER,onTimer) timer.start() } public static function onTimer(e:TimerEvent){ emptyMc.y+=speed } public static function reset():void { for (var j:int = 0; j<len; j++) { fesh=new FeshClass ; emptyMc.addChild(fesh); setPosition(fesh); arr.push(fesh) } } public static function setPosition(obj:Sprite):void { obj.x = Math.random()*(bound.width-obj.width)+bound.x; obj.y = Math.random()*(bound.height-obj.height)+bound.y; for (var i:int = 0; i<arr.length; i++) { if (obj != arr[i]) { //不重叠,没有间距 /*if(obj.hitTestObject(arr[i])){ setPosition(obj); return; }*/ //不重叠,并有一定间距dis,间距为0效果同上 if (Math.abs(obj.x-arr[i].x)<obj.width+dis&&Math.abs(obj.y-arr[i].y)<obj.height+dis) { setPosition(obj); return; } } } } } }
posted on 2019-06-18 14:09 zqiang0803 阅读(387) 评论(0) 编辑 收藏 举报