Flash object-oriented programming


Before we get into the code of the ManiFestival, I think it's important to convey a few principles of the Flash 4 programming environment. Flash 4 action scripts become elegant when looked at with the notion of object-oriented programming (OOP) in mind. The concept is simple and you don't need to have programming experience to understand its principles.

In OOP jargon, an object is an individual module of a program. These self-contained modules are designed so that they can be used ”and most importantly, reused ”in various programming projects. An object becomes a building block, which, by design, increases your productivity. It can also make it easy to add and remove features from a larger project without breaking the entire architecture. It can also be much easier to track down problems in the code when designed properly.

How does OOP relate to Flash? In essence, Flash movies are objects, as are Flash movie clips (see figure 08:01).


Figure 08:01.

graphics/08fig01.jpg


Each movie clip is a self-contained module that can be referenced, control other objects, and manipulate portions of the overall movie. The following shows some of the key features of objects in Flash:

  • Objects in Flash have their own Timelines (see figure 08:02). The individual Timeline gives a Flash object its independence from the overall Timeline, as is evident in embedded movies and movie clips.


    Figure 08:02.

    graphics/08fig02.jpg


  • Objects can tell each other where to go in each other's Timelines (see figure 08:03). With use of the Tell Target command, one object can specify which frame in another object is next ”and whether that frame should play or stop. This is a very important feature that lies at the heart of Flash object-oriented programming.


    Figure 08:03.

    graphics/08fig03.jpg


  • Objects have properties (see figure 08:04). Just as objects in life have properties ”an orange, for example, has the properties of size , color , taste, and aroma ”so do Flash objects. A Flash object's properties include scale, position, alpha, visibility, and rotation. The Flash code for setting the rotation of an object labeled "orange" to 30 degrees would look like that shown in figure 08:05.


    Figure 08:04.

    graphics/08fig04.jpg



    Figure 08:05.

    graphics/08fig05.jpg


  • Objects have their own storage containers, called variables . A variable acts as a temporary storage container, which can be accessed from other objects or affect the current object (see figure 08:06). For example, you might want to store a user 's name in a variable called bestfriend.


    Figure 08:06.

    graphics/08fig06.jpg


      Set Variable "bestfriend" = "Sally"  

    Now you've stored the name and you can use it throughout the movie without checking the name every time the screen changes.

    Anything can be put in a variable. In Flash, a variable can hold any type of data ” numbers or text (also known as integers and strings ); you can even simulate arrays in Flash. Unlike as in other languages, in Flash you don't have to declare a variable to be an integer or a string before using it.

    Variables can be named anything you want. The easiest way to get the most out of object- oriented programming is to name your variables usefully. It's difficult to keep track of what var01, var02, and var03 do ”however, it's hard to miss what thePosition, isOffScreen and isHit intend.

  • Any object can change what is inside another object's variables (see figure 08:07). Not only can variables be set as part of the overall movie, but one object can alter another object's variables. This facility allows for advanced interactivity. Suppose that you have a skiing game where you have a skier, the "skier" object. When the skier object collides with a "tree" object, the skier object sets a variable called score in an object called scoreboard.


    Figure 08:07.

    graphics/08fig07.jpg


      Set Variable "/scoreboard:score" = 50  
  • One object can be nested inside another object. Just as one symbol can be composed of other symbols in Flash, one object can be composed of other objects. In fact, that's the essence of OOP.

  • Objects can use hierarchies (see figure 08:08). When one object is inside another, together they're in a hierarchical relationship, just like a folder and subfolder:


    Figure 08:08.

    graphics/08fig08.jpg


      "/meta_orange/orange/sub_orange"  
  • Objects address one another through these hierarchies (see figure 08:09). Objects use a URL-type path name to address one another. For example, if one object wanted to tell another object to go to a different frame, you might see Flash action code like this:


    Figure 08:09.

    graphics/08fig09.jpg


      Tell Target ("meta_orange")  

    However, if you wanted to address another object contained within the meta_orange object, you would call it this way:

      Tell Target ("meta_orange/orange")  

    The forward slash tells ActionScript that you are addressing the object orange, which is an object inside the object meta_orange.

    You can even address an object that contains the current object by going up a level, like this:

      Tell Target ("../orange")  

    The two dots and forward slash tell ActionScript that you are addressing the object above the sub_orange object. In this case, the object above sub_orange is the orange object.

    To address an object on the uppermost level, on the main Timeline, use a leading forward slash:

      Tell Target ("/meta_orange")  

    In this case the meta_orange object is on the top level, and we call the top level using the forward slash (/).

    In fact, you can address the main Timeline alone:

      Tell Target ("/")  

    The main Timeline doesn't have a name really so this is the best way to address it.

    Just as we use the hierarchy to address a Tell Target command, we can use the hierarchy to address variables and set properties in each other's objects.

    Use the following to set an object's property:

      Set Property ("meta_orange/orange/ sub_orange", rotation) = "179"  

    In this case, the sub_orange object would turn 179 degrees (about a half of a turn ) on the screen when you set its rotation property.

    Use the following to set a variable in an object (note the colon before the variable name):

      Set Variable "meta_orange/orange:myColor" = "green"  

    Setting the variable to green doesn't change anything on the screen. I simply set the storage container "myColor" to contain the word green.

    Later I might use that word in another part of my code:

      If (myColor eq "green")   Go to and Stop ("green")   End If  

That's your primer for Flash 4 programming and architecture for now. Let's drop into the ManiFestival movie to see how to use this knowledge in a real-life example.



Flash Web Design The Art Of Motion Graphics
Flash Web Design oder: the art of motion graphics
ISBN: 3827256623
EAN: 2147483647
Year: 2005
Pages: 192

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net