< Day Day Up > |
7.3 Adding setPosition( ) and setSize( ) MethodsNow let's give the ImageViewerDeluxe class its new features: the abilities to reposition and resize the viewer. We'll implement two new methods to support these features:
Here's the setPosition( ) method: public function setPosition (x:Number, y:Number):Void { container_mc._x = x; container_mc._y = y; } The setPosition( ) method is declared public , allowing it to be accessed from any code inside or outside the ImageViewerDeluxe class. The body of setPosition( ) uses the good old-fashioned _x and _y properties of the MovieClip class to position the image viewer on screen. Notice that code in the ImageViewerDeluxe class has direct access to the container_mc property. As we learned in Chapter 6, a subclass can access all of its superclass's properties and methods, even those declared private . Here's the setSize( ) method, which resizes the image viewer by recropping the image: public function setSize (w:Number, h:Number):Void { createImageClipMask(w, h); createBorder(w, h); container_mc.image_mc.setMask(container_mc.mask_mc); } To resize our image viewer on screen, we first re-create its mask: createImageClipMask(w, h) then we re-create its border: createBorder(w, h) and finally, we reassign the new mask movie clip ( mask_mc ) as a mask over the image movie clip ( image_mc ): container_mc.image_mc.setMask(container_mc.mask_mc); The setSize( ) method gives a good sense of the convenience and logic of OOP. Every class offers a set of services; other classes can use those services to build new functionality. In setSize( ) , most of the work involves simply invoking methods that already exist in the ImageViewer class. Example 7-2 shows the updated code for the ImageViewerDeluxe class. Enter the new code, shown in bold, into your ImageViewerDeluxe.as file. Example 7-2. The ImageViewerDeluxe class with reposition and resize features/** * An ImageViewer subclass that adds reposition and resize features */ class ImageViewerDeluxe extends ImageViewer { public function ImageViewerDeluxe (target:MovieClip, depth:Number, x:Number, y:Number, w:Number, h:Number, borderThickness:Number, borderColor:Number) { super(target, depth, x, y, w, h, borderThickness, borderColor); } public function setPosition (x:Number, y:Number):Void { container_mc._x = x; container_mc._y = y; } public function setSize (w:Number, h:Number):Void { createImageClipMask(w, h); createBorder(w, h); container_mc.image_mc.setMask(container_mc.mask_mc); } } |
< Day Day Up > |