Introduction

You are currently browsing the weblog archives for October, 2003.

Archive for October, 2003

Friday, October 24th, 2003

Datastructures

Inspired by a tutorial about datastructures on javarworld.com from Jeff Friesen (good tutorial, worth reading), I wrote this little piece of code.
It isn’t useful at all because flash already has an Array class. But I think it’s interesting to see how these things, I expected always as normal, really work.

What you need are 4 classes named: ArrayStack, Node, EmptyStackException, FullStackException and 1 interface class named: Stack.

The ArrayStack.as file must be in one folder together with the Node.as file. That folder must contain two other folders named: Exception and SuperClasses.
The Exception folder contains the EmptyStackException.as file and the FullStackException.as file.
The SuperClasses folder contains the Stack.as file.

interface SuperClasses.Stack{
        public function isEmpty():Boolean;
        public function peek():Node;
        public function push(anObject:Node):Void;
        public function pop():Node;
        public function shift():Node;
        public function unshift(aNode:Node):Void;
        public function getNodeByNumber(aNumber:Number):Node
}
class Node{
        public var name:String;
        public var next:Node;
        public var prev:Node;

        public function Node(aName:String){
                name = aName;
        }
}
import SuperClasses.*
import Exceptions.*

class ArrayStack implements Stack{
        private var numbersOfElements:Number = -1;
        private var maxElements:Number = -1;
        private var topForward:Node;
        private var topBackward:Node;

        public function ArrayStack(mElements:Number){
                maxElements = mElements;
        }
        public function isEmpty():Boolean{
                return numbersOfElements < 0;
        }
        public function peek():Node{
                if(numbersOfElements < 0){
                        throw new EmptyStackException();
                }
                return topBackward;
        }
        public function push(aNode:Node):Void{
                if(numbersOfElements >= maxElements){
                        throw new FullStackException();
                }
                if(numbersOfElements == -1){
                        topForward = aNode;
                } else {
                        aNode.next = null;
                        aNode.prev = topBackward;
                        topBackward.next = aNode;
                }
                topBackward = aNode;
                numbersOfElements++;
        }
        public function pop():Node{
                if(numbersOfElements < 0){
                        throw new EmptyStackException();
                }
                numbersOfElements–;
                var temp:Node = topBackward;
                topBackward = topBackward.prev;
                return temp;
        }
        public function unshift(aNode:Node):Void{
                if(numbersOfElements >= maxElements){
                        throw new FullStackException();
                }
                if(numbersOfElements == -1){
                        topBackward = aNode;
                } else {
                        aNode.next = topForward;
                        aNode.prev = null;
                        topForward.prev = aNode;
                }
                topForward = aNode;
                numbersOfElements++;
        }
        public function shift():Node{
                if(numbersOfElements < 0){
                        throw new EmptyStackException();
                }
                numbersOfElements–;
                var temp:Node = topForward;
                topForward = topForward.next;
                return temp;
        }
        public function getNodeByNumber(aNumber:Number):Node{
                if(numbersOfElements < 0){
                        throw new EmptyStackException();
                }
                if(maxElements < aNumber){
                        throw new Error(”The number passed as an argument is too big!“);
                }
                var i:Number = 0;
                var temp:Node = topForward;
                while(i++ < aNumber){
                        temp = temp.next;
                }
                return temp;
        }
}
class Exceptions.EmptyStackException extends Error{
        var message = “The stack is already empty!“;
}
class Exceptions.FullStackException extends Error{
        var message = “The stack is already full!“;
}

// usage:
var arr:ArrayStack = new ArrayStack(3);
arr.push(new Node(”A“));
arr.push(new Node(”B“));
arr.push(new Node(”C“));
arr.push(new Node(”D“));
// arr.push(new Node(”E”));
// This additional function call would end the script because of an error.
// The ArrayStack is full.

trace(”function: getNodeByNumber“);

trace(arr.getNodeByNumber(0).name);
trace(arr.getNodeByNumber(1).name);
trace(arr.getNodeByNumber(2).name);
trace(arr.getNodeByNumber(3).name);
// trace(arr.getNodeByNumber(4).name);
// This additional function call would force the script to stop.
// The number is bigger than the maxElements number.

trace(”——————————“);
trace(”function: pop“);

trace(arr.pop().name);
trace(arr.pop().name);
trace(arr.pop().name);
trace(arr.pop().name);

// trace(arr.pop());
// This additional function call would end the script because of an error.
// The ArrayStack is empty.

arr.unshift(new Node(”A“));
arr.unshift(new Node(”B“));
arr.unshift(new Node(”C“));

trace(”——————————“);
trace(”function: getNodeByNumber“);

trace(arr.getNodeByNumber(0).name);
trace(arr.getNodeByNumber(1).name);
trace(arr.getNodeByNumber(2).name);

trace(”——————————“);
trace(”function: shift“);

trace(arr.shift().name);
trace(arr.shift().name);
trace(arr.shift().name);
/*
output:
function: getNodeByNumber
A
B
C
D
——————————
function: pop
D
C
B
A
——————————
function: getNodeByNumber
C
B
A
——————————
function: shift
C
B
A
*/

That’s it.
btw. the links to the tutorial are:
http://www.javaworld.com/javaworld/jw-05-2003/jw-0502-java101.html?
and
http://www.javaworld.com/javaworld/jw-06-2003/jw-0613-java101.html?

Friday, October 10th, 2003

How to use the VisibleObject Class

We are going to create a simple Rectangle class. Our goal is it to draw a Rectangle on stage just by writing:

var myRect:Rectangle = new Rectangle();
myRect.create();

At first we create a new ActionScript file and save it as Rectangle.as. The VisibleObject class from the previous post must be in the same directory. Open the Rectangle.as file and copy the following code in it.

// create a Rectangle class and let it inherit from the VisibleObject class
class Rectangle extends VisibleObject{
        // define the width and height properties
        private var width:Number;
        private var height:Number;

        public function Rectangle(){
                // set the type of the VisibleObject
                type = “Rectangle“;
                // set the default width and height
                width = 100;
                height = 12;
        }
        // override the create method
        public function create():Void{
                // call the original create method
                super.create();
                // draw the rectangle
                draw();
        }
        public function draw():Void{
                mc.beginFill(0×000000, 100);
                mc.moveTo(0, 0);
                mc.lineTo(0, height);
                mc.lineTo(width, height);
                mc.lineTo(width, 0);
                mc.lineTo(0, 0);
                mc.endFill();
        }
        public function clear():Void{
                mc.clear();
        }

        // declare getters and setters
        public function setWidth(aWidth:Number):Void{
                width = aWidth;
        }
        public function getWidth():Number{
                return width;
        }
        public function setHeight(aHeight:Number):Void{
                height = aHeight;
        }
        public function getHeight():Number{
                return height;
        }
        public function getBoundingBox():Object{
                return {width:width, height:height};
        }
}

That’s it. To test whether it works or not create a new fla and write the following code in it.

var myRect:Rectangle = new Rectangle();
myRect.create(); 

trace(myRect.getMC());
trace(myRect.getWidth());
trace(myRect.getHeight());
trace(myRect.getXPosition());
trace(myRect.getYPosition());

// Output:
// _level0.Rectangle_0_mc
// 100
// 12
// 0
// 0

If you want to see a bigger and more advanced example click here to download the bunch of code.
Click here to see the class hierarchy.
The zip contains all files shown in the class hierarchy as well as a fla that shows how to use all this.

Thursday, October 9th, 2003

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.