Welcome to part 2 of the BAM series!
In this tutorial I would like to cover the transition from timeline coding
(using the Flash IDE) to pure AS3 programming using the Flex SDK with a suitable IDE
(such as FlashDevelop).
Before you continue with this tutorial, it is reccomended that you are familiar with common programming techniques and perhaps know a little about Object Oriented Programming. The
Flash CS3 Documentation on Programming Actionscript 3.0 will cover everything you need to know before continuing with these tutorials.
Creating Flash applications using pure AS3 code is hardly different to what you are used to
(if you aren't even used to timeline coding in the Flash IDE then I would strongly consider you check out the Flash CS3 Documentation on Programming Actionscript 3.0) infact it's just a case of switching, in a sense,
"wrappers". You don't need to learn an entirely different language, you just need to change the context in which you program.
For example, your
"wrapper" in the Flash IDE would be a
frame. All your different code, one way or another, ends up inside a
frame. Now in pure AS3 programming, your
"wrapper" is simply a
class inside it's own text file
(simply .txt files, except you change the .txt to .as). I apologise if I am over-complicating things for you, let's just get onto the coding.
Following on from the last tutorial,
[AS3]Building an MMOCC - Part 1, we made a start on our
project. Right now you should only have 1 file in your
project and that should be the
Main.as file Ignore the extra folders, for now we will focus on
Main.as in the
src folder.
So quickly open up your
project then open the
Main.as file. You should now see this:
So let's go over all this new code we are being introduced to...
Quote:package
{
}
The
package keyword simply defines what folder this
class is in. The
src folder that
Main.as currently resides in is the highest you're source code can go. However, using packages, you can organise your files into folders to make everything easier to manage. If, for example, you wanted to create a
MyClass.as file and put it inside a folder called
classes. You would write out the package declaration like so:
Quote:package classes
{
}
Pretty simple, right?
Quote:import flash.display.Sprite;
import flash.events.Event;
You may have come across this before, all this does is import other
classes (like the one we are making now
) although these havn't been made by us. These
classes have been made by Adobe which let us do whatever we need to do. Like if you have used the
Sprite class before, the code above simply allows the compiler to know what
classes it needs to compile our code correctly. For more information on all the different types of
classes you can use, check out the
ActionScript 3.0 Language and Components Reference.
Quote:public class Main extends Sprite
{
}
Now this is where we define our
class name, how it can be accessed and also what
classes it extends. If you are unfamiliar with what I am talking about, you still have some stuff left to learn, check out the links at the top of this thread.
Quote:public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
Now this function is basically what we call the
constructor. If you have no clue as to what that is, check out the links above.

So what this function is basically doing is checking if the
stage variable is set. If it is, it calls
init();, if not, then that means the
stage hasn't loaded yet. So we add an
event listener to listen out for the
Event.ADDED_TO_STAGE event, which is run as soon as we are able to interact with the
stage.
Quote:private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
}
Finally, this is the
init(); function that is called once everything is ready. All it is doing in this function is removing the
event listener as we are finished with it and the line containing
// entry point is where we can start writing the code for our application.
So that ends this tutorial! I really just wanted to give you a feel for what it's like to program using pure AS3. If there was anything you didn't understand, please check out the links I have posted above. Games aren't the easiest thing to program, it's all about the theory. So if you can't even use the language properly then it's just going to be too difficult for you and chances are you will just quit. Which I am sure neither of us want.
I have decided that for this series, I will be using the SmartFoxServer application as the game's server. This will mean their is less time spent on connecting to the server and security etc and more time simply making the game. If you would like to get a heads up on SmartFoxServer before-hand, check out
http://www.smartfoxserver.com/. In the next tutorial we will begin on creating the actual client. So I hope you look forward to that!
To get an idea of what we will be making, you can check out my
"sandbox" client. This is simply a client I use whenever I want to try out new features. It is a bit dated and won't be exactly what we are working on
(I want to show you how to make something better
) but it will give you a feel of what you can expect to accomplish. So to have a look, pay a visit to
http://www.calumscott.com/castiel/engine/.
As usual, please leave feedback on the tutorial, let me know if there is anything else that should be included etc etc. Also a little rep wouldn't hurt either
EDIT: Part 3 is complete! Check it out at
http://mmoccforum.com/showthread.php?tid=17964
The Ultimate Battle... who will win?
(This post was last modified: 07-30-2010 05:49 PM by xLite.)