Moving balls


I did change in my previous example and make Balls moveable
  
 
  
 package 
  
 {
  
     import flash.display.Graphics;
  
     import flash.display.MovieClip;
  
     /**
  
      * ...
  
      * @author Jeet Chauhan
  
      */
  
     public class BallWithParameter extends MovieClip
  
     {
  
         
  
         private var color:uint;
  
         private var radius:Number;
  
         
  
         public function BallWithParameter(radius:Number = 10,color:uint=0xffff00) 
  
         {
  
             this.color = color;
  
             this.radius = radius;
  
             draw()
  
         }
  
         
  
         private function draw():void
  
         {
  
             var g:Graphics = graphics;
  
             g.beginFill(color, 1);
  
             g.drawCircle(0, 0, radius);
  
         }
  
         /**
  
          * this will set position of current instance
  
          * @param    dx
  
          * @param    dy
  
          */
  
         public function setPosition(dx:Number, dy:Number):void
  
         {
  
             this.x = dx;
  
             this.y = dy;
  
         }
  
         
  
         /**
  
          * this will move current instance 
  
          * @param    dx
  
          * @param    dy
  
          */
  
         public function moveBy(dx:Number, dy:Number):void
  
         {
  
             this.x += dx;
  
             this.y += dy;
  
         }
  
         
  
     }
  
 
  
 }
  
 
  
 
  
 
  
 package 
  
 {
  
     import flash.display.MovieClip;
  
     import flash.events.Event;
  
     
  
     /**
  
      * ...
  
      * @author Jeet Chauhan
  
      */
  
     public class MultipleBall extends MovieClip
  
     {
  
         private var balls:Array;
  
         public function MultipleBall() 
  
         {
  
             draw()
  
         }
  
         
  
         private function draw():void
  
         {
  
             balls = new Array();
  
             for (var i:int = 0; i < 100; i++) 
  
             {
  
                 var ball:BallWithParameter = new BallWithParameter(Math.random() * 5 + 5, Math.random() * 0xffffff);
  
                 balls.push(ball);
  
                 //ball.setPosition(Math.random() * stage.stageWidth,Math.random() * stage.stageHeight)
  
                 ball.x = Math.random() * stage.stageWidth;
  
                 ball.y = Math.random() * stage.stageHeight;
  
                 addChild(ball);
  
             }
  
             stage.addEventListener(Event.ENTER_FRAME, animate);
  
         }
  
         
  
         private function animate(e:Event):void 
  
         {
  
             for (var i:int = 0; i < 100; i++) 
  
             {
  
                 BallWithParameter(balls[i]).moveBy(Math.random() * 10 - 5, Math.random() * 10 - 5);
  
             }
  
         }
  
         
  
     }
  
 
  
 }  

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s