Bouncing Bitmap Data, Efficiency

I was curious as to how well the BitmapData object performed, I’m impressed with this one.

This is a good example of how to create visual objects that are not vector, yet are easily manipulated by a class.

We will keep creating balls that constantly are moving and bouncing around the screen. The efficiency comes in part that these balls are represented only as data, we are actually looping through all of the balls, and asking for their information then placing it on the screen manually.

BouncyBall.as


import flash.display.BitmapData;
import flash.geom.Rectangle;

class BouncyBall
{
   private static var BOUNCE:Number = -1;

   private var graphic:Rectangle; // the bitmap data that represents this object
   
   private var x:Number;
   private var y:Number;
   private var radius:Number = 2;

   private var vx:Number;
   private var vy:Number;

   private var limits:Rectangle;

   private var animationIntervalID:Number;

   /**
    * Constructor
    *
    * @param The starting X position of this ball.
    * @param The starting Y position of this ball.
    * @param The starting speed of this ball on the x axis.
    * @param The starting speed of this ball on the y axis.
    * @param A rectangle object that represents the limits that this ball is bound to
    * @param The movieclip that the ball will be drawn into.
    */
   public function BouncyBall( radius:Number, startX:Number, startY:Number, startSpeedX:Number, startSpeedY:Number, limits:Rectangle )
   {
      this.radius = radius;
      
      this.x = startX;
      this.y = startY;

      this.vx = startSpeedX;
      this.vy = startSpeedY;
      
      this.limits = limits;

      graphic = new Rectangle( x, y, radius, radius);


      //this.target.attachBitmap( graphic, 100 );

      beginMoving();
   }

   /**
    * Returns the bitmap data that represents this ball
    */
   public function get bitmap() : Rectangle
   {
      graphic.x = x;
      graphic.y = y;
      return graphic.clone();
   }

   /**
    * Moves the ball
    */
   private function beginMoving() : Void
   {
      animationIntervalID = setInterval(this, "move", 40);
   }
   private function move() : Void
   {
      this.x += vx;
      this.y += vy;

      if (x + radius > limits.right || x < limits.left)
      {
         vx *= BouncyBall.BOUNCE;
      }
      if (y + radius > limits.bottom || y < limits.top)
      {
         vy *= BouncyBall.BOUNCE;
      }
   }
   
}

The code that is in the .fla


import flash.display.BitmapData;
import flash.geom.Rectangle;

var balls:Array = new Array();

var dupID = setInterval(this, "newBall", 10);

var game = new BitmapData(550, 400, true, 0xFFFFFFFF);
var cleanUP = new BitmapData(550, 400, true, 0xFFFFFFFF);

test.setPixel( Stage.width/2, Stage.height/2, 0xFF0000 );

attachBitmap( game, 100 );

function newBall()
{
    balls.push(new BouncyBall(3, 20, 20, 5, 5, new Rectangle(0, 0, 550, 400), this));
}

function onEnterFrame()
{
    game.draw( cleanUP );
    
    for (var i:Number=0; i<balls.length; i++)
    {
        game.fillRect( balls*.bitmap, Math.random() * 0xFFFFFFFF );
    }
}

I let this run for over a half an hour with absolutely no loss in speed.

Take Care.
_Michael