view source

Pre-Object Oriented Programming in Actionscript, part 1

July 5th, 2007 . by polyGeek

Introduction

This series of articles are not intended to teach you object oriented programming. They are intended to teach you the framework and syntax around OOP. This is like the Pre-Calculus class you took before taking Calculus. It makes things a lot easier down the road. At least that’s my hope.

Pre-Object Oriented Programming

The problem with learning to apply OOP practices in your Actionscript is that it’s a radical departure from your existing workflow. Unless you have time to sit down and peruse Colin Moock’s Essential Actionscript book, a few times, then it’s just a frakking pain to try to switch over all at once when you know full well you could go about doing things as you always have and get the job done.

Besides, who cares if you use OOP practices? It’s likely that no one but yourself will ever see your code. And if the end result is the same then why bother. Right?

Wrong. Adopting OOP coding into your projects will make them much easier to write in the first place, and they’ll be easier to update later.

Plus, you can put it on your resume and make more money. :-)

Note: I’m assuming here that you are comfortable with at least creating variables and the occasional function. If you know how to pass parameters to a function then you’re in good shape and if you know how to return a value from a function then everything’s shinny.

The best way to learn OOP in Flash Actionscript is to start with the syntax. Here is a list of some of the things you don’t need to know in order to get started.

  • instantiate,
  • public vs private,
  • instances,
  • objects,
  • constructors,
  • methods and properties,
  • design patterns

Yep, you heard me. Skip all that crap. You can pick that stuff up later.

No more _root

When you write code do you create variables on the _root, like say myVariable, and then refer to them from code inside MovieClips all located all over your FLA with: _root.myVariable? Well, no more.

Here’s what you do:

  1. Create an FLA and save it with any fileName you want.
  2. In the same folder as the FLA create a new .as file.
  3. Give the .as file a fileName of: Root.as
  4. Copy the code from below and paste it into the the Root.as file
  5. Make sure you save the Root.as file.
  6. In the FLA you created write this: trace(”from the Root class: ” + Root.myVariable);
  7. Publish and your Output panel should say: from the Root class: some value

Here’s the code for the Root class:

This is the first step into a much larger world. From anywhere in your FLA you can access the variables in your Root class just as you did with _root before. But now your variables are in a different file and easier to locate.

Lets go over this class file real quick.

  • The public static var is just what you put before each and every variable - variables are called properties in a class.
  • The :String or :Number are how you type your properties. You don’t have to do that but you really, REALLY should. Trust me on this. Just get used to it.
  • It is a convention, and one you need to follow, to make all class names start with a capital letter. So don’t make a class name such as myClass. It should be MyClass.

A few gotchas

Very, very important: the fileName of the class file and the name of the class must be identical. Except the class name does not get the .as extension. So if the filename of your class file is SomeClassName.as then the name of the class must be SomeClassName. If you named the class SomeClassname - lowercase n - it wouldn’t work.

I’ll tell you right now, this above issue gets me all the time. The problem is that you usually give the file a fileName before you give the class a class name. Once you start write your code you think to yourself, “This ClassName would be more descriptive if it were called SuchAndSuch. So you change it, then forget to change the fileName and everything’s a mess. You’ll may do this a few times yourself so be on the lookout.

You know how with FLA files you can publish them anytime to see your changes without saving first? Well, you can’t do that with .as files. If you change something in your .as file you have to save that file (Ctrl + s) before you can publish the FLA to see those changes.

You do not need to upload the .as files to the server in order for the SWF to work. When you publish your FLA file, creating the SWF, it takes the code in all your .as files and wraps it up into the SWF. Now the SWF can stand on it’s own.

If you share your FLA with someone then you have to give them all the .as files as well. The FLA won’t work without them.

What’s next

You put some variables - properties - in your class file. So what about functions, called methods in a class file? That’s just as easy to do. Here’s an example added to the code above.

Lines 17-19 make up the method doSomething. If from anywhere in your FLA you typed: Root.doSomething(); you would see the Output panel display “this is code running in a method of the Root class.”. Which doesn’t do much of anything. Lets add another one that’s a little more useful.

Take a look at lines 21-25. The method average2numbers receives to arguments: a and b. It takes those two numbers and adds them together, then divides by two and then returns that result. That’s why line 21 has :Number appended. That declares the type of value that the method will return.

So back in your FLA you can say something like this:

