One of the biggest advantages to using OOP in your Flash projects is keeping your code neat and organized. In this presentation we are going to look at how we can build a fully functioning ferris wheel and keep all of its internal code separate from our main application. At the end of the video you should be insanely excited about applying this principal to your own projects.
Why use OOP in your Flash Projects Plus a Quick Overview
note: any visual glitches are due to my crappy editing and not performance of the swf:)
A Detailed Look at How the Code is Organized and How it Works
Background
The overwhelming majority of the files created for this project were provided by Cor Van Dooren. It was great working with him and I learned a lot. When I first asked him to create an OOP-based Ferris Wheel example I gave him minimal specs:
The creation of a FerrisWheel class with the following features:
-When constructed it will be told how many arms it will have
-The number of arms will be evenly spaced
-The ferrisWheel will have methods for—stop() will stop the rotation
—play() will play/resume the rotation—update() which will loop through all the arms and set their rotation to the proper mount.
—--this method should only run while the ferrisWheel is spinning
—--this method could either perform the calculation in FerrisWheel or call an updateRotation in Arm.It would be great to be able to demonstrate some sort of communication between the classes that make up a FerrisWheel.
Ultimately I would like to have an example where a document class
creates 2 or three FerrisWheels with varying numbers of arms by simply doing:var f1:FerrisWheel = new FerrisWheel(6);
fl.x = 300;
fl.y = 300;var f2:FerrisWheel = new FerrisWheel(12);
fl.x = 600;
fl.y = 300;
Cor definitely over-delivered in a good way. In order to make things a touch more beginner-friendly, I butchered his code down to a point that I could easily understand:) As I mentioned in the video (yes, you are supposed to watch the whole thing), his initial approach to his Main.as file would be super-duper in a FerrisWheel-Factory or FerrishWheel-Manager type class. I just thought it involved a bit too much work for people to get a single FerrisWheel up and running.
I have included Cor’s original files in the downloadable source files below so that you can see how he had it all working.
I know I rushed a bit going over his code in the end. I want to make it clear that his approach offered the same level of control and features, just in a different way. The main guts of how a FerrisWheel is built in both examples was all his doing, and I’m extremely thankful to have had such a strong starting point.
Where is the GreenSock?
Some of you may be wondering, why not use GreenSock for the animation? Well, the answer is that I just didn’t want to alienate people who are not yet onboard with the most robust and easy to use tween engine (for which there are many tutorials). There are in fact some downsides to tying the animation to an ENTER_FRAME and GreenSock would offer the following benefits:
- All the animation in your application could be tied to one central tween engine that could easily stop/kill any animation.
- If the frame rate bogs down, so does the speed of your wheel. If you use TweenLite/Max, it’s time-based.
- You could slow down, speed up or ease the rate of playback.
- You can easily tween to any angle of rotation, even in reverse!
I hope to have an example of this sometime.
Download Source Files
Resources
As mentioned in the video here is the explanation of ADDED_TO_STAGE event from Emanuele Feronato.
What can YOU do?
I had a great time working with Cor all the way over in the Netherlands. Many of you have offered your services of creating files that I can discuss and share with the snorklsphere. I would love to hear about what you would like to contribute as I am sure that the more methods we are exposed to, the better off we all will be. Furthermore, I would love to have some help.
Leave your thoughts below. Thanks!
Carl







