Grant Skinner

The "g" in gskinner. Also the "skinner".

@gskinner

Class locations in AS2.0 run-time

I hadn’t really thought about where AS2.0 classes reside at run-time until I encountered a need to dynamically create instances of the classes I had made for one of my current projects. In AS1, it was really simple to dynamically create instances from a class name, because you specified where the class definition resided, for example:

// on _root
function MyClass() { };
// anywhere else:
className = "MyClass";
myInstance = new _root[className]();

However, in AS2.0, you never specify where the class definition sits – it’s really pretty obvious though, if you think about it for a moment. AS2.0 classes can be accessed by name from anywhere in the swf – they are accessible globally. This , of course, is because they reside inside of _global. For instance, if you defined a custom class called “MyClass” in AS2, you could access it at _global.MyClass, and could create instances of it dynamically with:

className = "MyClass";
myInstance = new _global[className]();

Simple, eh? So what about classes inside of packages? Well, the dot notation indicates an object hierarchy, and it turns out that’s all there is to it. The class definition for a class called “MyClass” inside the “com.gskinner.utilities” package can be found at _global.com.gskinner.utilities.MyClass. If you knew the package was always the same, you could create instances dynamically with:

myInstance = new _global.com.gskinner.utilities[className]();

If you want to create instances from full dynamic class names with varying packages, you can use this function I whipped up:

function getInstanceOf(p_fullName:String):Object {
var classPath:Array = p_fullName.split(".");
var package:Object = _global;
var l = classPath.length-1;
for (var i:Number=0;i<l;i++) {
package = package[classPath[i]];
}
var c:Function = package[classPath[classPath.length-1]];
var instance:Object = new c();
return instance;
}
// test it:
myInstance = getInstanceOf("com.gskinner.utilities.MyClass");

Unfortunately, I haven’t got it working with .apply yet, so you can’t pass arguments to the constructor (if anyone gets this to work, send it to me, and I’ll post it).

All of this raises an important consideration: Be careful of name conflicts with packages when adding items to the _global object (classes should be fairly safe due to their capitalization)! If you have a class “com.gskinner.utilites.MyClass”, and you then you define a _global.com variable, you will overwrite the entire “com” class package (Eek!). For example:

import com.gskinner.utilities.MyClass;
_global.com = 127;
myInstance = new com.gskinner.utilities.MyClass();
// myInstance is undefined!

Upcoming presos: FitC, FFSF, NAIT, UofA

I received confirmation today that I will be speaking at Flash In The Can 2004 (Toronto) in early April. I will be speaking on coding production-friendly ActionScript 2.0. Rather than rehash syntax, I will focus on how to actually apply AS2.0 to real projects in the most effective manner possible. I really feel it will be a phenomenal session, given the ramp-up I will have in the preceding speaking engagements, and the practical knowledge I am gaining from a number of projects that I have been working on.

In January and February, I will be instructing “FlashMX 2004 Enterprise-level Development” at the Northern Alberta Institute of Technology. This is a 36 hour course, spread over 4 weeks, covering advanced Flash development.

At the end of January, I will doing a guest lecture to fourth year Arts students at the University of Alberta (topic to be finalized), which I’m pretty psyched about.

In early March, I will be doing an Ask The Experts session at FlashForward San Francisco, dealing with basically the same topic as my FitC session. I will begin with a short session discussing my development techniques, best practices, and look at some real world examples; then provide attendees with an opportunity to ask questions about implementing AS2.0 in their projects. This should help provide a great foundation of really relevant material for my FitC session.

Hope to see some of you at these events. Be sure to say “hi” if you see me!

The Internet is sh!t

Via gijs nijholt

I don’t usually link to people’s opinion pieces, but I thought this post on why the Internet is shit was a good read, and the last paragraph reflected a feeling that plagues me.

