# CenterStage element

**URL:** <https://forum.kirupa.com/t/centerstage-element/256728>\
**Category:** flash\
**Created:** [April 6, 2008, 4:52pm UTC](https://forum.kirupa.com/t/centerstage-element/256728 "2008-04-06T16:52:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![snozbunny](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@snozbunny](https://forum.kirupa.com/u/snozbunny)\
**Post date:** [April 6, 2008, 4:52pm UTC](https://forum.kirupa.com/t/centerstage-element/256728/1 "2008-04-06T16:52:48Z")

</div>

I’m a bit new to AS3 (It’s just a hobby) and I wanted a opinion regarding how to go about creating a center stage element. I’ve got two versions so far, one just adds the element from the document class and the second creates a new instance.

Every time I ask about OOP I get a new answer. Some say only use it if you plan to re-use the code in the same script, some say use it as much as possible because it’s there to compartmentalize. I’ve never gotten a straight answer so please excuse me if I utilized it incorrectly.

Here is what I’m presently using, it works but I want to be sure my comprehension of the feature isn’t flawed.

```php

package {
    import flash.display.Sprite;
    import components.centerstage;

    public class index extends Sprite {
        private var centerStage:centerstage;
        
        public function index():void {
            centerStage = new centerstage(stage);
        }
    }
}

```

```php

package components
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.display.Stage;

    public class centerstage extends Sprite
    {

        // -----[Creates container]-----

        public function centerstage(target:Stage):void {
            target.stage.addEventListener(Event.RESIZE, centerContainer);
            this.addEventListener(Event.ADDED, centerContainer);
            target.addChild(this);
        }
        
        // -----[Centers the container]-----
        
        private function centerContainer(event:Event):void {
            this.x = Math.round((stage.stageWidth/2)-(this.width/2));
            this.y = Math.round((stage.stageHeight/2)-(this.height/2));
        }
    }
}

```

Also, if anyone sees what is considered to be a bad habit or has suggestions on optimizing it would be greatly appreciated. Thanks in advance!
