Aug 20 2008

ActionScript 3: Events

Thomas Viktil

A couple of days ago I encountered a problem with Events. I clicked and object, but another returned the event. Despite adding mouseEnabled = false to the objects I didn’t care about, I still couldn’t figure it out. Events is nothing new to me, but this was beginning to get on my nerves.

After a bit of reading I stumbled across an article on Adobe’s Developer Connectionколи под наем, written by Trevor McCauley (Senocular), about events in ActionScript 3 (AS3). It’s basic stuff, and a bit old, but gives some background info that really helped me better understand the anatomy of the event dispatchers and how the entire event thingamybob works under the hood.

It might even be of help to you too. Read it if you like: Introduction to event handling in ActionScript 3.0


Aug 11 2008

Flash CS3: Adding custom classes to MovieClips in the library

Thomas Viktil

Today I was tearing out my hair in frustration of a problem I just couldn’t seem to figure out. And the solution was just about as stupid as I had hoped it wouldn’t be.

The case was as followed: I’m putting MovieClips in a SWF for use as a library in another SWF (I’ll call it master SWF). The library SWF has a custom document root with a function that helps me return the MovieClips I need. Some of the MovieClips in the library are extended with custom classes.

When I tried to return a MovieClip from the library SWF I would get an error message saying the MovieClip doesn’t exist. Even if the class has been set up correctly, the package names are fine, and everything looks right.

The library SWF resides in the root, together with the master SWF. This way I can keep the same document root in both SWF’s and share classes between. The custom class for the MovieClip I tried to return is in package bb.page, and the name of the class is Frontpage.

In Flash I set the linkage to bb.page.Frontpage, compiled, and expected a positive result. But no. I tried moving everything down to root level, thus flattening the whole directory structure. Removed the package name from the Frontpage class and gave it a try. It actually worked. But it’s not an optimal solution because I don’t want to mess up the root directory with tons of files.

Moving everything back in to the directories they came from, I gave a stupid idea a try; renaming the MovieClip in the library. And guess what? Renaming the MovieClip from ‘Frontpage’ to ‘bb.page.Frontpage’ did the trick. I haven’t bothered trying to figure out why it’s like this, but it seems like it just is.

So, conclusion to this;

Make sure the name of the MovieClip is the same as the linkage class.

Example: class Frontpage resides in bb.page. Linkage class is therefore ‘bb.page.Frontpage’. This will also have to be the name of the MovieClip.

UPDATE August 12 2008

While at work today I spent a few hours figuring out how to deal with this problem and came to a new conclusion; what I’ve written in this article is wrong. I haven’t managed to come to a perfect solution, but good enough for the project I’m working on. I’ll be back later to write more about this.

In the mean time, and if you are looking for a solution to a similar problem, try searching for keywords like as3 asset managemt, resource management, external libraries and such. I found some articles that gave me a better understanding of what I’m dealing with.


Aug 5 2008

Actionscript: Accessing MovieClips from an externally loaded SWF

Thomas Viktil

For the time I’m working on a website and so far I’ve only spent some time doing research around all the ideas I have. One of them was to put all MovieClips in one externally loaded SWF. Loading the SWF is easy, but how do you access the individual MovieClips? Easy as 1-2-3!

For loading the assets I use BulkLoader; so far it seems like a pretty good alternative to writing a bunch of code myself. I like.

In the assets.swf I have only stuffed the library with MovieClips, and the ones I’d like to access from the outside are set to linked. Nothing is placed on stage. The document root to the SWF is set to a custom class. It is within this class the trick lays. And the trick is to set up a public function that returns any MovieClip to the parent SWF.

The parent SWF loads assets.swf and puts it in a variable. The public function in the assets document class is now accessible by dot syntax. By calling this function we can easily add MovieClips from the assets library to any MovieClip in the parent SWF.

Here’s the document class for assets.swf:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package
{
	import flash.display.MovieClip;
	import flash.utils.getDefinitionByName;
 
	public class Assets extends MovieClip
	{
		public function Assets()
		{
		}
 
		public function getAsset(s:String):*
		{
			var a:Class = getDefinitionByName(s) as Class;
			return new a();
		}
	}
}

What this does is first of all import getDefinitionByName from the flash.utils package. This is a function that will return any object based on the string you give it. Second, and last, is setting up a generic public function that will look for and return an object with the name you provide it with.

In the parent SWF you would set up an eventHandler that puts the loaded SWF in a variable, and then call getAsset(). Like this (note that I’m using BulkLoader):

1
2
3
4
5
6
private function mclibLoaded(e:Event):void
{
	var lib:MovieClip = loader.getContent("mclib");
	var p1:MovieClip = lib.getAsset("Page2");
	keeper.addChild(p1);
}

loader is the name of the BulkLoader object, and keeper is a MovieClip on stage.

Rather easy, right?

If you’re interested in knowing more about BulkLoader, take a look at the documentation.
The generic function I’ve written here was taken from Big Spaceship Labs. They did it in a more elegant way than I initially did. Don’t thank me, thank them.