Browse > Home / ActionScript, Tutorials / Introduction to OOP

| Subcribe via RSS

Introduction to OOP

January 27th, 2009 Posted in ActionScript, Tutorials

OOP: Object Oriented Programming

Object Oriented Programming is a concept rather than a technique or method of coding.  However, within the concept of OOP, there are certain techniques and methods.  Object Oriented Programming (OOP) is an object-based style of programming that uses objects to store and work with data. Templates known as classes are used to define the properties and methods that make up objects in code.  So the point of it is to easily re-use robust code, making developing applications much easier, stronger, and allows more possibilities.

In this tutorial, I will take you through a very basic and easy, but very powerful method and aspect of OOP in Flash.  We will create a loading circle that spins around by attaching external code to the movie clip.  This will hopefully demonstrate to you the power of OOP.














—————————————————————————————————————————————


In a new Flash AS3 file, create a shape for your loading circle and convert it into a Movie Clip symbol with the registration point in the center. You can convert it into a symbol by right clicking it and choose Convert to Symbol.. or hitting F8. Then make sure you select Movie Clip for the type of symbol and that the registration point is in the center. I called my movie clip loader, but it doesn’t matter.

Now we’re going to make a new ActionScript file. So save your FLA file and close that - we will come back to it later on.

Select ActionScript file from the main menu.  In the same folder you have your FLA file, create a new folder called com.  Now save this ActionScript file as Rotator in that com folder.  Now write in the code that you see above in the ActionScript.  The package is the folder path so we can just write com there.  You will need to import the display and events packages to use the classes in those folders.  Now we declare our Rotator class and extend it as a Movie Clip, because we are going to attach this to a Movie Clip, so it has to have the same properties.  Now in the Rotator function we add a Enter_Frame event listener.  And then we create the rotateIt function that will be called on every frame which rotates the object from which the function was called.  The keyword “this” represents the object that we will attach this code to and currentTarget refers to the same object.  When you make a new class, it is protocol that you have your class declaration and class function the same as the name you use to save it. So save that, we’re done with writing the external code.




Open back up or jump back into your FLA file.  Now in your library, right click and choose Properties to open up the properties and linkage properties.  In previous versions of Flash, you can just right click and choose Linkage.















































In the Linkage section, check the box next to Export for ActionScript. You can Export in frame 1, but it’s best not to for preloading purposes; you don’t want unnecessary bytes on the first keyframe. Now in the class path field - next to where it says Class: - write in the path to your external ActionScript file. If you followed along, then this will be com.Rotator, because com is the folder and within it, our file named Rotator.as. That’s it! Now export your SWF and you should see your spinning loading shape. Now you can reuse this code very easily on as many different movieclips as you want. And if you need to change the speed of the spinning for example, you can just go into the as file and change that number.

Leave a Reply

Security Code: