Attaching a Bitmap – AS2 vs AS3

A common task that has changed drastically in ActionScript 3.0 is loading a library image via its linkageID into a BitmapData object and attaching it to a movieclip. In ActionScript 2.0, this can be done like so:

import flash.display.BitmapData
var bmp:BitmapData = BitmapData.loadBitmap("linkageID");
var img = createEmptyMovieClip("img",0);

In ActionScript 3.0 movieclips and bitmaps are not attached or created like this – everything is created with the new keyword. Also, a BitmapData object should be dropped into a new Bitmap, which can be added to the stage with the all-important addChild() method.

Since a library bitmap inherits from BitmapData, you now set the class of the bitmap its Linkage Properties in Flash CS3, instantiate it and wrap it in a Bitmap object that you add to the stage:

Attaching a bitmap in AS3

Where ‘Butterfly’ refers to the bitmap library item you’ve given the class definition ‘Butterfly’. The Base class is generated automatically. These five lines of code can be shortened to one, but is less readable:

var img = addChild(new Bitmap(new Butterfly(0,0)));

Note that in Flex Builder, unlike Flash CS3, you would embed an image using the embed meta tag, associating it with a variable, instead of its Linkage Properties dialog:

[Embed(source='image.jpg')] public var Butterfly:Class;

Building SWFs in Flex Builder is a whole different topic – so stay tuned!

4 thoughts on “Attaching a Bitmap – AS2 vs AS3”

  1. I cant see how to set the transparency of the linked image to true? you cant use bmp.transparent=true, its read only. I’m using a work around of creating another transparent bitmapdata, then drawing the bitmap from the library onto it, but it seems a bit daft. Am I missing something really obvious?

  2. Ah yes, without playing around with code right this minute, I can’t say for certain… but you need to create a BitmapData object and make sure the transparent flag is set to true when you create it. Don’t confuse Bitmap and BitmapData. A Bitmap object contains a property bitmapData (which is an BitmapData object), but a BitmapData object can exist on its own. If that makes sense.

  3. So in my library I have a bitmap that I created the linkage name card1. I don’t have a card1 class but it says it creates them for me. However when I try and create an object of the bitmap card1(1,1) it throws the error Call to possibly undefined method. Am I missing something? Is there a way around this?

  4. public function cloneMovieClip(original:MovieClip, clone:MovieClip){
    var tempData = new BitmapData(original.width, original.height, false, 0xffffff);
    var tempBitmap:Bitmap = new Bitmap(tempData);

Leave a Reply