Classed stage objects with empty library

Every class is compiled into a swf in the same way that we used to include AS1 classes, by putting them inside an #initclip block of a linked movieclip. The linkage name of the movieclip is __Packages followed by the full package and class name.

If you wish to read the original article instead of this one which I am archiving for my own reference as I keep scrambling for the link everytime I try to refer to, then head over to Peter Hall’s post on How to create classed stage objects with an empty library? Below is the reproduction of my friend, Peter Hall’s article;

Some time back, Peter Hall reported that every class is associated with a library symbol. This means that every class has a corresponding library symbol with a predictable linkage name derived from the full class name. So, we can simply include a component without adding a symbol to the library. Well this help prevent tightly coupling your class codes from your FLA.

Every class is compiled into a swf in the same way that we used to include AS1 classes, by putting them inside an #initclip block of a linked movieclip. The linkage name of the movieclip is "__Packages." followed by the full package and class name. For example the class com.peterjoel.data.XMLLoader would be exported with a movieclip with linkage "__Packages.com.peterjoel.data.XMLLoader"

If you are making a component, you can define it like this:

class com.peterjoel.MyComponent extends UIComponent {
	 static var symbolName:String = "__Packages.com.peterjoel.MyComponent";
	 static var symbolOwner:Object = MyComponent;
	 // associate the symbol to the class when the class is defined 
	 static var symbolLinked = Object.registerClass(symbolName, symbolOwner); 
}

This will allow you to use the V2 method createClassObject, without worrying about the symbol. But you could do that anyway if you were using createClassObject because that assumes that you have included the V2 framework, so you would have the export symbol for UIObject available to “borrow” anyway.

But, using this linkage, you can just use attachMovie, without relying on the framework at all. For example, define a simple class that draws a coloured rectangle:

class com.peterjoel.shapes.Rectangle extends MovieClip{
	 static var symbolName:String = "__Packages.com.peterjoel.shapes.Rectangle";
	 static var symbolOwner:Function = Rectangle;
	 public var width:Number = 100; 
	 public var height:Number = 100; 
	 public var color:Number = 0;
	 function Rectangle (){
			draw();
	 }
	 function draw():Void{
			beginFill(color);
			moveTo(0,0);
			lineTo(width, 0); 
			lineTo(width, height); 
			lineTo(0, height); 
			lineTo(0, 0); 
			endFill();
	 }
	 // associate the symbol to the class when the class is defined 
	 static var symbolLinked = Object.registerClass(symbolName, symbolOwner); 
} 

Usage

import com.peterjoel.shapes.Rectangle;
var initObj:Object = {width:150, height:80, color:0xFF0000};
attachMovie(Rectangle.symbolName, "rect_mc", 1, initObj);

And for those who want a quick view of the simple downloadable sample file.
[Disclaimer: I don't see it documented, so it is your choice.]

Update

In ActionScript 3.0, you can instantiate movieclip/sprites directly with new ClassName (). So __Packages is no longer needed.


Don't like it? There are lots of published articles, pick a random one.

oCricket

Brajeshwar posted this article on Wed, Aug 18th, 2004 at 1:03 am
Categorized under Technology and has the following tags

Prev Article: Advanced Flash Developers needed

Next Article: Kill Bill Vol. 2


Possibly Related Articles

Archives: Visit the Archives for more articles.

Comments Post Yours

There are 2 responses so far. You can follow any responses to this entry through the RSS feed. You can skip to the end and leave a response. Pinging is currently not allowed.

  1. How strange I was just reading that old post at Peter’s blog. I’ve literally just finished extending the Rectangle example he uses for a Flash blog app I’m releasing soon. But I must admit I did not feel it safe to use the “__Packages” method at the time, especially now. In fact it feels easier and more OO to use the composition method. What do you think?

    If you’re interested (or anyone else) take a look at the class and usage here:
    http://richardleggett.dotgeek.org/beta/Rectangle/Rectangle.zip

  2. Hmmm, yeah it feels a bit like trickery somehow. Not that there’s something wrong with that offcourse ;).

Post yours

Sidenotes

Quick notes, scribbles, somehow related to this website and to what I do. Or perhaps I'm just plain lazy to make them into a full article.

India gets ready for Free and Open Source Software

Over the years, FOSS.in has attracted thousands of participants, and the speaker roster reads like a "Who's Who" of FOSS contributors from across the world. FOSS.in is a non-commercial event organised and run entirely by FOSS ...21st Nov, 2008

Cooliris for the iPhone

Cooliris is the browser extension that revolutionizes the way you view media on your computer. It is now available as an application for your iPhone! Cooliris for the iPhone allows you to search the web ...22nd Oct, 2008

Adobe releases Flash Player 10 (Mac, Win & Linux)

Adobe have released the shipping version of Flash Player 10 for Mac, Windows and Linux. More about Flash Player 10 * About Flash Player 10 * Get Flash Player 10 (Official Release version 10.0.12.36) * Download Debug and Standalone ...15th Oct, 2008

What a bad team can do to a Startup?

Flash/Flex developer, Orian Marx, started his Startup with much zeal and enthusiasm but had to give-in to failures because of none other but the team - the founders involved. His article outlined the story so ...13th Oct, 2008

Introduction to Adobe AIR (NetTuts)

Nettuts have a nice Introduction to Adobe AIR which focus on using HTML/JS to build an AIR Application. It covers -- Adobe AIR Architecture, How to install AIR, Get the development SDK, Configurations, gets on to ...2nd Oct, 2008

View the Sidenotes Archive

Play the Penguin Game

Recommended

  • o! Just Me Of colorful cultures, entertainment, media, life hacks, music, books and movies from hollywood & bollywood.
  • Forum Oinam’s technical discussion forum where developers and designers can discuss all technical topics.
  • AS 2.0 Reference Reference for ActionScript 2.0 Programming Language used in Flash. Primarily stashed here for my own personal reference.
  • oCricket oCricket is about Cricket and people enthusiastic about it.
  • Not Safe for Work Ever clicked a link and felt embarrassed with the content in front of your co-workers? Ever caught unaware because the funny link your friend sent was a little beyond funny? Let’s minimize that with NSWF.
  • Ode to Apple Dedicated to Apple - Mac, iPhone, iPod, iTunes, Quicktime, Apple TV and all the awesome softwares for the Apple Mac.
  • My Special Job My Special Job is a place where you can look for your weird necessities, strangely superb employees, when your need are more of those hackers, geeks, and ultimate rockstars in the Internet Technology.

Download free Brajeshwar Wordpress Theme

Brajeshwar

Brajeshwar I firmly believe in keeping things simple, easy for users and I envison pushing the technical envelop time and again for the betterment of viable commercial and practical applications. More about me.

Brajeshwar's affinity with Adobe

My Photos

More photos on Flickr

Member of 9rules Network

Since its inception on 11th June, 2001, "Brajeshwar" has 891 Articles and 6,069 comments, contained within 21 categories and 1,328 tags.