literature

The complexity of Flash

Deviation Actions

Doom-the-wolf's avatar
Published:
5.7K Views

Literature Text

The complexity of Flash programming


Why most Flash tutorials fail.



This article deals with Actionscript 2.0, but will be useful to understanding any Flash version.

Comparing Flash to the Earth



Let's imagine for a moment that learning Flash is like learning how the earth works. You'll come across many "Earth Tutorials" that will tell you "The earth is flat, here's the code to calculate the distance between your house and the local mall":

Distance = SQUARE_ROOT((Mall.X-yourHouse.X)2+(Mall.Y-yourHouse.Y)2)


This is fine when you're working with small distances, but those tutorials never tell you how you can learn to calculate the walking distance between Paris and Madrid. Now a real Flash expert will tell you it's different, the flat earth model is only a subset of the round earth. In the round earth, you'd have to calculate the distance differently, maybe a bit more like this:

"Alpha" is the angle between the 2 cities compared to the center of the earth.
Distance = EARTH_RADIUS*Alpha


Suddenly you realize you've been looking at the earth on a small scale and forgot to look at the bigger picture. Calculating the angle between two cities also takes more work.

The Flash tutorials



Let's apply this to Flash. Many tutorials you find will give you a code, like this one:

//Write this code in your Movieclip item
onClipEvent(load){
   this._y = 0;
   this.ySpeed = 0;
}
onClipEvent(enterFrame){
   if(this.hitTest(_root.ground) == true){
      if(Key.isDown(32) == true){
         this.ySpeed = -10;
      } else {
         this._y = _root.ground._y;
         this.ySpeed = 0;
      }
   } else {
      if(this.ySpeed < 20){
         this.ySpeed = this.ySpeed + 2;
      } else {
         this.ySpeed = 20;
      }
   }
   this._y = this._y + this.ySpeed;</sub>
}


This code will make a MovieClip object appear to "Jump" when you press the space bar while it's touching the "ground", which is another MovieClip.

The Flash compiler is a program that converts your code into machine language, I'm going to tell you what it does with this code:

The Main Timeline



All MovieClips on the stage need a name. If your MovieClip doesn't have a name, Flash will automatically give it one, like "instance34". Every single possible code inside a MovieClip has an equivalent code for the main timeline. Here's what it would look like

instance34._y = 0;
instance34.ySpeed = 0;
instance34.onEnterFrame = function(){
   if(this.hitTest(_root.ground)){
      if(Key.isDown(32)){
         this.ySpeed = -10;
      } else {
         this._y = _root.ground._y;
         this.ySpeed = 0;
      }
   } else {
      if(this.ySpeed < 20){
         this.ySpeed += 2;
      } else {
         this.ySpeed = 20;
      }
   }
   this._y += this.ySpeed;

};


I always recommend writing all your code in the main timeline so you always know where it is. But this article isn't a tutorial, so I'm not going to teach you how to use Flash.

Key Events



A Key event occurs when you press or release a key. A lot of Flash beginners desperately try to avoid having to handle them because they don't understand how events work.

Unfortunately, the tutorial makers have found one way to avoid having to learn: Key.isDown(). In the code mentioned earlier, you'll notice the code Key.isDown(32). This code returns true if the space bar is pressed. You can change the number 32 for whatever Key code you want.

This function prevents the user from having to go through the process of detecting that a key is pressed, then marking it as pressed, then detecting that it was released and marking it as released. Inside the "Key" class there is a function that does this. Here's an idea of the work it's doing:

var listener:Object = new Object();
var keysDown:Array = new Array();
for(var i:Number = 0; i<128;i++){
   keysDown[i] = false;
}
listener.onKeyDown = function(){
   keysDown[Key.getCode()] = true;
}
listener.onKeyUp = function(){
   keysDown[Key.getCode()] = false;
}
Key.addListener(listener);
//And this is the function the people use all the time:
function isDown(n:Number){
   return keysDown[n];
}


Classes



Everybody sees it, but many of them don't seem to care. Why is there a "." between "instance34" and "onEnterFrame"? ActionScript is an Object Oriented Programming language, everything you see belongs to a class. A class is a way of defining a group of properties and a list of methods that apply to them. For example, the MovieClip object.

"instance34" is an object of the class MovieClip. It has both properties and methods. A property is like a variable that belongs to the object, it can hold information. In this case "_y" and "onEnterFrame" are both properties of "instance34".

Methods are functions that belong to the object, like "hitTest". A method acts upon the properties of the object either by reading them or writing them. In this case, "hitTest" reads the boundary properties of the MovieClip and compares it to the boundaries of the MovieClip in the parameter.

Conclusion



I don't expect you to understand all the explanations I gave you, that isn't the purpose of this text. This is advanced programming and takes months to learn. What I do want you to realize is that there is a whole lot of information hidden in the code mentioned in the very beginning of this article.

Many of the Flash tutorials found on deviantArt fail to reveal all of this information. They try to make the code as short and simple as possible, they try to steer the viewer's eyes away from the parts they don't understand. They are trying to explain how to navigate a flat earth while living on a round one.

If you truly want to learn Flash, stay away from the deviantart tutorials and go to Flash dedicated websites, such as ActionScript.org. Or learn interesting things from the learnFlash.com weekly video tutorials.
I decided to write this short article one why I think most Flash tutorials are doing a bad job at teaching.

First of all, it's because many of the tutorial makers don't even know what they are talking about. But the other problem is that they aren't really teaching because they are only showing you tiny pieces of a much larger puzzle.
© 2010 - 2024 Doom-the-wolf
Comments13
Join the community to add your comment. Already a deviant? Log In
Riox's avatar
Ah...it's like being back in my Programming class back in high school. I'm actually able to, for the most part, keep up and understand what you were saying. I'll honestly take my lack of remembering the stuff from that class as my own fault, and put the fair amount of blame on my teacher only teaching us a little bit then expecting us to just know the rest right off the bat that she deserves. Such as it goes.

Given the fact I've actually wanted to attempt a Flash project, I would probably want to upgrade my computer first before attempting it so I can actually run the programs and such before making the attempt, but this is a nice refresher to remind me of what I do happen to remember, and the two links you provided will no doubt allow me to re-learn what I forgot and learn what I was never taught in high school. Many thanks, Doom, for providing myself - and the rest of us - the opportunity to learn.