;

PDA

Click to See Complete Forum and Search --> : Objects and classes in AS 3.0


Frase69
07-04-2008, 09:25 AM
Hi,
I've only recently begun making the step from 2.0 to 3.0 and I was wondering if anyone could briefly explain a few things to me. I definately think 3.0 is worth the effort of learning as it is much more similar to Java which I am familiar with.

With the old language, when I made games I made everything on the stage, now I'm interested into looking into making movieclip objects in code and placing them onto the stage.

What I can't get my head around though is where all this code should go, it seems ridiculously to put absolutely everything into the frame of the main timeline. Thus I looked around, and realised I could build external .as scripts to hold my classes.

However, I'm finding this all very hard to picture. Even if I made .as files for all my objects, where would I put all the code for interactions between those objects, would this all go in the actual timeline frame? Or else, could I put these controller classes into .as files, and just import everything in with the timeline actions? It seems to require much, much more planning than the old days of just including all methods and attributes within a movieclip. What are the standards?

Basically could you please give me advice on the above as best you can, and one other little thing:

If I did make a flash project of all these external documents, does it combine absolutely everything into the .swf once published? (I'm assuming that must be the case in order to add the flash to a web page without all the project files).

Thankyou for any help or information you can give. Any tutorials or game examples would be much appreciated too.

benthejack
07-04-2008, 10:46 AM
look up how to use the document class in flash help, i think that will answer most of your questions.
basicly the document class' constructor is called when your game is loaded, so you can start your code running there.
you have to import any custom classes into any of your other custom classes which use them, ie if you have a dog class which uses your leg class, you have to specifically import it (into your dog class).

ok so ill give you an example of a document class



package
{
//any importing goes here
import flash.display.MovieClip;
import OtherClass;

public class myDocClass extends MovieClip
{

//your global (well global within this class) variables are
//defined here
//ie...

public var myVariable:SomeDataType;

public function myDocClass()
{
//once youve told flash that this is your document class,
//any code here will be run at start of program, sorta like
// frame 1's timeline i suppose
}

public function anotherFunction(params:SomeDataType)
{
// code run when you call function
}

}
}



important things are declaring things public or private, and always stateing "package" at the start of your file.
your OtherClass class can be written in the same way.

I hope this made sense (its 3am at the mo, so if it didnt thats why :P )
and is some help to get you started.

oh and always save your files with the same name as the class, ie this will be saved as MyDocClass.as

and yes when you publish your file it compiles it all into the swf file.

cheers

Frase69
07-04-2008, 11:07 AM
Thanks! that's very helpful, the document class thing was exactly what I was looking for.

Just one other thing though, say I was to draw a movieclip and export to AS with the linkage class name 'Player', how would I then associate a class (with the variables and methods of the player etc) with this movieclip?

Would I simply create an external .as file with the same class name 'Player'?

In other words:
In 2.0, I would simply apply actions to the movieclip in order to give it variables and methods, how do I do the same (link it to my class) in 3.0?

I can create an object of the movieclip, and draw it on the stage, but it doesn't have any actions associated with it.

benthejack
07-04-2008, 11:13 AM
Would I simply create an external .as file with the same class name 'Player'?


yes, same class name and filename ie Player.as
make sure your class extends MovieClip though, or you will get errors.

when you want to display it on stage you call
this.stage.addChild(instanceOfPlayerClass);
from your document class.

Frase69
07-04-2008, 11:16 AM
Excellent, thankyou very much for your time and help :)

benthejack
07-04-2008, 11:19 AM
happy to have been a help :)
i remember having the same issues :P
once youve got it its a ton nicer than as2 IMO, just takes a bit of getting used to.