flash on 2016-5-18

by mutantleg
♥0 | Line 76 | Modified 2016-05-18 06:23:39 | 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/gZdL
 */

package {
    import flash.ui.Keyboard;
    import flash.events.KeyboardEvent;
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {

          vecSpr = new Vector.<xSpr>(0,false);
          var i:int; var a:xSpr; var num:int;
          for(i=0;i<64;i+=1)
          {
            a = new xSpr();
            a.cx = 465*3 * Math.random()  - 465;
            a.cy = Math.random()*300+50;
            vecSpr.push(a);  
          }//nexti

          stage.addEventListener(KeyboardEvent.KEY_DOWN, onKdown);
          stage.addEventListener(KeyboardEvent.KEY_UP, onKup);
          stage.addEventListener(Event.ENTER_FRAME, onEnter);
        }//ctor
        
        public var vecKey:Vector.<Boolean> = new Vector.<Boolean>(512,false);
        public function onKdown(e:KeyboardEvent):void { vecKey[e.keyCode]=true; }
        public function onKup(e:KeyboardEvent):void { vecKey[e.keyCode]=false; }
        public function isKeyDown(k:int):Boolean { return vecKey[k]; }
        
        public var cx:Number = 230;
        public var cy:Number = 230;
        public var dx:Number = 0;
        
        public var vx:Number=0;
        public var vy:Number=0;
        
        public var vecSpr:Vector.<xSpr>;
        
        public function onEnter(e:Event):void
        {
            graphics.clear();            
            graphics.lineStyle(2, 0);
            
            
            graphics.drawCircle(cx,cy, 16);
            
            graphics.drawRect(0,300,465,165);
            
            vx*=0.5;vy*=0.5;
            
            var ms:Number; ms = 8;
//            if (isKeyDown(Keyboard.LEFT)) { cx-=ms; dx+=1; }
 //           if (isKeyDown(Keyboard.RIGHT)) { cx+=ms; dx-=1; }
  //          if (isKeyDown(Keyboard.UP)) { cy-=ms; }
   //         if (isKeyDown(Keyboard.DOWN)) { cy+=ms; }
            if (isKeyDown(Keyboard.LEFT)) { vx=-ms; dx+=0.5; }
            if (isKeyDown(Keyboard.RIGHT)) { vx=ms; dx-=0.5; }
            if (isKeyDown(Keyboard.UP)) { vy=-ms; }
            if (isKeyDown(Keyboard.DOWN)) { vy=ms; }
            
            cx+=vx; cy+=vy;

            var kx:Number;
            kx = 0;
            /*
            if (cx < 200) { kx=200-cx; cx=200; }                        
            if (cx > 300) { kx=300-cx; cx=300; }            
            */
            var ax:Number;ax=235;
            ms=64; ms=32;
            if (dx>ms){dx=ms;} else if (dx<-ms){dx=-ms;}
            ax+=dx;
            if (cx<ax||cx>ax){kx=ax-cx; cx=ax;}
            
            
            var w:Number;
            w=465*3;
            var i:int; var num:int; var a:xSpr;
            num=vecSpr.length;
            for(i=0;i<num;i+=1)
            {
              a = vecSpr[i];
               
               a.cx+=kx;
               if (a.cx<-465){a.cx+=w;}
               else if (a.cx>465*2){a.cx-=w;}
                
                a.cy+=a.vy;
                if (a.vy==0){a.vy=Math.random()-0.5}
                else if (a.vy<0 &&a.cy<40){a.vy*=-1;}
                else if (a.vy>0&&a.cy>300){a.vy*=-1;}
                
              graphics.drawCircle(a.cx,a.cy, 8);  
              
              
              graphics.drawCircle(235+a.cx/16,a.cy/16,2);
              
                              
            }//nexti
            
            
        }//onenter
        
    }//classend
}

internal class xSpr
{
  public var cx:Number = 0;
  public var cy:Number = 0;  
  public var vy:Number = 0;
    
}//xspr