Section 8.7. Shared Objects and Custom Classes

8.7. Shared Objects and Custom Classes

With a little extra work, you can store instances of a custom class in the slot of a shared object. For example, if you are developing a shared whiteboard, a shared object could contain all the shapes that define each graphic in the whiteboard. The client-side Rectangle class in Example 8-9 can be used to create a simple ActionScript Rectangle instance using the new operator that will draw itself into a movie clip. The Rectangle object can also be stored in a shared object slot so that other movies can use it to draw the same rectangle.

Registering the class using the Object.registerClass( ) method is the essential step that makes storing and calling methods on an instance of a class possible in a shared object slot.

Example 8-9. A client-side Rectangle class
 // Rectangle constructor called when instantiated using   new Rectangle(  )   // and when a rectangle object is copied into a remote shared object. function Rectangle (xMin, yMin, xMax, yMax) {   if (typeof xMin == "undefined") return;  // Return if being deserialized   this.xMin = xMin;   this.yMin = yMin;   this.xMax = xMax;   this.yMax = yMax; } // Register the Rectangle class. Object.registerClass("Rectangle", Rectangle); /* Default prototype properites.  * Note: methods are not saved in the shared object;  * they must be defined in each movie. */ Rectangle.prototype.bThickness = 2; Rectangle.prototype.bRGB = 0x0000AA; Rectangle.prototype.bAlpha = 100; // A simple   draw( )   method that expects to be passed a movie clip reference. Rectangle.prototype.draw = function (mc) {   mc.lineStyle (this.bThickness, this.bRGB, this.bAlpha);   mc.moveTo (this.xMin, this.yMin);   mc.lineTo (this.xMin, this.yMax);   mc.lineTo (this.xMax, this.yMax);   mc.lineTo (this.xMax, this.yMin);   mc.lineTo (this.xMin, this.yMin); }; Rectangle.prototype.setBThickness = function (thickness) {   this.bThickness = thickness; }; Rectangle.prototype.setBRGB = function (rgb) {   this.bRGB = rgb; }; Rectangle.prototype.setBAlpha = function (alpha) {   this.bAlpha = alpha; }; 

For example, a movie that includes the definition for the Rectangle class from Example 8-9 can create a Rectangle object using the new operator, draw with it, and save it into a shared object:

 var r = new Rectangle(10, 10, 32, 30); r.setBRGB(0xFF0000); r.draw(canvas_mc); shapes_so.data.myRectangle = r; 

Later, another movieprovided it also includes the definition for the Rectangle class from Example 8-9can use the Rectangle object in the shared object to draw:

 shapes_so.data.myRectangle.draw(canvas_mc); 

You must do three important things to make this work:

  • The class must be defined wherever it is used. In this example, the Rectangle class definition must be present in any movie that wants to create and store a Rectangle object in a shared object and must be defined in any movie that wants to draw a Rectangle object stored by another movie.

  • The class must be registered using Object.registerClass( ) . A more common use for registerClass( ) is to associate a movie clip symbol with a class. In this example, the arbitrary string "Rectangle" is used since the Rectangle class is not associated with a movie clip. When a custom class is a subclass of MovieClip , movie clip properties such as _x and _y will not be copied to remote shared objects.

  • The constructor is called when a movie creates an object before placing it in a shared object slot. When movies receive a copy of the object, the properties of the object are loaded into the object and then the object's constructor is called without any parameters. To make sure the constructor does not overwrite the data already copied into the object, the constructor must test whether it is safe to overwrite any properties. In Example 8-9, the constructor checks to see whether any parameters are being passed in before updating the object.

Custom classes can also be used in shared objects in SSAS. However, the constructor of a server-side object is called before an object's properties are copied into it. The onInitialize( ) method, if any, is called just after the property data is copied into the object. Example 8-10 is an SSAS implementation of the Rectangle class and is designed as a complement to the client-side Rectangle class in Example 8-9.

Example 8-10. Server-side Rectangle class definition
 function Rectangle (  ) {   // Serialized properties are not available yet. } Rectangle.prototype.onInitialize = function ( ) {   // Serialized properties are available. }; Rectangle.prototype.getXML = function ( ) {   var buffer = '<Rectangle ';   buffer += 'xMin="' + this.xMin + '" ';   buffer += 'yMin="' + this.yMin + '" ';   buffer += 'xMax="' + this.xMax + '" ';   buffer += 'yMax="' + this.yMax + '" ';   buffer += 'bRGB="' + this.bRGB + '" ';   buffer += 'bThickness="' + this.bThickness + '" ';   buffer += 'bAlpha="' + this.bAlpha + '" ';   buffer += '/>';   return buffer; }; application.registerClass("Rectangle", Rectangle); 

Example 8-10 does not include a draw( ) method, as there is no point in having one on the server (the server application has no Stage on which to display graphics) but includes a getXML( ) method, which generates an XML tag representing the shape for storing in an XML text file.

In a whiteboard component, the client-side part of the component could respond to a user request to draw a rectangle by placing a rectangle instance in a shared object slot. Other clients would detect the new slot and add the rectangle instance to the whiteboard. If the whiteboard must be saved, the server-side part of the whiteboard could generate XML to represent each part of the drawing.



Programming Flash Communication Server
Programming Flash Communication Server
ISBN: 0596005040
EAN: 2147483647
Year: 2003
Pages: 203

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