Link to home
Start Free TrialLog in
Avatar of msukow
msukowFlag for United States of America

asked on

AS3 Why use classes?

I understand how classes are constructed, but not sure why I need to use them. I understand that they can be used to extend movieClips, etc. However, I seem to be able to code each project without using classes, even fairly involved site.

Can someone explain why I should use classes? For example, what functionality absolutely needs to use classes, or it is impossible.
ASKER CERTIFIED SOLUTION
Avatar of asaivan
asaivan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of msukow

ASKER

So, there are not any situations that demand classes; however, classes are more useful as the site's architechure grows larger in scope, correct?

I use many, many functions and seem to have no issues. However, I am finding that some of my code / function in one project could be used in another. If I start making use of classes, then instead of copy/pasting the functions, I could add the external class/packages to new projects and be that much more efficient, correct?
Correct, you don't HAVE to use classes in a sense, although by coding in Flash you are already making use of Classes which are already ready for use,  such as the MovieClip class.  

And also, correct, you can reuse code by making classes which encapsulate specific functions, which is really what ActionScript is based on.  MovieClip, Math, Mouse, Sprite, Stage... the list goes on and on, these are all Classes which encapsulate functionality and are re-used by the Flash designer / developer.
Avatar of msukow

ASKER

Thanks a lot - you really helped me get over this knowledge hump!
Avatar of rascalpants

Well, it is a more advanced way of thinking, but everything in Flash is run by a class.  A MovieClip on the stage is actually a class.  It is just a visual representation of what the Flash IDE (software) puts on the code.  You create a red square with a black border on the stage and turn it into a Movieclip, and Flash does this behind the scenes....


var rect:Sprite = new Sprite();

rect.graphics.lineStyle(1, 0x000000, 1);
rect.graphics.beginFill(0xFF0000);
rect.graphics.drawRect(10, 10, 200, 200);
rect.graphics.endFill();
addChild( rect );


actually, the IDE probably just used the byte code, but basically this is what is happening...


but the question, is why use classes, when you can do the same thing in the Flash IDE with framescript?

Let's say that there is an ad agency that has 3 designer and 2 flash developers that all will be working on a project.  This project will be a web site that has a lot of functionality like a painting tool, a send to a friend option, a download assets tool, a video player, and many other tools... but it is all combined into one or two pages.  Now, there will be a ton of overlap here with the kind of things that need to be utilized, such as preloaders for all of these tools, database or XML connection code, etc, etc.  Now imagine, that this project needs to be completed in a 2 week time, so there is no way a single designer or developer would be able to knock this out.  So you break up the project, and have a few designers working on different parts of the applications along with developers doing the same.

Well, what if this project and all of the code was done in one SWF?  The only one person could be working on it at a time.  and it would take about 6 weeks to complete it this way.

But instead of this, you can have ALL of the code be seperate from the FLA... I mean ALL code...  all you do is setup classes for your movieclips in the Libraries properties window.  You associate all of the interface elements with classes that they themselves have control over....  This way the designers can be working on the FLAs and change/update the graphical portions of the project, an no matter what shape, color, or position they put the movieclips, as long as they keep them in the same scope or structure, then the code will work...  and more importantly, the Flash Developers can work on the individual Classes for the functionality without ever needing the latest copy of the project FLAs.

This process is much more efficient... and now imagine doing this 50 times in a year...  there will be all sorts of overlap and features that can easily be "ported" from one project to the next, and you already know they work(in theory), cause they came from a previously launched project.  So instead of only being able to complete 10 to 15 projects a year by using Framescript, you can do it the right way and complete 50 to 60 projects in a year.


And just to think about it in another way...  imagine all of your CSS code in an HTML document being inline with the page elements, instead of assigning classes or IDs to them and using a master.css file.  Image the headache of having to change all of the underlined headers on a web site with 35 pages...  or you could just change one or two lines of code.


hope this sheds a lot of light on why we work this way...  you would be suprised how quickly and more accurately you can develop when you use these techniques.


rp / ZA