First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Jonathan Kaye | Website: http://www.amethyst-research.com

Step 9. Insulating your Implementation with Object.addProperty

We designed the triangle component with good programming practice in mind, namely, defining access methods such as setBase, getBase, etc. for the outside world to use in order to access internal variables tbase, etc. In that way, if we changed how we store the base internally, we would just have to fix getBase and setBase, for example, and we would not break developer's code that used the component. However, Flash does not have a way of enforcing public and internal (private) class areas, such as other object-oriented languages like C++. Unfortunately, that means rogue developers can go in and access and manipulate properties that you really never intended for them to see.

The new addProperty method allows you to define a property that, when accessed, calls a function to compute a value, instead of storing the value in a real variable. For example, we can define a property called "base" that uses setBase and getBase to do its setting and reading, respectively. When we tell the outside world to use "base", "height", and other of these properties, we know that they are really calling the functions we have defined and not using the internal variables. Therefore, if we change the internal implementation, we only have to update the access functions and code built with the component will behave properly.

Here is the code to add within the #initclip/#endinitclip block (after getBase and setBase have been defined):

TriangleClass.prototype.addProperty("base", TriangleClass.prototype.getBase, TriangleClass.prototype.setBase);

The addProperty method takes three arguments: the name (in double quotes) of the new property, the method to use in order to access the 'value' of the property, and the method to use to store a new value. The get function must return the value, and the set function must accept one argument, which is the new value. The functions can do whatever computation is necessary to produce the property value. An example of this is the "area" property:

TriangleClass.prototype.addProperty("area", TriangleClass.prototype.getArea, null);

As you can see from the code, we do not explicitly store the triangle's area, rather, we compute when requested to do so. From the user's perspective, it does not matter because we compute it when the user requests that property. For area of a triangle, there is no way to set its value explicitly, so we pass in null.

For completeness, we also define a "height" property (not to be confused with the _height property of the movie clip, which is the y axis extent of the component)

TriangleClass.prototype.addProperty("height", TriangleClass.prototype.getHeight, TriangleClass.prototype.setHeight);

Suppose a developer creates an instance of your triangle component called "tri0". We tell the developer about the public properties, namely area, base, and height. Now we do not have to worry about potential problems caused by implementation changes in the future.

Advanced: Robin Debreuil pointed out (thank you!) that one catch with this way of defining properties is that it will not work if you override the get or set methods for a sub-class, because the method will remain set from TriangleClass. To be the safest possible, if you believe your class may be sub-classed and the methods overridden, use the following as an example:

TriangleClass.prototype.addProperty("height", function () { return this.getHeight(); }, function (v) { this.setHeight(v); } );

» Level Intermediate

Added: 2002-04-16
Rating: 8 Votes: 55
(10 being the highest)
» Author
Jonathan Kaye, PhD, is the President and CTO of Amethyst Research LLC, an award-winning interactive design and engineering firm specializing in the creation of online device simulations. He and David Castillo are the authors of "Flash for Interactive Simulation: How to Construct and Use Device Simulations", to be published by Delmar Thomson Learning in November, 2002 (the accompanying web site will be www.FlashSim.com).
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!


  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.