ActionScript 1.0: Building Dynamic Interactivity in Flash 6
It’s 2002, and Macromedia Flash MX has changed the game. While previous versions were mostly for animation, Flash 6 brings ActionScript 1.0, a language based on the ECMAScript standard (just like JavaScript). We are no longer limited to gotoAndPlay. We can now build real applications and games using prototypes.
The Prototype Pattern
In ActionScript 1.0, we don't have "Classes" in the Java sense. Instead, we use the prototype property to add methods to constructor functions. This allows all instances of an object to share the same code, saving precious memory in the Flash Player.
// Constructor function for a SpaceShip
function SpaceShip(name, speed) {
this.name = name;
this.speed = speed;
this.x = 0;
}
// Adding a method via prototype
SpaceShip.prototype.move = function() {
this.x += this.speed;
trace(this.name + " is now at " + this.x);
};
// Creating instances
var ship1 = new SpaceShip("Falcon", 10);
var ship2 = new SpaceShip("Eagle", 5);
ship1.move(); // Output: Falcon is now at 10
MovieClip Events
One of the most powerful features in MX is the ability to assign code to MovieClip instances dynamically. In the old days, you had to place code on the clip in the IDE. Now, you can do it all from the main timeline.
// Controlling a MovieClip named 'hero_mc' on the stage
hero_mc.onEnterFrame = function() {
if (Key.isDown(Key.RIGHT)) {
this._x += 5;
}
if (Key.isDown(Key.LEFT)) {
this._x -= 5;
}
};
hero_mc.onRelease = function() {
trace("You clicked the hero!");
};
Loading Dynamic Data
With LoadVars, we can finally pull data from a server (like a PHP script) without refreshing the page. This is the foundation of "Rich Internet Applications."
var myData = new LoadVars();
myData.onLoad = function(success) {
if (success) {
status_txt.text = "Welcome, " + this.username;
} else {
status_txt.text = "Error loading data.";
}
};
myData.load("get_user.php");
ActionScript 1.0 is turning Flash from a "cartoon tool" into a "software platform." If you haven't mastered this and prototype yet, you're going to get left behind in the new era of the interactive web.
Aunimeda develops mobile and PC games - from casual hyper-casual titles to mid-core games with complex progression systems.
Contact us to discuss your game project. See also: Game Development, Mobile Game Development