flash on 2016-12-16

by mutantleg
♥0 | Line 120 | Modified 2016-12-16 05:43:21 | MIT License
play

ActionScript3 source code

/**
 * Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/aANv
 */

package {
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            
            var i:int; var k:int; var num:int;
            var a:xTile; var yt:int;
            mw=16;mh=16; 
            num=mw*mh;
            vecGrid = new Vector.<xTile>(num,false);
            for(i=0;i<num;i+=1){ vecGrid[i] = new xTile(); }
           for (i=0;i<mh;i+=1) { 
               yt = mw * i;
               for (k=0;k<mw;k+=1) {  a = vecGrid[yt+k]; a.tx=k;a.ty=i; }
             }//nexti
            
          stage.addEventListener(MouseEvent.MOUSE_DOWN, onMdown);
          stage.addEventListener(MouseEvent.MOUSE_UP, onMup);  
          stage.addEventListener(Event.ENTER_FRAME, onEnter);            
        }//ctor
        
        public function onMdown(e:MouseEvent):void { mbutton = 1; }
        public function onMup(e:MouseEvent):void { mbutton = 0; }
        
        public var mbutton:int = 0;
        public var mx:Number = 0;
        public var my:Number = 0;
        
        public var mhold:int = 0;
        
        public var vecGrid:Vector.<xTile>// = new Vector.<int>(16*16,false);
        public var mw:int = 16;
        public var mh:int = 16;
        public var cw:Number = 32;
        public var ch:Number = 32;
        
        public function setTile(ax:int, ay:int, t:int):void
        { if (ax<0||ax>=mw||ay<0||ay>=mh){return;} vecGrid[ax+(ay*mw)].col = t;  }
        
        public function getTile(ax:int, ay:int):xTile
        { if (ax<0||ax>=mw||ay<0||ay>=mh){return null;} return vecGrid[ax+(ay*mw)];  }
        
        
        
        public function onEnter(e:Event):void
        {
           graphics.clear();
           graphics.lineStyle(1,0);
           
            mx = stage.mouseX;
            my = stage.mouseY;
            
            var tx:int; var ty:int;
            tx = Math.floor(mx/cw);
            ty = Math.floor(my/ch);
            
            
          var a:xTile;
          var yt:int; var t:int;
          var i:int; var k:int;
          for (i=0;i<mh;i+=1)
          {
           yt = mw * i;
           for (k=0;k<mw;k+=1)
           {
             //graphics.drawRect(k*cw, i*ch, cw, ch);
             a = getTile(k,i);
             if (a.col>0)
             {
              graphics.beginFill(a.col,1);
               graphics.drawRect(k*cw, i*ch, cw, ch);             
              graphics.endFill();    
             }//endif
             
           }//nextk  
          }//nexti    
          
          graphics.lineStyle(4, 0xFF);
          graphics.drawRect(tx*cw,ty*ch,cw,ch);

          if (mbutton > 0) { mhold += 1; }
          else { mhold = 0; }
          
          a = getTile(tx,ty);
          if (a != null && mhold == 1)
          { 
           if (a.col == 0)
           {
            a.col = Math.random()*0xFFffFF; 
            checkGrp(a.tx, a.ty);
           }
           else
           {
             a.col=0; a.grp=0;  
            checkGrp(a.tx-1, a.ty);
            checkGrp(a.tx+1, a.ty);
            checkGrp(a.tx, a.ty+1);
            checkGrp(a.tx, a.ty-1);
           }//endif2
          }//endif
                      
        }//onenter


        public var vecTemp:Vector.<xTile> = new Vector.<xTile>(512, false);
        public var curGrp:int = 1;
        //public var curTest:int = 0;

        public function checkGrp(ax:int, ay:int):void
        {
          var it:int; var a:xTile; 
          var i:int;  var b:xTile;
          
          a = getTile(ax,ay);
          if (a==null) { return; }
          if (a.col==0) { return; }
          curGrp += 1;    
          a.grp = curGrp;
          a.col = Math.random()*0xFFffFF; 
          vecTemp[0] = a;
          it = 1;
          
          for (i=0; i < 512; i+=1)
          {
            it -= 1; if (it <0){return;}
            a = vecTemp[it];  
            //a.grp = curGrp;
            b = getTile(a.tx-1, a.ty);
            if (b.grp != curGrp && b.col>0) { vecTemp[it] = b; it+=1; b.grp = curGrp; b.col=a.col;  }
            b = getTile(a.tx+1, a.ty);
            if (b.grp != curGrp && b.col>0) { vecTemp[it] = b; it+=1; b.grp = curGrp; b.col=a.col;  }
            b = getTile(a.tx, a.ty+1);
            if (b.grp != curGrp && b.col>0) { vecTemp[it] = b; it+=1; b.grp = curGrp; b.col=a.col;  }
            b = getTile(a.tx, a.ty-1);
            if (b.grp != curGrp && b.col>0) { vecTemp[it] = b; it+=1; b.grp = curGrp; b.col=a.col;  }
                        
          }//nexti 
         
            
            
        }//checkgrp

        
        
    }//classend
}

internal class xTile 
{
 public var tx:int = 0;
 public var ty:int = 0;
 public var col:int = 0;
 public var grp:int = 0;
// public var test:int = 0;
}//xtile