[ActionScript 3.0] AS3.0 对象在一定范围随机显示不重叠

import flash.geom.Rectangle;
import flash.display.MovieClip;
import flash.display.Sprite;

var arr:Array = [];
var dis:Number = 20;//间距
var len:int=15;//对象数量
var bound:Rectangle = new Rectangle(0,0,1000,800);//显示范围
for(var i:int = 0;i<len;i++){
    var obj:Sprite = new Sprite();
    obj.graphics.beginFill(0xcccccc);
    obj.graphics.drawRect(0,0,120,100);
    obj.graphics.endFill();
    obj.name = "obj_"+i;
    arr.push(obj);
    this.addChild(obj);
}
reset();
function reset():void{
    for(var j:int = 0;j<len;j++){
        setPosition(this.getChildByName("obj_"+j) as Sprite);
        //test(this.getChildByName("obj_"+j) as Sprite);
    }
}
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;
            }
        }
    }
}
//检测是否重合
function test(obj:Sprite):void
{
    for(var i:int = 0;i<len;i++){
        if(obj != arr[i]){
            if(obj.hitTestObject(arr[i])){
                trace(obj.name,arr[i].name);
            }
        }
    }
}
时间: 2024-10-07 08:24:25

[ActionScript 3.0] AS3.0 对象在一定范围随机显示不重叠的相关文章

[ActionScript 3.0] AS3.0和AS2.0的相互通信

AS3和AS2之间的通信,最好的方式可能就是LocalConnection了. AS2向AS3发送数据,即AS2调用AS3的函数: as2.0代码(按钮上写的发送信息代码): on (release) { var param = "this message is from as2"; var caller:LocalConnection = new LocalConnection(); caller.send("AS2 send to AS3","funI

[ActionScript 3.0] AS3.0 水面波纹效果

import flash.geom.Point; import flash.display.BitmapData; import flash.filters.DisplacementMapFilter; import flash.display.MovieClip; import flash.events.Event; /** * 创建湖面微波效果 */ function createWater(target:MovieClip):void { var count:int = 1; var po

[ActionScript 3.0] AS3.0 模拟套索工具抠图的两种方法

方法之一:遮罩法 package com.fylibs.tools { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.DisplayObject; import flash.display.Shape; import flash.display.Sprite; import flash.events.MouseEvent; import flash.geom.Matrix; i

[ActionScript 3.0] AS3.0 生成xml方法之一

var type:Array = ["type0", "type1", "type2"]; var property:Array = [["20","0","1.jpg"], ["60","0","2.jpg"], ["120","0","3.jpg"]];var da

[ActionScript 3.0] AS3.0 马赛克效果

var bmpd:BitmapData; var matrix:Matrix; var bmp:Bitmap; var size:Number = 5; /** * @author:Frost.Yen * @E-mail:[email protected] */ function mosaic(source:MovieClip,size:Number):void { if (bmpd) { bmpd.dispose(); } if (matrix) { matrix = null; } bmpd

[ActionScript 3.0] AS3.0 Socket通信实例

以下类是充当Socket服务器的例子 package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.events.ProgressEvent; import flash.events.ServerSocketConnectEvent; import flash.net.ServerSocket; import flash.net.Sock

[ActionScript 3.0] as3.0加载as2.0的swf时获取as2.0的实际舞台尺寸

var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler); loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorH

[ActionScript 3.0] AS3.0根据当天日期获取明天,后天...日期

const dayTime:Number=24*3600*1000//一天毫秒数 var date:Date = new Date(); trace("今天:"+ date.toDateString()); for(var i:int = 2;i<15;i++){ date.setTime(date.valueOf()+dayTime); trace("第"+i+"天:"+ date.toDateString()); }

[ActionScript 3.0] AS3.0 Loader加载子swf时是否需要指定新的应用程序域ApplicationDomain

实际应用中, Loader加载子swf时是否需要指定新的应用程序域ApplicationDomain,需要择情况而定. 1.当加载的子swf中有流存在时,比如视频或者声音,则最好需要指定新的应用程序域ApplicationDomain,如果没有指定,调用_ldr.unloadAndStop();时无法将子swf中的流从内存中卸载掉,了解原因,请深入了解应用程序域ApplicationDomain,代码如下: var ldr:Loader = new Loader(); var context:L