Creating and Using Objects


Flash 5 uses a number of built-in objects. These include everything from the most basic object of them all, the Object object, to the XMLSocket object, which handles the transfer of Extensible Markup Language (XML) data over a TCP/IP socket. The 15 built-in objects in Flash 5 are as follows :

  • Array. Holds a list of data.

  • Boolean. Acts as an object wrapper to True/False values. True/False values are primitive data types. By acting as a wrapper, the Boolean object adds object functionality, in the form of methods , to this data type.

  • Color . Used to modify the color of a movie clip with code.

  • Date. Holds and formats all types of date and time information.

  • Key. Used to track keyboard presses.

  • Math. Acts as a library for complex math operators and functions.

  • Mouse. Used to show or hide the mouse pointer.

  • MovieClip. Points to an actual movie clip on the Stage.

  • Number. Acts as an object wrapper to normal numbers .

  • Object. A blank object with no methods or properties.

  • Selection. Used to set and get information about focus and selected areas of text fields.

  • Sound. Controls the playback of a sound, including pan and volume.

  • String. Acts as an object wrapper to normal strings.

  • XML. Enables the loading, manipulation, and sending of XML data.

  • XMLSocket. Enables the sending and reception of XML data over a socket connection.

When you want to create your own object based on one of the built-in objects in Flash, you use the keyword new. Thus, if you want to create a new array based on the Array object, all you have to do is launch the Actions panel (see Figure 14.1) and type the following:

 myObject = new Array(); 
Figure 14.1. Use the Actions panel to create a new object.

graphics/14fig01.gif

You'll notice that after you type new, you specify the preexisting object on which you want your new object to be based. Something else interesting happens here as well. Not only are you specifying the name of the object you want, but also you're calling a function to create that object. How can you tell? Any time you see parentheses following an item in ActionScript, it's a pretty good indicator that you're dealing with a function or method. The parentheses indicate that you can pass a parameter or argument to the function.

Tip

Notice the semicolon at the end of the line of code in Figure 14.1. Semicolons in ActionScript are like periods on the end of a sentence . They end a statement. Theoretically, you can sometimes get away without using semicolons, but don't go there. As your code becomes increasingly complex, you'll find yourself in a world of hurt if you don't follow the syntax rules.


When you create a new object, you actually are calling the prototype object's constructor method. A constructor method is just a special function that initializes your new object for you. Remember what a constructor method does; you'll be creating your own later!



Inside Flash
Inside Flash MX (2nd Edition) (Inside (New Riders))
ISBN: 0735712549
EAN: 2147483647
Year: 2005
Pages: 257
Authors: Jody Keating

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