Introduction

Flash is a lightweight cross-platform runtime for rich media, enterprise applications and mobile applications, as well as an integrated development environment. Flash can be programmed in ActionScript 1/2/3.

Thursday, October 9th, 2003 at 7:54 pm

VisibleObject

A little but useful class I use in almost every project that needs visible Objects like MovieClips on stage e.g. in a GUI.

class VisibleObject{
        public var type:String;

        private var container_mc:MovieClip;
        private var mc:MovieClip;

        private var depth:Number;

        private var xPosition:Number;
        private var yPosition:Number;

        private function VisibleObject(){
                type = “VisibleObject“;

                container_mc = _root;

                depth = container_mc.getNextHighestDepth();

                xPosition = 0;
                yPosition = 0;
        }
        public function create():Void{
                mc = container_mc.createEmptyMovieClip(getMCName(), depth);
                mc.master = this;
                setPosition();
        }
        public function remove():Void{
                mc.removeMovieClip();
                delete this; // Does this really work?
        }
        public function draw():Void{
                throw new Error(”The draw function hasn’t been overwritten!“);
        }
        public function clear():Void{
                throw new Error(”The clear function hasn’t been overwritten!“);
        }
        /*public function reset():Void{
        throw new Error(”The reset function hasn’t been overwritten!”);
}*/
        public function getBoundingBox():Void{
                throw new Error(”The getBoundingBox function hasn’t been overwritten!“);
        }
        public function getWidth():Void{
                throw new Error(”The getWidth function hasn’t been overwritten!“);
        }
        public function getHeight():Void{
                throw new Error(”The getHeight function hasn’t been overwritten!“);
        }

        private function setPosition():Void{
                mc._x = xPosition;
                mc._y = yPosition;
        }
        private function getMCName():String{
                return type + “_” + depth + “_mc“;
        }

        public function getMC():MovieClip{
                return mc;
        }

        public function setContainer(aContainer:MovieClip):Void{
                container_mc = aContainer;
                depth = container_mc.getNextHighestDepth();
        }
        public function getContainer():MovieClip{
                return container_mc;
        }

        public function setXPosition(aXPosition:Number):Void{
                xPosition = aXPosition;
        }
        public function getXPosition():Number{
                return xPosition;
        }

        public function setYPosition(aYPosition:Number):Void{
                yPosition = aYPosition;
        }
        public function getYPosition():Number{
                return yPosition;
        }
}

If I want to create a visible Object on stage e.g. a button, I create a Button class and let it inherit from the VisibleObject class. Then I specify all the drawing informations etc. After that steps I’m able to create a visible Button on stage just by writing:

myButton:Button = new Button();
myButton.create();

I’ll upload a real life example where it comes in handy in a few days.

4 Responses to “VisibleObject”

  1. kaneda

    Quite cute but:

    1. Whats the Name of the Instance and the level on the mc.

    2. Why don’t you throw Special Errors ?

    3. Why do you throw even Errors ? Ever heard of Interfaces ?

  2. Simon Wacker

    1. If you create your whole application based on this approach, you don’t have to matter what the name or the depth of the mc is. If you nevertheless wanna work with the mc directly you can get it through the getMC() method.

    2. Sorry, but don’t know what special errors are. Error-Handling is kind of new to me.

    3. I wanted to define a few methods which bodies are identical for all visibleObjects. And well, function bodies aren’t permitted in interfaces.

    Hope I got all your points right.

  3. kaneda

    So. @flashforum.de i’ve posted a new Library where i worked on something like this.

    Responses:

    * I created the mc similar like you would attach it
    ….new VisibleElement(_parent, “name”, depth);

    * I recognized that Interfaces are not practiseable with MC’s … too bad that “setter/getter” are not allowed in interfacese :D
    * Even if you use no Interface: instanceof should return if the “type”(as u used it) is correct

    * I used all the Functions, that are available within a “usual” MovieClip, so it works similar to former “registerClass”, you only don’t have to add a Movie to the Stage.

  4. Simon Wacker

    I saw the post already. But didn’t watched at the code yet. I’m going to do that tomorrow. I’ll post some response in the flashforum. So see you there!