| Ultrashock Tutorials > Flash4 > Object oriented Actionscript | ||||
|
||||
| Object oriented Actionscript | ||||
| Introduction You have mastered the actions and know your expression inside and out. The next step is to think about the most intelligent way to put it all together. The structure of Flash is very object oriented and understanding this can help you write better actionscript. An important note: this article relates to Object Oriented thinking in Flash4. Flash5 brings much more sophisticated object oriented coding to actionscript. We're all objects Object oriented programming is found in modern programming languages such as java. The idea of objects is that they combine both the information and the functionality in one tidy unit. The general terminology is that objects have properties (height, width, colour) and methods (dance, draw, move). The key is that the functionality is grouped within the object and does not lie outside of it. A movie clip symbol in Flash can be thought of as an object. Each instance of a movie clip has properties, such as position and scale, as well as methods, such as Set Property, which allow us to alter these properties. Movie clips can also contain actionscript which give it behaviour such as movement. Strategies in actionscript One of the tough parts of writing good actionscript is choosing the appropriate place to put it. Sometimes the answer is to put all your code in one timeline. Other times you may end up spreading it across nested timelines. A common approach is to have the control lie at a top level and "talk down" to the various movie clip instances. Think of this as handing someone five remote controls with which to drive a set of cars. The cars don't have any built-in ability to get around. Every time you add a car you have to hand the person another set of controls and point out which car is which. The alternative is to get a driver for each car. The driver comes bundled with the car. They never have to worry about which car they are controlling, and adding a new car is never much of a problem. In Flash, keeping the artwork and actionscript together inside of movie clips allows us to create new instances by draging out a new instance of the symbol or using the duplicate movie clip action. They may still need some guidance from a higher authority (the guy at the starting gate for example), but otherwise they can be left alone to do what they do. An example
Here is a very simple example where we can take advantage of the object oriented approach. Here I've created a movie clip and added actions to my parent timeline which make it move about in a random fashion. Here is what the actions look like:
Set Property ("nervous", X Position) = nervous:_x+random(5)-2
Set Property ("nervous", Y Position) = nervous:_y+random(5)-2
Here we need to use the target instance to identify which movie clip we plan to shake. If we decide to introduce another movie clip and want it to act in a similar way we need to give each one a new instance name and repeat the above actions for each one. The alternative is to include the actionscript inside the movie clip. Here is what the actionscript looks like there:
Set Property ("", X Position) = _x+random(5)-2
Set Property ("", Y Position) = _y+random(5)-2
You'll notice that we can leave out the targets because without them Flash will assume you are talking about the timeline where these are found. That eliminates one major source of errors in Flash. Also, if we want to add more instances of the same movie clip we simply drag them onto the stage and they know what to do.
Breaking it down The benefits of this approach are familiar to any experienced Flash author - consistency, reusability and malleability . When tackling complex problems in Flash think of how you can break them down into a set of smaller problems. Grouping the visuals and functionality into movie clips will help minimize duplication of effort and get you there quicker. |
||||
|
©2000 Ultrashock.com Inc.
- All rights reserved
|