In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

The Tatami Galaxy

you might also like:
http://wonderfl.net/c/8muP
http://vimeo.com/4917346
/**
 * Copyright yuuganisakase ( http://wonderfl.net/user/yuuganisakase )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/a5Ex
 */

package 
{
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.TimerEvent;

    import flash.utils.Timer;
    
    import flash.geom.Rectangle;
    import gs.plugins.*;
    
    
    [SWF(width = "465", height = "465", backgroundColor = "0x4F6D58")]
    public class Main extends Sprite 
    {
        
        private var nodes:Vector.<RoomNode> = new Vector.<RoomNode>()
        private var timer:Timer;
        
        private var holder:Sprite = new Sprite();
        private var room2:RoomNode;
        
        public function Main():void 
        {
 Wonderfl.capture_delay(10);           

            timer = new Timer(200);
            timer.addEventListener(TimerEvent.TIMER, onTimer);
            timer.start();
            
            addChild(holder);
            
            var room:RoomNode = new RoomNode(nodes);
            room.xx = 180;
            room.yy = 230;
            room.ww = 40;
            room.hh = 50;
            room.redraw();
            nodes.push(room);
            holder.addChild(room);
            
            //var room2:RoomNode = new RoomNode();
            //room2.xx = 600;
            //room2.yy = 300;
            //room2.ww = 200;
            //room2.hh = 200;
            //room2.redraw();
            //nodes.push(room2);
            //holder.addChild(room2);
            
            //var a:Rectangle = room.getRect(room.stage);
            //var b:Rectangle = room2.getRect(room2.stage);
            //trace("intersect");
            //trace(a.intersects(b));
            
            //
            //var room2:RoomNode = new RoomNode();
            //room2.xx = 400;
            //room2.yy = 300;
            //room2.hh = 400;
            //room2.redraw();
            //nodes.push(room2);
            //holder.addChild(room2);
            //room2.spaceFlag = 1 + 2 + 4 + 8;
            
            stage.addEventListener(MouseEvent.CLICK, onClick);
            stage.addEventListener(Event.ENTER_FRAME, onEnter);
        }
        private function onEnter(e:Event):void {
            //holder.x -= 1;
        }
        private function onClick(e:MouseEvent):void 
        {
            //timer.stop();
            for (var i:int = 0; i < nodes.length; i++) 
            {
                nodes[i].dispose();
            }
            nodes.length = 0;
            var room:RoomNode = new RoomNode(nodes);
            room.xx = this.stage.stageWidth/2;
            room.yy = this.stage.stageHeight/2;
            room.ww = 40;
            room.hh = 50;
            room.redraw();
            nodes.push(room);
            holder.addChild(room);
        }
        
        private function onTimer(e:TimerEvent = null):void 
        {
            trace("" );
            trace("[Main].onTimer -- " );
            var i:int;
            //if (nodes.length > 40) return;
            if (nodes.length < 20) {
                for (i = nodes.length; i > 0; i--) 
                {
                    if ( Math.random() > 0.4) {
                        if ( new AddRoomCommand(nodes[i-1], holder, nodes).execute(RoomNode.DIR[int(Math.random()*2)]) ){
                            break;
                        }
                    }
                }
            }else {
                for (i = 0; i < nodes.length; i++) 
                {
                    if ( Math.random() > 0.6) {
                        if ( new AddRoomCommand(nodes[i], holder, nodes).execute(RoomNode.DIR[int(Math.random()*2)]) ){
                            break;
                        }
                    }
                }    
            }


            
            trace("node.length : " + nodes.length);
            
        }

        
    }
    
}

    import gs.easing.Sine;
    import gs.TweenMax;
    import flash.display.DisplayObject;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.utils.setTimeout;
    import flash.geom.Matrix;
    class RoomNode extends Sprite {
        
        public var ww:Number;
        public var hh:Number;
        public var xx:Number;
        public var yy:Number;
                        
        private var co:uint;        
        
        public static const DIR:Array = [ "left","right","top","bottom"];
        
        public var nodes:Vector.<RoomNode>;
        
        public function RoomNode(_n:Vector.<RoomNode>) {
            var nn:int = 110;
            ww = Math.random() * nn + 25;
            hh = Math.random() * nn + 25;
            nodes = _n;
            co = colors[int(Math.random()*colors.length)]
            //addEventListener(MouseEvent.CLICK, onMove);
            addEventListener(MouseEvent.MOUSE_MOVE, onMove);
        }
        private function remove():void {
            this.parent.removeChild(this);
            dispose();
        }
        private function onMove(e:MouseEvent):void 
        {
            //TweenMax.to(this, 0.2, {transformMatrix:{a:0.2, b:0, c:0, d:0.2, tx:xx, ty:yy},alpha:0.8, onComplete:remove});
                TweenMax.to(this, 0.3, {alpha:0, onComplete:remove});
            removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
            for (var i:int = 0; i < nodes.length; i++) 
            {
                if (nodes[i] == this) {
                    nodes.splice(i, 1);
                    break;
                }
            }
            
        }
        
        public function dispose():void {
            
            nodes = null;
            this.graphics.clear();
            toiletGra.graphics.clear();
            
        }
        
        
        private function onclick(e:MouseEvent):void 
        {
            trace(xx + " - " +   yy + " - " +  ww + " - " +  hh);
            
            trace(this.getRect(this.stage).toString());
            e.stopPropagation();
        }
        
        
        //public var doorPoint:Point;
        //public var doorDir:String;
        public var doors:Vector.<DoorObj> = new Vector.<DoorObj>();
        
        private var colors:Array = [0x6397AD,0xB73A37,0xF9E08F,0x513633,0x607567,0x8C915C,0x32353A,0xEFCC6A,0xBABCB3];
        public function redraw():void {
            this.graphics.clear();
            this.graphics.beginFill(co);
            this.graphics.lineStyle(4, 0x333333);
            this.graphics.drawRect(xx - ww/2, yy - hh/2, ww, hh);

            
            this.graphics.endFill();
            if (Math.random() > 0.85) {
                createToilet();
            }else {
                if ( contains(toiletGra)) removeChild(toiletGra);
            }
            for (var i:int = 0; i < doors.length; i++) 
            {
                
                createDoor(doors[i].point, doors[i].dir);
            } 
            
        }
        override public function getRect(a:DisplayObject):Rectangle {
            return new Rectangle(xx-ww/2, yy-hh/2, ww, hh);
        }
        public function createDoor(p1:Point, dir:String):void {
            this.graphics.beginFill(0xC2A96F);
            this.graphics.lineStyle(1, 0x333333);
            const Num:Number = 16;
            
            var pp:Point
            if (dir == RoomNode.DIR[0]) {
                pp = new Point(p1.x - 2, p1.y);
                this.graphics.drawRect(pp.x , pp.y, 4, 16);
                
                
                if (Math.random() > 0.7) {
                    if (p1.x > xx && ww > 20 && (!contains(toiletGra) || !toiletGra.getRect(this.stage).containsPoint(new Point(pp.x-Num, pp.y)))) {
                        
                        this.graphics.moveTo(pp.x, pp.y);
                        this.graphics.lineTo(pp.x-Num, pp.y);
                        this.graphics.curveTo(pp.x - Num, pp.y+Num, pp.x , pp.y  + Num);
                        
                    }else if(ww > 20 && (!contains(toiletGra) || !toiletGra.getRect(this.stage).containsPoint(new Point(pp.x+Num, pp.y+Num)))) {
                        pp.x += 4;
                        this.graphics.moveTo(pp.x, pp.y+Num);
                        this.graphics.lineTo(pp.x + Num, pp.y+Num);
                        this.graphics.curveTo(pp.x + Num, pp.y, pp.x , pp.y );
                    }
                }
            }else if (dir == RoomNode.DIR[2]) {
                pp = new Point(p1.x, p1.y - 2);
                this.graphics.drawRect(pp.x, pp.y , 16, 4);
                
                if (Math.random() > 0.7) {
                    if (p1.y > yy && hh > 20 && (!contains(toiletGra) || !toiletGra.getRect(this.stage).containsPoint(new Point(pp.x, pp.y-Num)))) {
                        
                        this.graphics.moveTo(pp.x, pp.y);
                        this.graphics.lineTo(pp.x , pp.y - Num);
                        this.graphics.curveTo(pp.x + Num, pp.y-Num, pp.x+Num , pp.y);
                    }else if(hh > 20 && (!contains(toiletGra) || !toiletGra.getRect(this.stage).containsPoint(new Point(pp.x+Num, pp.y+Num)))){
                        pp.y += 4;
                        this.graphics.moveTo(pp.x+Num, pp.y);
                        this.graphics.lineTo(pp.x+Num, pp.y+Num);
                        this.graphics.curveTo(pp.x , pp.y+Num, pp.x , pp.y  );
                    }
                }
            }
            this.graphics.endFill();
        }
        
        private var toiletGra:Shape = new Shape();
        
        private function createToilet():void {
            
            if (ww > 30 && hh > 30) {
                addChild(toiletGra);
            }else if(contains(toiletGra)){
                removeChild(toiletGra);
            }
                
                toiletGra.graphics.clear();
                toiletGra.graphics.lineStyle(1,0x333333);
                toiletGra.graphics.beginFill(0x95AB78);
                toiletGra.graphics.moveTo(-8, 10);
                toiletGra.graphics.lineTo( 8,10);
                toiletGra.graphics.lineTo( 8, 6);
                toiletGra.graphics.lineTo(4, 5);
                toiletGra.graphics.curveTo(13, -5,  1,  - 8 );
                toiletGra.graphics.lineTo( -1, -8);
                toiletGra.graphics.curveTo(-13, - 5,  -4,  5 );
                toiletGra.graphics.lineTo(-8 , 6);
                toiletGra.graphics.lineTo(-8 , 10);
                
                
                if (Math.random() > 0.75) {
                    toiletGra.rotation = 90;
                    toiletGra.x = xx - ww / 2 + 14;
                    toiletGra.y = yy;
                }else if (Math.random() > 0.5) {
                    toiletGra.rotation = 180;
                    toiletGra.x = xx;
                    toiletGra.y = yy - hh / 2 + 14;
                }else if (Math.random() > 0.25) {
                    toiletGra.rotation = 270;
                    toiletGra.x = xx + ww / 2 - 14 ;
                    toiletGra.y = yy 
                }else {
                    toiletGra.x = xx;
                    toiletGra.y = yy + hh / 2 - 14;

                }
        }
    }
    
        
        
        
    class DoorObj
    {
        public var point:Point;
        public var dir:String;
        public function DoorObj(_p:Point, str:String):void {
            point = _p;
            dir = str;
        }
    }
    
    
    class AddRoomCommand
    {
        public var root:RoomNode;
        public var baseSprite:Sprite;
        public var nodes:Vector.<RoomNode>;
        
        public function AddRoomCommand(_r:RoomNode, _b:Sprite, _a:Vector.<RoomNode>) {
            root = _r;
            baseSprite = _b;
            nodes = _a;
        }
        
        public function execute(flag:String, _c:RoomNode = null):Boolean {
            trace("" );
            trace("[AddRoomCommand].execute -- " );
            trace(flag);
            
            var child:RoomNode;
            if (_c) {
                child = _c;
            }else {
                child = new RoomNode(nodes);
            }
            var childrect:Rectangle = child.getRect(child.stage);
            var rootrect:Rectangle = root.getRect(root.stage);
            baseSprite.addChild(child);
            
            if (flag == RoomNode.DIR[0]) {
                //left
                child.xx = root.xx - root.ww / 2 - child.ww / 2;
                child.yy = root.yy + getRandomFromMinus(root.hh/2 + child.hh/2 - 20);
                childrect = child.getRect(child.stage);
                
            
            }else if (flag == RoomNode.DIR[1]) {
            //right
                child.xx = (root.xx + root.ww / 2 + child.ww / 2);
                child.yy = (root.yy + getRandomFromMinus(root.hh / 2 + child.hh / 2 - 20));
                childrect = child.getRect(child.stage);
            
            }else if (flag == RoomNode.DIR[2]) {
            //top
                child.xx = root.xx + getRandomFromMinus(child.ww/2 + root.ww / 2 - 20);
                child.yy = root.yy - root.hh/2 - child.hh/2 
                childrect = child.getRect(child.stage);
                
            }else if (flag == RoomNode.DIR[3]) {
            //bottom
                child.xx = root.xx + getRandomFromMinus(child.ww/2 + root.ww / 2 - 20);
                child.yy = root.yy + root.hh/2 + child.hh/2 
                childrect = child.getRect(child.stage);
            }else {
                throw new Error("addroomcommand error");
            }
            //makeCheckRoom(child, root, flag);
            if ( checkOverlap(flag, child) ) {
                trace("checkOverlap : true");
                child.redraw();
                if ( fitNearRoom(flag, child)) {
                    nodes.push(child);
                    
                    
                const time:Number = 0.2;
                 var mat:Matrix = child.transform.matrix;
                
                    if (child.doors.length > 0) {
                        TweenMax.from(mat, time, {a:0, b:0, c:0, d:0, tx:child.doors[0].point.x, ty:child.doors[0].point.y, onUpdate:onUpdate, onUpdateParams:[child,mat]});
                        //TweenMax.from(child, time, {transformMatrix:{a:0, b:0, c:0, d:0, tx:child.doors[0].point.x, ty:child.doors[0].point.y}});
                    }else    if (flag == RoomNode.DIR[0]) {
                        TweenMax.from(mat, time, {a:0, b:0, c:0, d:0, tx:child.xx+child.ww/2, ty:child.yy, onUpdate:onUpdate, onUpdateParams:[child,mat]});
                    }else if (flag == RoomNode.DIR[1]) {
                        TweenMax.from(mat, time, {a:0, b:0, c:0, d:0, tx:child.xx-child.ww/2, ty:child.yy, onUpdate:onUpdate, onUpdateParams:[child,mat]});
                    }else if (flag == RoomNode.DIR[2]) {
                        TweenMax.from(mat, time, {a:0, b:0, c:0, d:0, tx:child.xx, ty:child.yy+child.hh/2, onUpdate:onUpdate, onUpdateParams:[child,mat]});
                    }else if (flag == RoomNode.DIR[3]) {
                        TweenMax.from(mat, time, {a:0, b:0, c:0, d:0, tx:child.xx, ty:child.yy-child.hh/2, onUpdate:onUpdate, onUpdateParams:[child,mat]});
                    }                    moving = true;
                    //TweenMax.to(baseSprite, 5, { x:baseSprite.x - 290 , ease:Sine.easeInOut} );
                    if(Math.random() > 0.8) end(child);
                //}
                    return true;
                }else {
                    if(baseSprite.contains(child)) baseSprite.removeChild(child);
                    return false; 
                }
            }else {
                trace("checkOverlap : false");
                if(baseSprite.contains(child)) baseSprite.removeChild(child);
                return false; 
            }
            
            
        }
        private var moving:Boolean = false;
        private function onUpdate(node:RoomNode, mat:Matrix):void {
            node.transform.matrix = mat;
        }        
        private function end(child:RoomNode):void {
            
                        moving = false;
                        for (var i:int = 0; i < nodes.length; i++) 
                        {
                            var po:Point = baseSprite.localToGlobal(new Point(nodes[i].xx, nodes[i].yy));
                            
                            var bb:Boolean = po.x < -30 || po.x > 500 || po.y < -30 || po.y > 500;
                            if ( bb)
                            {
                                baseSprite.removeChild(nodes[i]);
                                nodes.splice(i, 1);
                                i -= 1;
                            }
                        }
        }
        private function fitNearRoom(flag:String, child:RoomNode):Boolean {
            const padd:int = 15;
            var len:int = nodes.length;
            var rootrect:Rectangle = root.getRect(root.stage);
            
            const Prob:Number = 0.8;
            const DoorLen:Number = 16;
            for (var i:int = 0; i < len; i++) 
            {
                if (child != nodes[i]) {
                    
                    var childrect:Rectangle = child.getRect(child.stage);
                    var inter:Rectangle;
                    var otherrect:Rectangle = nodes[i].getRect(nodes[i].stage);
                    
                    if (flag == RoomNode.DIR[0]) {
                        childrect = child.getRect(child.stage);
                        childrect.offset( -padd, 0);
                        childrect.inflate( -1, -1);
                        if ( childrect.intersects(otherrect)) {
                            childrect.inflate( 1, 1);    
                            trace("fit1" );
                            inter = childrect.intersection(otherrect);
                            child.xx = (inter.right + rootrect.left ) / 2;
                            child.ww = (child.xx - otherrect.right) * 2;
                            if(Math.random() > Prob) makeCheckRoom(child, nodes[i], RoomNode.DIR[1]);
                            trace("dir 0 intersetct");
                            eraseDoor(new Rectangle(inter.right - 1, inter.top-DoorLen, 2, inter.height+DoorLen), nodes[i]);
                        }
                        
                        childrect = child.getRect(child.stage);
                        childrect.offset( padd, 0);
                        childrect.inflate( -1, -1);
                        if ( childrect.intersects(otherrect)) {
                            childrect.inflate( 1, 1);    
                            trace("fit1" );
                            inter = childrect.intersection(otherrect);
                            
                            trace("dir 0 intersetct");
                            eraseDoor(new Rectangle(inter.left - 1, inter.top-DoorLen, 2, inter.height+DoorLen), nodes[i]);
                        }
                        
                        
                        childrect = child.getRect(child.stage);
                        childrect.offset(0, -padd);
                        childrect.inflate( -1, -1);
                        if ( childrect.intersects(otherrect)) {
                            childrect.inflate( 1, 1);    
                            trace("fit2" );
                            inter = childrect.intersection(otherrect);
                            child.yy = (otherrect.bottom + childrect.bottom+padd) / 2;
                            child.hh = (child.yy - otherrect.bottom) * 2;
                            if (Math.random() > Prob) makeCheckRoom(child, nodes[i], RoomNode.DIR[3]);
                            eraseDoor(new Rectangle(inter.left-DoorLen, inter.top-1, inter.width+DoorLen, 2), nodes[i]);
                            
                        }
                        childrect = child.getRect(child.stage);
                        childrect.offset(0, padd);
                        childrect.inflate( -1, -1);
                        if ( childrect.intersects(otherrect)) {
                            trace("fit3");
                            childrect.inflate( 1, 1);    
                            //trace("fit3" );
                            //trace(child.yy);
                            //trace(child.hh);
                            inter = childrect.intersection(otherrect);
                            child.yy = (otherrect.top + childrect.top -padd) / 2;
                            child.hh = (otherrect.top-child.yy) * 2;
                            if (Math.random() > Prob) makeCheckRoom(child, nodes[i], RoomNode.DIR[2]);
                            eraseDoor(new Rectangle(inter.left-DoorLen, inter.bottom-1, inter.width+DoorLen, 2), nodes[i]);
                            
                            //trace(child.yy);
                            //trace(child.hh);
                        }
                        
                    }else if (flag == RoomNode.DIR[1]) {
                        
                        
                        childrect = child.getRect(child.stage);
                        childrect.offset( padd, 0);
                        childrect.inflate( -1, -1);
                        if ( childrect.intersects(otherrect)) {
                            childrect.inflate( 1, 1);    
                            trace("fit1" );
                            inter = childrect.intersection(otherrect);
                            child.xx = (otherrect.left + rootrect.right) / 2;
                            child.ww = (otherrect.left - child.xx) * 2;
                            if(Math.random() > Prob) makeCheckRoom(child, nodes[i], RoomNode.DIR[0]);
                            eraseDoor(new Rectangle(inter.left - 1, inter.top-DoorLen, 2, inter.height+DoorLen), nodes[i]);
                        }
                        
                        childrect = child.getRect(child.stage);
                        childrect.offset( -padd, 0);
                        childrect.inflate( -1, -1);
                        if ( childrect.intersects(otherrect)) {
                            childrect.inflate( 1, 1);    
                            trace("fit1" );
                            inter = childrect.intersection(otherrect);
                            
                            eraseDoor(new Rectangle(inter.right - 1, inter.top-DoorLen, 2, inter.height+DoorLen), nodes[i]);
                        }
                        
                        childrect = child.getRect(child.stage);
                        childrect.offset(0, -padd);
                        childrect.inflate( -1, -1);
                        if ( childrect.intersects(otherrect)) {
                            childrect.inflate( 1, 1);    
                            trace("fit2" );
                            inter = childrect.intersection(otherrect);
                            child.yy = (otherrect.bottom + childrect.bottom+padd) / 2;
                            child.hh = (child.yy - otherrect.bottom) * 2;
                            if (Math.random() > Prob) makeCheckRoom(child, nodes[i], RoomNode.DIR[3]);
                            eraseDoor(new Rectangle(inter.left-DoorLen, inter.top-1, inter.width+DoorLen, 2), nodes[i]);
                        }
                        childrect = child.getRect(child.stage);
                        childrect.offset(0, padd);
                        childrect.inflate( -1, -1);
                        if ( childrect.intersects(otherrect)) {
                            childrect.inflate( 1, 1);    
                            trace("fit3" );
                            inter = childrect.intersection(otherrect);
                            child.yy = (otherrect.top + childrect.top -padd) / 2;
                            child.hh = (otherrect.top - child.yy) * 2;
                            
                            if(Math.random() > Prob) makeCheckRoom(child, nodes[i], RoomNode.DIR[2]);
                            eraseDoor(new Rectangle(inter.left-DoorLen, inter.bottom-1, inter.width+DoorLen, 2), nodes[i]);
                        }
                        
                        
                    }else if (flag == RoomNode.DIR[2]) {
                    }else if (flag == RoomNode.DIR[3]) {
                    
                    
                    }else {
                        throw new Error("fit near room error");
                    }
                }
            }
            
            makeCheckRoom(child, root, flag);
            
            child.redraw();
                    if ( child.ww > 20 && child.hh > 20 && checkOverlap(flag, child, true)) {
                        return true;
                    }else {
                        return false;
                    }

        }
        
        private function eraseDoor(rec:Rectangle, node:RoomNode):void {
            
            for (var i:int = 0; i < node.doors.length; i++) 
            {
                if (rec.containsPoint(node.doors[i].point)) {
                    node.doors.splice(i, 1);
                    node.redraw();
                }
            }
            
        }
        
        private function checkOverlap(flag:String, child:RoomNode, recursive:Boolean = false):Boolean {
        //collision detection and adjust size    
            var len:int = nodes.length;
            var childrect:Rectangle = child.getRect(child.stage);
            
            const SIZE:int = 30;
            
            var otherRect:Rectangle;
            var inter:Rectangle;
            
            if (flag == RoomNode.DIR[0]) {
            //left
                for (var i:int = 0; i < len; i++) 
                {
                    otherRect = nodes[i].getRect(nodes[i].stage);
                    
                    otherRect.inflate( -1, -1);
                    var bb:Boolean = childrect.intersects(otherRect);
                    otherRect.inflate(1, 1);
                    if ( bb){
                        inter = childrect.intersection(otherRect);
                        
                        child.ww = (childrect.right - inter.right);
                        child.xx = childrect.right - child.ww / 2;
                        
                        child.redraw();
                        childrect = child.getRect(child.stage);
                    }
                    
                    if (child.ww < 10 ) {
                        
                        if (recursive == false) {
                            child.ww = SIZE + Math.random()*SIZE;
                            child.xx = childrect.right - child.ww / 2;
                            inter = childrect.intersection(otherRect);
                            if (otherRect.top > childrect.top && otherRect.bottom < root.getRect(root.stage).bottom) {
                                child.hh = childrect.height - inter.height;
                                child.yy = otherRect.bottom + child.hh / 2;
                                
                            }else if( otherRect.top > root.getRect(root.stage).top){
                                child.hh = childrect.height - inter.height;
                                child.yy = otherRect.top - child.hh / 2;
                                
                            }
                            child.redraw();
                        
                        
                            if ( checkOverlap(flag, child, true)) {
                                return true;
                            }else {
                                return false;
                            }
                        }
                    }
                    
                }
            
            }else if (flag == RoomNode.DIR[1]) {
            //right
                for (var i:int = 0; i < len; i++) 
                {
                    
                    otherRect = nodes[i].getRect(nodes[i].stage);
                    
                    otherRect.inflate( -1, -1);
                    var bb:Boolean = childrect.intersects(otherRect);
                    otherRect.inflate(1, 1);
                    if ( bb){
                        
                        trace("intersect true");
                        trace(childrect.toString());
                        trace(otherRect.toString());
                        inter = childrect.intersection(otherRect);
                        
                        child.ww = - (childrect.left - inter.left);
                        child.xx = (childrect.left + child.ww / 2);
                        
                        childrect = child.getRect(child.stage);
                        
                    }else {
                    }

                    if (child.ww < 10) {
                        if (recursive == false) {
                            
                            child.ww = (SIZE + Math.random()*SIZE);
                            child.xx = (childrect.left + child.ww / 2);
                            inter = childrect.intersection(otherRect);
                            
                            if (otherRect.top > childrect.top && otherRect.bottom < root.getRect(root.stage).bottom) {
                                child.hh = childrect.height - inter.height;
                                child.yy = (otherRect.bottom + child.hh / 2);
                                
                                
                            }else if(otherRect.top > root.getRect(root.stage).top) {
                                child.hh = childrect.height - inter.height;
                                child.yy = (otherRect.top - child.hh / 2);
                                
                            }
                            //child.redraw();
                        
                        
                            if ( checkOverlap(flag, child, true)) {
                                return true;
                            }else {
                                return false;
                            }
                        }

                    }
                    
                    
                }
            
            }else if (flag == RoomNode.DIR[2]) {
            //top
            
                for (var i:int = 0; i < len; i++) 
                {
                    otherRect = nodes[i].getRect(nodes[i].stage);
                    
                    otherRect.inflate( -1, -1);
                    var bb:Boolean = childrect.intersects(otherRect);
                    otherRect.inflate(1, 1);
                    if ( bb){
                        otherRect.inflate( 1, 1);
                        inter = childrect.intersection(otherRect);
                        
                        child.hh = (childrect.bottom - inter.bottom);
                        child.yy = childrect.bottom - child.hh / 2;
                        child.redraw();
                        childrect = child.getRect(child.stage);
                    }
                    
                    if (child.hh < 10) {
                        
                        if (recursive == false) {
                            
                            child.hh = SIZE + Math.random()*SIZE;
                            child.yy = childrect.bottom - child.hh / 2;
                            inter = childrect.intersection(otherRect);
                            
                            if (otherRect.right > childrect.right && otherRect.left > root.getRect(root.stage).left) {
                                child.ww = childrect.width - inter.width;
                                child.xx = childrect.left + child.ww / 2;
                                
                                
                            }else if(otherRect.left > root.getRect(root.stage).left){
                                child.ww = childrect.width - inter.width;
                                child.xx = otherRect.right - child.ww / 2;
                                
                            }
                            child.redraw();
                        
                        
                            if ( checkOverlap(flag, child, true)) {
                                return true;
                            }else {
                                return false;
                            }
                        }

                    }
                }
                
            }else if (flag == RoomNode.DIR[3]) {
            //bottom
                for (var i:int = 0; i < len; i++) 
                {
                    otherRect = nodes[i].getRect(nodes[i].stage);
                    
                    otherRect.inflate( -1, -1);
                    var bb:Boolean = childrect.intersects(otherRect);
                    otherRect.inflate(1, 1);
                    if ( bb){
                        
                        inter = childrect.intersection(otherRect);
                        
                        child.hh = - (inter.top - childrect.top);
                        child.yy = childrect.top + child.hh / 2;
                        child.redraw();
                        childrect = child.getRect(child.stage);
                        
                    }
                    
                    if (child.hh < 10) {
                        
                        if (recursive == false) {
                            
                            child.hh = SIZE + Math.random()*SIZE;
                            child.yy = childrect.top + child.hh / 2;
                            inter = childrect.intersection(otherRect);
                            
                            if (otherRect.right > childrect.right && otherRect.left > root.getRect(root.stage).left) {
                                child.ww = childrect.width - inter.width;
                                child.xx = otherRect.left + child.ww / 2;
                                
                            }else if(otherRect.left > root.getRect(root.stage).left){
                                child.ww = childrect.width - inter.width;
                                child.xx = otherRect.right - child.ww / 2;
                                
                            }
                            child.redraw();
                        
                        
                            if ( checkOverlap(flag, child, true)) {
                                return true;
                            }else {
                                return false;
                            }
                        }

                    }
                }
            }else {
                throw new Error("addroomcommand error");
            }            
            
            if (child.ww < 15 || child.hh < 15 || child.xx < -30 || child.xx > 500 || child.yy < -30 || child.yy > 500) {
                return false;
            }else {
                return true;
            }
        }
        
        
        public function makeCheckRoom(child:RoomNode, root:RoomNode, dir:String):void {
            const Max:Number = 24;
            
            var doorPo:Point;
            
            
            var childrect:Rectangle;
            
            if (dir == RoomNode.DIR[0]) {
                childrect = child.getRect(child.stage);
                var child2rect:Rectangle = child.getRect(child.stage);
                child2rect.offset(5, 0);
                var rootrect:Rectangle = root.getRect(root.stage);
                var inter2rect:Rectangle = rootrect.intersection(child2rect);
                
                
                if (inter2rect.height > Max) {
                    doorPo = new Point(childrect.right, inter2rect.top + 5 + Math.random() * (inter2rect.height - Max));
                    child.doors.push(new DoorObj(doorPo, RoomNode.DIR[0]));
                    
                }else {
                    //child.doorPoint = null;
                }

                
                
            }else if(dir == RoomNode.DIR[1]) {
                childrect = child.getRect(child.stage);
                var child2rect:Rectangle = child.getRect(child.stage);
                child2rect.offset(-5, 0);
                var rootrect:Rectangle = root.getRect(root.stage);
                var inter2rect:Rectangle = rootrect.intersection(child2rect);
                
                
                if (inter2rect.height > Max) {
                    doorPo = new Point(childrect.left, inter2rect.top +5 + Math.random() * (inter2rect.height - Max));
                    child.doors.push(new DoorObj(doorPo, RoomNode.DIR[0] ));
                    
                }else {
                    //child.doorPoint = null;
                }
            }else if(dir == RoomNode.DIR[2]) {
                childrect = child.getRect(child.stage);
                var child2rect:Rectangle = child.getRect(child.stage);
                child2rect.offset(0, 5);
                var rootrect:Rectangle = root.getRect(root.stage);
                var inter2rect:Rectangle = rootrect.intersection(child2rect);
                
                
                if (inter2rect.width > Max) {
                    doorPo = new Point(inter2rect.left + 5 + Math.random() * (inter2rect.width - Max), childrect.bottom);
                    child.doors.push(new DoorObj(doorPo, RoomNode.DIR[2]));
                    
                    
                }else {
                    //child.doorPoint = null;
                }
            }else if (dir == RoomNode.DIR[3]) {
                childrect = child.getRect(child.stage);
                var child2rect:Rectangle = child.getRect(child.stage);
                child2rect.offset(0, -5);
                var rootrect:Rectangle = root.getRect(root.stage);
                var inter2rect:Rectangle = rootrect.intersection(child2rect);
                
                
                if (inter2rect.width > Max) {
                    doorPo = new Point(inter2rect.left + 5 + Math.random() * (inter2rect.width - Max), childrect.top);
                    child.doors.push(new DoorObj(doorPo, RoomNode.DIR[2]));                    
                }else {
                    //child.doorPoint = null;
                }
            }
            
            
        }
        
        public static function getRandomFromMinus(num:Number):Number {
            return -num + Math.random() * num * 2;
        }
        
    }