• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 528
  • Last Modified:

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.
  • 2
  • 2
1 Solution
Classes are primarily a programmer thing.  They're more of a conceptual framework to help code be cleanly separated into functional areas of responsibility, thus making code more readable, more organized, and more maintainable.

When you divide your code into classes--that is, if you work primarily with code and are more of a 'developer' rather than a 'designer'--you are taking a higher level approach to architecting your code.

I used to code in the Flash IDE, just on the timeline. But eventually, the more I coded my Flash projects rather than using the IDE, I started extracting code into classes so I could make better sense of what I had done.  So when I would go back and try to reuse code I wouldn't necessarily have to hunt and peck through already written code, but I would have code that was clearly labeled as a Class which had a specific function.  e.g. A RollOverGlow class, which would add a Glow Filter to a MovieClip when I rolled over with the mouse....those kinds of things.

You can take any number of approaches, and ultimately it's what works best for you and your team.  Using Classes is an evolution of programming from the more 'procedural'  i.e. "One Giant Function" approach to the 'object oriented', Class structured approach.
msukowAuthor Commented:
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.
msukowAuthor Commented:
Thanks a lot - you really helped me get over this knowledge hump!

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.drawRect(10, 10, 200, 200);
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now