flash on 2015-7-6

by mutantleg
♥0 | Line 89 | Modified 2015-07-06 08:11:49 | 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/dBUQ
 */

package {
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
          
          mw = 12; mh = 12;
          vecGrid = new Vector.<int>(mw*mh, false);

          var i:int; 
          for (i=0;i<mw;i++)  { vecGrid[i] = 1; vecGrid[i*mw]=1; vecGrid[i*mw+11]=1; vecGrid[11*mw+i]=1; }          
    
           vecGrid[12*3+3] = 1;           
           vecGrid[12*3+8] = 1;
           vecGrid[12*8+3] = 1;
           vecGrid[12*8+8] = 1;
          
          gridAct = new Vector.<xActor>(mw*mh,false);
          
          vecAct = new Vector.<xActor>(0, false);
          var a:xActor;
          a = new xActor(); vecAct.push(a); a.tx =3; a.ty =2; a.c = 0x0000FF;
          a = new xActor(); vecAct.push(a); a.tx =4; a.ty =2; a.c = 0x00F0FF;
          a = new xActor(); vecAct.push(a); a.tx =5; a.ty =2; a.c = 0xF000FF;
          a = new xActor(); vecAct.push(a); a.tx =6; a.ty =2; a.c = 0xF00F3F;
          
          for (i=0;i<8;i++)
          {   a = new xActor(); vecAct.push(a); a.tx =1+i; a.ty =1; a.c = Math.random()*0xffFFffFF; }
          
            stage.addEventListener(Event.ENTER_FRAME, onEnter);
        }//ctor

        public var gt:int =0;
        
        public var vecGrid:Vector.<int>;
        public var mw:int = 0;         public var mh:int = 0;
        public var cw:Number = 32;     public var ch:Number = 32;

        public function isWall(ax:int, ay:int):Boolean
        { if (ax<0||ax>=mw||ay<0||ay>=mh){return true;}  return vecGrid[ay*mw+ax] > 0;  }

        public function getAct(ax:int, ay:int):xActor
        { if (ax<0||ax>=mw||ay<0||ay>=mh){return null;}  return gridAct[ay*mw+ax];  }

        public function setAct(ax:int, ay:int, a:xActor):void
        { if (ax<0||ax>=mw||ay<0||ay>=mh){return;} gridAct[ay*mw+ax] = a ;  }

 
        public var gridAct:Vector.<xActor>;
 
        public var vecAct:Vector.<xActor>;        
        
        public function onEnter(e:Event):void
        {
          graphics.clear();
          graphics.lineStyle(2, 0);  
            
          var i:int; var k:int; var yt:int; var t:int;
          for (i=0;i<mh;i++)
          {
           yt = i * mw;
           for (k=0;k<mw;k++)
           {
             t = vecGrid[k+yt];
             if (t > 0)
             {
               graphics.beginFill(0x404040, 1);
                 graphics.drawRect(k*cw,i*ch,cw,ch);               
               graphics.endFill();  
                 continue;
             }//endif
             //graphics.drawRect(k*cw,i*ch,cw,ch);
              
           }//nextk
          }//nexti  

      
         var tx:int; var ty:int;  
          var num:int; var a:xActor;
          
          num=vecAct.length;
          if (gt % 8 == 0)
          for (i=0;i<num;i++)
          {
             a = vecAct[i];
             
             tx = a.tx; ty = a.ty;
             if (getAct(tx,ty)==a) { setAct(tx,ty, null); }    
             if (Math.random() <0.5)
             { tx += Math.random() <0.5 ? 1:-1; }
             else { ty += Math.random() <0.5 ? 1:-1; }
             if (isWall(tx,ty)==false && getAct(tx,ty)==null)   {a.tx=tx;a.ty=ty;}             
             if (getAct(a.tx,a.ty)==null) { setAct(a.tx,a.ty, a); }
          }//nexti

          num=vecAct.length;
          for (i=0;i<num;i++)
          {
             a = vecAct[i];
             graphics.beginFill(a.c, 0.5);
               graphics.drawRect(a.tx*cw,a.ty*ch,cw,ch);               
             graphics.endFill();               
          }//nexti

            gt += 1;             
        }//onenter
        
        
    }//classend
}

internal class xActor
{
  public var tx:int = 0;
  public var ty:int = 0;  
  public var c:uint = 0x0000ff;
}//xactor