When I first got into web design (a whopping 9 years ago), everything was new, everyone was excited, and I was kept in a constant state of awe as the modern “rich” internet emerged from the primordial soup of HTML 1.0. Maybe I’m just getting old and jaded, but lately that awe has receeded – I rarely see web work anymore that really excites me. There have been a few pieces that really caught my attention as good examples of what can be done (really, what should be done), but nothing that makes me really feel that the pea-brained dinosaurs of the present-day web will be replaced with smarter, more adaptable successors.

I’m left in a constant state of anticipation – I feel like something has to happen, some new concept has to begin defining what the web is going to be 5 years from now. This serves to define much of my professional life, I’m fortunate in that I can usually pick and choose the clients I work with, and I typically do so on the basis of how progressive or interesting the project (or the client) is. It also drives me to tackle monolithic projects with little hope of recompense, just to prove to myself and the community that things can (and should) move forwards (see: FlashOS, FlashOS2, gModeler, etc).

I think the most exciting thing I’ve seen recently is the involvement of talented writers, with a firm understanding of the medium, in the development of websites. A good writer goes beyond copy-writing to help define the narrative of a site, establish a repoire with the user, effectively (and engagingly) communicate the core messages of the site, and work with the designer to develop the user experience.

Now don’t get me wrong – I love my job, and I think the Flash community is phenomenal, I’m just left wondering if there isn’t more that we could do to evolve this medium to the benefit of our clients before the meteor (longhorn?) hits.

PS. If you’ve built a Flash intro in the past 3 months, without discussing with your client why its a waste of their money and what their alternatives are, kindly raise your hand and exit the blog – you’re holding us all back, mate.

[EDIT:] Wow, I really must be getting old and cranky… too many complaining posts in the last few days… must… post… something… positive. 🙂

setStyle == Huge CPU drain

Sam Neff recently explored how setStyle notifies components of style changes, and the results are definitely worth noting (this may save you some major headaches, so listen up). It seems that every time you call setStyle, it calls notifyStyleChangeInChildren(), which iterates through EVERY movieclip in your movie, checking to see if it should notify it of the style change… ouch! Double-ouch even.

This was very useful information for me, as I’m presently working on a (top secret) project that has 100+ components instantiated at a time, and I was a little confounded as to why initializing the first interface was taking about 12 seconds. It all became very clear when I realized that I was setting about 40 different styles, which resulted in literally TENS of THOUSANDS of movieclip “style checks”. Moving the setStyle calls to the first frame solved the problem completely.

The moral of this story: always set all of your global style information before instantiating ANY movieclips, and then don’t change it – at least until Macromedia changes their EULA, and someone can legally distribute a fix for it (jab, jab). 😉

‘Tis the season: Snow FLA

Well, the -20c temperatures (that’s -4f for those of you stuck with archaic systems of measurement), and 20cm+ of snow (8″) we got over the past week tells me it’s about time to dust off the old snowflakes code, and make it available to the community. There’s a lot of code available for simulating snow, but I think my extensive real-life experience (brrr!) gives mine an edge, plus it comes with a cute peacenik snowman.

This is the same FlashMX version that’s been available on my site for a year and a bit now, with a few minor modifications. I plan to post an MX 2004 class based version, just for the helluvit, in the near future.

You can take a look at the Flash snowfall swf, then download the free FLA. Peace on Earth, and goodwill to men. 🙂

Also, here’s a nice (though completely uncredited – Oscar, you bastard! 🙂 usage of my code with some nice snowflake artwork tossed in.

FlashMX 2004 Enterprise-level Development

My apologies for the lack of posts lately – things have been comfortably busy, with client projects, and a few other endeavours, one of which is a new advanced Flash course I will be instructing at the Northern Alberta Institute of Technology (NAIT) in January and February ’04.

If you’re an intermediate Flash developer in Edmonton or area, check it out – it might be “the course to take your game to another level”. It also helps that NAIT is running it for an amazingly good price.

Read on for the full press release from NAIT…

Continue reading →