If you treat it like an object in your game, most likely you should consider the proposed @Pier OOP approach.
This comes in many flavors:
You can extend the class from Sprite and draw a window as soon as the ADDED_TO_STAGE field is ADDED_TO_STAGE its parent.
public class box extends Sprite { protected var _color:uint; protected var _size:int; public function box(size:int=100, color:uint=0x000000) { super(); _size = size; _color = color; this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage); } protected function onAddedToStage(event:Event):void { this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage); draw(); } protected function draw():void { this.graphics.beginFill(_color); this.graphics.drawRect(0,0,_size,_size); this.graphics.endFill(); } }
This block can be created / created by calling:
var b:box = new box(); this.addChild(b);
Or you can let the box contain itself - which could be more doable if you are dealing with a large number of objects. The box just needs a reference to its parent, then - and, of course, it must provide the dispose () function -
public class box { private var _parent:Sprite; protected var s:Sprite; public function box(parent:Sprite) { _parent = parent; s = new Sprite(); s.graphics.beginFill(0x00000); s.graphics.drawRect(0,0,100,100); s.graphics.endFill(); _parent.addChild(s); } public function dispose():void { _parent.removeChild(s); } } }
In this case, you should build the window as follows: it requires a link to Sprite (or any extension) that has already been added to the scene:
var b:box = new box(this);
In both cases, you can dynamically change attributes and make the object more universal:
public function set size(val:int):void { _size = val; draw(); } public function set color(val:uint):void { _color = val; draw(); }