Blitting - attach as file to instances of bitmapdata
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Blitting - attach as file to instances of bitmapdata

  1. #1
    Member
    Join Date
    May 2006
    Posts
    70

    Blitting - attach as file to instances of bitmapdata

    I'm new at blitting, so hopefully I explain this right.

    I'm basically creating a project that takes a simple image, duplicates it, and sends it in a random direction. Now normally I can get this working, but I need to get the performance better so I am trying to do it with blitting.

    Previously, I had a movieclip, and I added it to the stage as many times as I needed. And in the library, that movieclip was linked to a .as file that gave the movieclip it's random direction. And this worked fine.

    So now on to my blitting. I have been following this tutorial for the most part:
    http://www.adobe.com/devnet/flash/ar...itting_mc.html

    And here is my code:
    Code:
    package 
    {
    	import flash.display.MovieClip;
    	import flash.display.Bitmap;
    	import flash.display.BitmapData; 
    	import flash.geom.*;
    	import flash.events.Event;
    
    	public class myClass extends MovieClip
    	{
    		
    		public var canvasBitmap:Bitmap;
    		public var canvasBitmapData:BitmapData; 
    		public var canvasRectangle:Rectangle = new Rectangle(0,0,550,400);
    		
    		public var circle:mcCircle;
    		
    		public function myClass():void{
    			
    			circle = new mcCircle();
    
    			
    			canvasBitmap = new Bitmap(); 
    			canvasBitmapData = new BitmapData(550, 400); 
    			canvasBitmapData.fillRect(canvasRectangle,0xFFDDDDDD) 
    			canvasBitmap.bitmapData = canvasBitmapData; 
    			canvasBitmap.x = 0; 
    			canvasBitmap.y = 100; 
    			addChild(canvasBitmap);
    			
    			
    			addEventListener(Event.ENTER_FRAME, onEnterFrame);
    			
    			
    		}
    		
    		
    		public function onEnterFrame(evt:Event):void { 
    		
    
    		canvasBitmapData.fillRect(canvasRectangle, 0xFFDDDDDD);
    		
    	///first circle
    			var cirlceBitmapData:BitmapData = new BitmapData(circle.width, circle.height); 
    			cirlceBitmapData.draw(circle);
    	
    			var areaRectangle:Rectangle = new Rectangle(); 
    			areaRectangle.width = star.width; 
    			areaRectangle.height = star.height; 
    			var destinationPoint:Point = new Point(circle.x, circle.y); 
    			canvasBitmapData.copyPixels(cirlceBitmapData,areaRectangle,destinationPoint
    
    
    ////second circle
    var cirlceBitmapData:BitmapData = new BitmapData(circle.width, circle.height); 
    			cirlceBitmapData.draw(circle);
    	
    			var areaRectangle:Rectangle = new Rectangle(); 
    			areaRectangle.width = star.width; 
    			areaRectangle.height = star.height; 
    			var destinationPoint:Point = new Point(circle.x, circle.y); 
    			canvasBitmapData.copyPixels(cirlceBitmapData,areaRectangle,destinationPoint
    );
    			
    		}
    		
    		
    	}
    }

    I plan on cleaning up the code where it adds both circles, so please ignore that for now. So this works, and the new bitmaps both use the random .as file that is attached to my movieclip in the library. The problem is that they both go in the same exact direction. So I need to figure out how to attach the .as file to each individual instance of the bitmapdata so that they each go a different direction.

    Hope that made sense!! And thanks for any help!

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    That's not how it works. You don't attach a class to a Bitmap.
    Instead, think of it as replacing the rendering system. Your Circle (or whatever) class no longer needs to be a DisplayObject because it won't be put on the displaylist. Either your main instance will use the information from each Circle to draw into a common bitmap, or you'll pass the bitmap(data) to each instance of Circle, and they will draw their own representations into it.

  3. #3
    Member
    Join Date
    May 2006
    Posts
    70
    I'm not sure I understand. I guess I'm just not following how I would animate these bitmaps.

    Let's say for this example that I want one of the circles to move to the left, and the other to move to the right. How would I accomplish this with my code above?

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your posted code is incomplete and buggy, so you wouldn't.

    Let's try to code up a simple implementation of Circle which you could use to replace mcCircle.
    Basically, keep all the logic and non-visual stuff. Toss anything to do with the display list. Don't inherit from Sprite or MovieClip (because you're not going to use displaylist or frame stuff). DO provide a method to blit into a provided BitmapData. In fact, let's make an interface for that.
    Code:
    public interface Blittable {
      public function blitInto(BitmapData bd):void;
    }
    Code:
    public class Circle implements Blittable{
      //not shown: all your existing logic/variables, etc.
      //you'll probably have to declare some stuff that you previously inherited like x, and y.
    
      private var asset:BitmapData;
      private var matrix:Matrix;
      private var _x:Number;
      private var _y:Number;
    
      public function Circle(radius:int){
         asset = new BitmapData(radius*2, radius*2); 
         //not shown: draw into your asset. 
         matrix = new Matrix();
         _x = 0;
         _y = 0;
      }
    
      /**
      *draw this Circle into the provided BitmapData
      **/
      public function blitInto(bd:BitmapData):void{
         bd.draw(asset, matrix);
      }
    
      public function get x():Number{
        return _x;
      }
    
      public function get y():Number{
       return _y;
      }
    
      //alter matrix so that it reflects new x value.
      public function set x(v:Number):void{
        _x = v;
        matrix.tx = _x;
      }
    
      public function set y(v:Number):void{
        _y = v;
        matrix.ty = _y;
      }
    }
    And you'd use it from your main like this:
    Code:
    firstCircle.blitInto(canvasBitmapData);
    secondCircle.blitInto(canvasBitmapData);
    I'd probably create a pseudo-abstract ancestor class for all my Blittables to handle the common stuff like blitInto and the matrix manipulation.
    Also, if each instance of your class is going to share the same asset, you can make it static. But don't do that if they change appearance at all since all of them would change at the same time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width


HTML5 Development Center

Poll by Flashkit.com