- Most Recent | Since 2004
- List View
- Archive
- August 11, 2005
More Spareribs
August 11, 2005Quickie post…a picture of some ribs I made last week to show cousin Jason how it’s done. These were made under the broiler, as the deck is being worked on. It’s Rib Recipe 5.0 Alpha 1. except using store-bought sauce (Bullseye™ Original Recipe) as the coating.Read moreToo bad the background is so unappealingly gray.Thanks Jeff for knocking out the gray background :-) - August 10, 2005
Object-Oriented MovieClip Callbacks
August 10, 2005Read moreIt’s Day 2 of Jason’s Actionscript 2.0 Bootcamp! The first step was to port his AS1.0 code into AS2.0 classes. We’ve gone as far as just creating all our components on the screen, but have not yet hooked up any events. Why? Because they’re tricky in AS2.0 (at least when you are rolling your own). For example, we had to go over how MovieClip callbacks in an object-oriented context doesn’t work. Witness this class definition:
class Foo {
var mc :MovieClip; var myProperty :String;
function Foo ( parentClip:MovieClip ) { mc = parentClip.attachMovie (“FooImage”,”Foo1″,1); mc.onPress = HandleOnPress; }
function HandleOnPress() { trace(“myProperty == ” + myProperty); }
}
When you run this, the output will say “myProperty == undefined”. The reasoning is a bit esoteric: The
MovieClip
class uses old callback conventions that don’t maintain object context in ActionScript 2.0…you’re just setting a pointer to the address of a stream of code, not a method being invoked on an object. With the object context, you can’t access any variables in the object you’re defining your code in, though it certainly almost looks like it should be fine.Why they didn’t add MovieClip listeners in MX 2004 baffles me. Maybe they didn’t want to add duplicate listeners in an already confusing API? But then why did they go ahead and do that in the
TextField
class? You can also use the Mouse and Keyboard object listeners instead to do the same thing I’m describing. But I digress.Anyway, I showed him the workaround. Here’s the modified constructor:
function Foo ( parentClip:MovieClip ) { mc = parentClip.attachMovie(“FooImage”,”Foo1″,1); mc.parentObject = this; mc.onPress = function() { this.parentObject.HandleOnPress() } }
The key to this is the
this
keyword. It means two different things here. In the first line, it refers to the instance of Foo that’s being created. In the anonymous function, this evaluates to the MovieClip that is triggering the callback function. If you didn’t know that, you’d be screwed! So, we’re using this behavior to re-establish object context in the callback without a lot of tedious manual lookup using associative arrays (which I’ve done in the past). MovieClip is a skanky object…it’s not too discerning about what you stick into it during runtime, if you catch my drift. The parlance in AS 2.0 is that it’s a “dynamic object”, requiring no declaration of properties before you can use them. You know, just like good ole horrible Actionscript 1.0, with its “call me and I’ll be there but useless and non-error generating so you’ll never know you misspelled ‘proerty’ in 1000 lines of code but hey it makes the language seem more friendly” insanity. Gah!After Jason finished porting his code, we’ll talk about the need for explicit destructors, as using a composition class like this for MovieClip sort of defeats some of the magical garbage collection features of Actionscript 2.0.
UPDATE MAY 12, 2006:
Since writing this article, I found a neat trick from Colin Moock’s Essential Actionscript 2.0 book. It’s just mentioned in passing, but it’s very critical. Check out pages 300 and 308 about this use of an intermediate variable
thisInstance
to hold the value of this.class Foo {
var mc :MovieClip; var myProperty :String;
function Foo ( parentClip:MovieClip ) { mc = parentClip.attachMovie (“FooImage”,”Foo1″,1); // dereference ‘this’ var thisInstance:Foo = this; mc.onPress = function() { thisInstance.HandleOnPress() } }
function HandleOnPress() { trace(“myProperty == ” + myProperty); }
}
The use of the local variable thisInstance saves a copy of this so it refers specifically to the instance being created. So, when the anonymous function assigned to mc’s onPress callback executes, it actually refers to the instance instead of the this keyword. Remember, the this keyword, in the context of an anonymous function used as a callback handler, refers to the MovieClip. This is a very handy trick.
If you’re using event listeners with components, you’ll be looking instead into using Macromedia’s
mx.utils.Delegate
class to create the handler for theaddEventListener()
call. Works great in conjunction with Grant Skinner’s GDispatcher class. - August 10, 2005
A Hair Odyssey
August 10, 2005There was a brief period of time, from around 2000 to 2002, where I actually had my hair under control. I was a contract interactive designer working the Boston and Southern New Hampshire Greater New Media Zone. I kept my hair close-cropped and clean-edged, cutting a mean (if not exactly lean) silhouette in basic bad-ass black.Read moreI finally got my hair cut professionally. Story follows!
- August 9, 2005
Actionscript 2.0 Teaching Day 1
August 9, 2005Read moreI’m running Jason through an introduction to the glory of Actionscript 2.0 Programming. About 10 minutes into it, I started to feel very bad for him when I realized that the event handling was going to seem really really weird when we get to that. Then, as I was working up the basic application architecture, I had flashbacks to the frustration of getting “Hello World” running in today’s environments…so much typing for so little gain! Or so it would seem at first…
These are today’s notes on the teaching experience, so I don’t forget.
- August 9, 2005
Fog Creek Copilot
August 9, 2005Fog Creek Software, the company that Joel built, has a new product that promises easy remote assistance between computers. You know, like Microsoft’s Remote Assistance, except without the requirement for Windows Messenger when behind a firewall. In other words, it’s supposed to be easy for anyone to use. Huzzah!Read moreEven cooler, the product was built by Fog Creek’s team of four hand-picked interns. The entire intern program sounds really cool, involving documentary filmmakers and development blogging. The news release is pretty damn inspiring. The entire system built in just six weeks, start to finish.