Linear Sprites

Starting from scratch at understanding just how Object-Oriented Programming, ActionScript 2.0 models inside of Flash. I took a stab at what [Grant Skinner]( spoke about at FlashForward 2005 in San Francisco (last week). Here’s the finished product…

[Download the source files](

Grant’s a pretty smart guy. Sure the example he wrote above isn’t terribly sexy, but Grant revealed a key element in how to conceive of OOP in a way that bridges most developers’ understanding of Flash and very evident objects — associating movieclip behaviors with a class that describes the behavior.

I’m going to journal these experiments, working my way up to rebuilding my (somewhat famous) Asteroids code with ActionScript 2.0.

But for now, let’s get back to the basics…

Directly associating Classes to movieclips isn’t the only approach you can use in grasping OOP with ActionScript 2.0, but it’s a great start for understanding how theoretical object structures can relate to the virtual objects that you can manipulate in Flash’s IDE.

All I did in this example was to create a movieclip, just as Grant described in his presentation, and link it with a class file of the same name as the linkage for that movieclip out of the library.

The code for the class is here, and as you might guess, it’s saved as an ActionScript file (“”):

class LinearSprite extends MovieClip
	private var xvel:Number;
	private var yvel:Number;

	private function LinearSprite()
		xvel = 2 + random( 80 ) / 10;
		yvel = 2 + random( 80 ) / 10;

	private function onEnterFrame():Void
		_x += xvel;
		_y += yvel;

		if ( ( _x > 400 && xvel > 0 ) || ( _x < 0 && xvel < 0 ) ) { xvel *= -1; }
		if ( ( _y > 150 && yvel > 0 ) || ( _y < 0 && yvel < 0 ) ) { yvel *= -1; }

So basically, in my Flash example above, I dragged four of these sprites onto the stage, and they’re just doing what they do. It certainly runs pretty smooth with each sprite responsible for its own movement, rather than the timeline chugging through the onEnterFrame.