Line #1 says, I want to create a variable, of type Number, and I want it’s value to be equal to the result that comes from sending the parameters (2,6) to the method average2numbers in the Root class. Then line #2 just traces that result to the Output panel.

This should enhance your workflow right off the bat by giving you a place to put your variables and functions to get at them faster than if you placed them on the _root of your FLA.

In part 2 you’ll learn how to make those public parts private and create getters and setters.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Digg
  • Facebook

9 Responses to “Pre-Object Oriented Programming in Actionscript, part 1”

  1. comment number 1 by: rob

    Moving all your _root variables into a Root class is really just shifting a bad practice to a slightly less-bad practice.

    This is more like an introduction to AS2 rather than OOP. As you said, there is no OOP going on here at all - making the rest of the post pretty misleading. However it is a great introduction to AS2 for AS1 coders, and a step in the right direction to eventually picking up OOP.

  2. comment number 2 by: polyGeek

    @Rob, thanks for the comment. I totally agree that it’s moving from a bad practice to a slightly less bad practice. I think in most cases people tend to teach OOP and the syntax at the same time. Why not go ahead and learn the syntax first, how to create a class file - .as - and get data from it.

    Later, when learning OOP practices things will make a lot more since if they aren’t bogged down with the syntax.

    Besides, this approach reaps immediate benefits.

    There’s more to come in this series that will end with instantiating a class to create a new object.

  3. comment number 3 by: eddy

    Small typo:
    So if the filename of your class file is SomeClassName.as then the name of the class must be SomeFileName.

  4. comment number 4 by: polyGeek

    @eddy, Good eye. Thanks. It’s all fixy now.

  5. comment number 5 by: ryan

    Well I like where you coming from. But perhaps your example seemed a bit misleading to some. I think that learning OOP is a big step, and very difficult at first.

    I like the idea to encourage AS1 developers to move on to AS2…

    But if you want my opinion, skip AS2 and go straight to AS3…

    This may seem harsh but really it is far more logical and structured. Learning AS3 will give you far more results and after getting used to it, AS2 will seem like a breeze. Besides AS3 is really similar to AS2 with better objects to play with.

  6. comment number 6 by: polyGeek

    @Ryan, tough call either way. I guess that could be a whole separate post: if you’re just starting to learn OOP should you start with AS2 or AS3?

    My first thought is it depends on what sort of development a person will likely be doing. A designer probably isn’t going to need much of the benefits of AS3. In fact it might be a real workflow problem for them. For instance I have a project at work that I would love to do in AS3/Flex but I can’t because I’ll have assets coming from designers that use AS2.

    But, a person looking to do more application type coding should be all means learn AS3 first. This post is definitely not written for that person in mind. I wrote it with the designers at work in mind, many of whom have just rudimentary - sorry guys - Actionscript knowledge.

    With that in mind I’ve added a short intro to cover just this topic. What do you think?

  7. comment number 7 by: Phillip Kerman

    This topic of whether to learn AS2 or AS3… it’s a toughy. AS3 may very well be easier to learn if you’re planning to become a “real” programmer. If you’re just attempting to make more powerful applications and you know some Flash, I think it’s worthy to say that you may not ever need to learn AS3. The thing that I think is misleading some people is the fact that you normally expect to upgrade, relearn, and take on whatever new technology comes along. I don’t see AS3 as an incremental jump… it’s really big. I’d only advise for those who HAVE to or who plan to become programmers. Side note: perhaps even more important would be for Flex folks to learn Flash player.

  8. comment number 8 by: polyGeek

    @Phillip, Good points. I’ve never heard anyone suggest that the “real” Flex programmers should take the time to learn the intricacies of the Flash player. I think that’s where the Flash dev who grew up with the advancements of the Flash player really has an advantage.

    You’re absolutely correct that many Flash designers who already know a little AS2 may not need to learn AS3 for a long time to come. AS2 is extremely powerful and incorporating any class structure to their projects can take them a long way.


  9. polyGeek.com » Blog Archive » Pre-Object Oriented Programming in Actionscript, part 1 This series of articles are not intended to teach you object oriented programming. They are intended to teach you the framework and syntax around OOP. (tags: actionscript as2 code tips tutorial oop flash as3 classes) …

Leave a Reply

Name

Mail (never published)

Website

- Why ask? This confirms you are a human user!

   




© Copyright 2008 polyGeek.com / Dan Florio, All Rights Reserved Except Where Explicitly Stated
Web Developement Blogs - Blog Catalog Blog Directory
M2 Websites