Preface
Basics of variables/functions and other mundane actionscript are recommended before reading.
Many of us know and understand how to create and utilize "Classes" in our flash, because we know what they are, how they work and we understand both the terminology and the amazing uses for them. Many people that are new to languages are scared shit face of these things though. This tutorial, part of the "for Morons" series aims to cut out all of the large words, strange syntax and scary symbols that make most people completely turn their back on OOP and continue coding "the old way." This is not an all in one tutorial for how classes and/or OOP work. It is stripped, bare bones about the uses of Classes in plain English for people that cry and cut themselves when they see anything that resembles an object. If you're afraid of Classes, and haven't used them in any of your programs, because you're a whiney little girly man, read on.
Once you have a strong grasp on "why" you'll better understand the "how" that both Authorblues and Inglor described in their tutorials, here...
OOP classes by AuthorBlues
OOP Overview by Inglor
Words that will NOT be used in this tutorial because of the nature of it (but should be learned immediately afterwards )
Encapsulation, Inheritance, derive, implement, delimit, instantiate, property, method,
Moron'd definition of Classes: Think of classes as stencils. You cut out a stencil one freaking time and you can use it over and over again.
Moron'd definition of Objects: Think of objects as what is created after you spray paint over the stencil.
You can have one stencil, and make the same design in 20 different colors. All of them are the basically the same, but different in their own way. Classes sounding pretty freaking sweet so far? Hoping to save your poor typing digits hours of agony? Good. Let's continue. Let's create a class... on ideas alone first.
First, we need something cool. How about Ninjas? Ninjas are freaking metal. So, we're going to start building our Ninja stencil. This will be our class.
Now, in order to create a class, we need to know what things that ninjas have in common. We need to separate these in cool shit they HAVE and cool shit they can DO.
So, things all good Ninjas HAVE...
A name
A certain color uniform
A weapon
A master
A certain type of martial art
A finishing move
Now, ALL good ninjas can DO the following things.
Be stealthy
Kick major ass
Assassinate Mo fos.
Alright, we have our list of what Ninjas have and can do. Guess what.
The stuff they HAVE… those are basically variables.
The stuff they can DO… those are basically functions.
Now, if you wanted to create 10 different ninjas, without classes, you would have like 60 variables, and maybe 30 functions. Screw that. We have ONE class, and TEN objects.
Now, I know we said we weren't going to get into the "how" of classes, but I lied. Don't cry, pussy. This will help you get a visual of the basic framework of what that class would look like in a very simple form. (keyword "var" and variable data identifiers, constructor and access properties left our purposefully)
class Ninja{
//variables (properties of a class)
name;
colorUniform;
weapon;
master;
martialArt;
finishingMove;
//functions (methods of a class)
beStealthy();
kickMajorAss();
assassinateMoFos();
}
Now, the question is this... "Dude, why the hell don't the variables equal anything?" It because we just built our template. Our stencil. Those don't have to be filled in. We haven't taken our paint to the stencil yet. Now, we can create all of our ninjas based on that one class we just created! Not that exact one. You still need to learn a few things, listed in the other tutes.
Alright, let's think of some other uses for classes, so you can first hand some of the uses of coding.
A car
Color;
Speed;
numberOfDoors;
Move();
Turn();
Stop();
Explode()
Hookers
breastSize;
numberOfDiseases;
homeCorner;
hookerName;
turnTricks();
bitchSlap();
explode();
Your main hero (because then all of his stuff is stored in ONE place)
strength;
defense;
magicPoints;
penisSize; (important for main characters)
Magic();
UseItem();
Attack();
Defend();
GetTheChicks();
Explode() (after getting the chicks)
Platforms in a game!
rotating = false
elevating = false
stationary = true
stopCharacterFromFalling();
explode();
some guns.
ammo;
range;
accuracy;
damage;
shoot()
reload()
explode()
Midgets for you midget punting game.
Size;
Weight;
Aerodynamics;
ScreamLikeAbitch();
Explode();
Now, every time you create an object, you use the class you created and just fill in the blanks! If you play table top role playing games (Such as D&D) think of classes as your character sheets. One sheet can be filled in thousands of ways to create thousands of different characters.
If you don't play, think of classes as the new ground's forums. The basic functions of general, flash, and where is/how to are the same. They all have places for you to click "reply" and flame me. They all have a list of posts, yet every forum has different posts in it, and concerns different topics. It's a stretch, but still a form of a class.
With all of these things listed, you should have no problem recognizing just a few of the VERY fundamental uses for classes and how they can not only help you organize your code, but also speed up your coding and reuse of the same code, over multiple games so you can crank out games that with some luck, and some skill won't get the big fucking blam. I now recommend reading Inglor's description of OOP which goes into the basic structure and then following that up with AuthorBlues Hella in-depth class tutorial, both listed at the top of this page.
I don't want any excuses from you newer folk for not submitting games with classes in them, and peppering the damned BBS with questions about OOP. No excuses!
(hopefully classes in actionscript work the same as C++ ones....)