View Full Version : Classes - Having Trouble understanding them
Hi, i'm new to OOP. I was learning AS2 but then saw AS3 had been released so decided to learn that instead.
There is one problem, im used to programming by attaching code to MC's and this idea of classes has made me stumble. I understand that i can now only write code on the time line but what exactly is a class?
I know how to write them, but what do they do? I know how to link them to MCs and the stage ect. Is it kind of like writing code into an MC?
Can i write code in them to be executed by the particualr mc? i.e
addEventListner(MouseEvent.MOUSE_MOVE, mouseDrag)
function mouseDrag():void
{
//code here
}
:eek:
Can you help me understand? i have really tryed to but just cant grasp it completley...
Lazer57
12-02-2007, 12:22 AM
I recommend a couple of tutorials on this site:
Tut by Mazoonist (http://www.actionscript.org/resources/articles/698/1/Make-your-own-reusable-classes-using-Flash-and-AS3/Page1.html)
Tut by dvlnblk (http://www.actionscript.org/resources/articles/636/1/Getting-Started-With-AS3-and-Flash-CS3/Page1.html)
I still am not very good with classes but I understand them better now and I can usually get simple things to work, in large part due to those tutorials.
Classes are just bunches of code that have a specific job and which can be used over and over again for different things. Classes can do anything, they are not limited to certain code or activities.
matbury
12-02-2007, 12:22 AM
If you're used to putting your code onto MCs, you're gonna have to make a big adjustment. The good news is, it's much easier to handle your code in classes and OOP, once you've got the hang of it.
You use Flash's built in classes all the time:
var myTextField:TextField = new TextField(); // Instantiate the TextField class
myTextField.font = "Verdana"; // Set the TextField's variables
myTextField.align... etc.
Note: Colin Moock's book 'Essential AS 3.0' is not for beginners, be warned!
You've used methods before and created objects... well, objects are classes and methods are the functions inside them. Instead of writing very long code on your FLA timeline and perhaps writing the same chunks of code over and over again, you can write a class file once and use it as many times as you like.
e.g.
package {
import flash.display.Sprite;
public class SayHello extends Sprite {
private var hi:String = "Hello";
public function SayHello() {
trace(hi);
}
public function sayGoodbye():void {
hi = "Goodbye";
trace(hi);
}
}
}
Any time you want to use the class you can instantiate it (the class file should be in the same directory when you compile (Ctrl + Enter) your SWF) from your FLA timeline or another class file:
var mySayHello:SayHello = new SayHello();
This will execute the SayHello function automatically, tracing: "Hello"
Once you've instantiated the class as a variable, you can access any of its public functions like this:
mySayHello.sayGoodbye();
Which will trace: "Goodbye"
Of course, this is only a short example. You class file could be hundreds of lines of code long and contain dozens of functions, although it's best to keep them simple and specific. The advantage is when you come to write code on your document class or on the FLA timeline. You only have to write 1 or 2 lines of code to represent dozens, perhaps hundreds of lines of code. It makes it much easier to manage your code.
Remember that the document class represents the FLA timeline so you can only have one or the other. If you put code on the timeline and point it at a document class, you'll get an error.
Mazoonist
12-02-2007, 12:27 AM
I just recently finished writing a tutorial on this very subject. I don't necessarily like to toot my own horn, except as it might help someone, but I really believe that this tutorial would be easy enough for you to grasp and begin to see some possibilities. Just click the first link in my signature.
You can accomplish a lot by coding on the timeline, though. You just need to make sure you give all your objects instance names (in the properties inspector), then you can write code that addresses them by name.
Thankyou for all your help, one more question. what exactly does the
extend MovieClip line of code in a package do?:confused:
creynders
12-02-2007, 10:06 AM
Thankyou for all your help, one more question. what exactly does the
extend MovieClip line of code in a package do?:confused:
It's extends. It means that the class you're writing extends the other class which comes after the keyword "extends". This means that it inherits all properties and methods from the other class. In other words, if your class extends the MovieClip class, you have all the same functionality in your class as in the MovieClip class (and more, since you're extending it).
Read up on:
- classes (http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000764.html#wp165352)
- inheritance (http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000820.html#wp118885)
-about writing subclasses (http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000821.html#wp125723)
Cheers, this forums great. Thanks alot for all your help:D
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.