Using properties with your own behavior
Overriding display object properties but why? One good reason: Manipulate your DisplayObject based like anyone else but control its width, height or rotation behaviours. If your want to see a big use of override let’s open Flex framework Sources (Everything is override in UIComponent, properties and methods: Width, height, addChild, scaleX… )
You can override properties or method of every common classes (EventDispatcher, Sound …) but this example deals about Displayobject.
- Override and remove reference to the super-Class:
In this case, if you change the width property of a Sprite, the display Will not change. So you have to write your own behaviour.
override public function set width(nW:Number) : void { _nWidth = nW; draw(); }
- Override and and hold reference to the super-class.
It’s very interesting to add behaviours without change anything to the internal one. For example you may want to hold an Array witch contains all Point witch describe Shape positions. So, just had this method on your subclass:
override public function set width(nW:Number) : void { super.width = nW; _nWidth = nW; draw(); }
An example of a MovieClip based class with possibility to override width, height and rotation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | package com.imarkahann.qbox2d { import flash.display.Graphics; import flash.display.MovieClip; import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; /** * ... * @author Germain LECOURTOIS */ dynamic public class SkinBase extends MovieClip { private var _nWidth: Number = 0; private var _nHeight: Number = 0; public var overrideProp:Boolean = true; private var mcBox:Sprite; public var _tfName:TextField; public function SkinBase() { mcBox = new Sprite(); addChild(mcBox); _tfName = new TextField(); addChild(_tfName); var pFmt:TextFormat = new TextFormat(); pFmt.size = 9; _tfName.defaultTextFormat = pFmt; _tfName.text = "B" + Math.round(Math.random() * 99).toString(); _tfName.autoSize = TextFieldAutoSize.LEFT; _tfName.border = true; _tfName.background = false; _tfName.selectable = false; } override public function set width(nW:Number) : void { if (!overrideProp) { super.width = nW; } else { _nWidth = nW; draw(); } } override public function set height(nH:Number) : void { if (!overrideProp) { super.height = nH; } else { _nHeight = nH; draw(); } } override public function set rotation(nR:Number) : void { mcBox.rotation = nR; } public function draw() : void { if ((_nHeight > 0) && (_nWidth > 0)) { var hw:Number = _nWidth / 2; var hh:Number = _nHeight / 2; _tfName.x = -hw; _tfName.y = -hh; var pG:Graphics = mcBox.graphics; pG.clear(); pG.beginFill(0xCC9933, 1); pG.drawRect(-hw, -hh, hw*2, hh*2); pG.endFill(); pG.beginFill(0x00FF00, 1); pG.lineStyle(3, 0x11FF11); pG.drawRect(-hw, -hh, hw*2, 10); pG.endFill(); } } } } |
To use your Object :
_mcResize = new SkinBase(); _mcResize.width = 30; _mcResize.height = 25; _mcResize.draw(); _mcResize.x = stage.stageWidth /3; _mcResize.y = stage.stageHeight / 2; addChild(_mcResize);
To conclude, this is an example of Sprite based class witch show you difference between an action on width height and an action on its override version.
I will reuse this principle on another article in order to show the writing of QuickBox2D skin class with the skinScale support.
Tags: Actionscript, as3, displayobject, Flash, methods, override, properties