views:

342

answers:

3

Im new to actionscript 3 and am a little confused about how the addchild function works.

I want to draw 5 houses. each house has a roof and wall. Each wall has a door and a window. I have the following classes and this is how I grouped them

class Main
  class House
    class Roof  //a triangle
    class Wall  //a rectangle
      class Door //a rectangle
      class Window //a square

Im having trouble with inheritance of the classes. I cant made the wall class show up with a window and a door. Can someone point me to the right direction?

UPDATE: This is the part I do not understand. How can I write it so that it draws the door and window relative to the wall? (use the top left corner of wall as 0,0; instead of the screen)

+1  A: 

objects are not added automatically to the display list (shown on screen), to do this you use the addChild() or addChildAt() (for specifying a depth).

what you want here is presumably something that looks like this (if you want seperate classes):

class Window/Door/Roof want to just be sprite constructors:

package {
    import flash.display.Sprite;

    public class Window extends Sprite{
        public function Window() {

        }
    }
}

Wall wants to add some children.

package {
    import flash.display.Sprite;

    public class Wall extends Sprite {

        private var door:Door;
        private var window:Window;

        public function Wall() {
            //Instance the objects
            //This creates a reference but they will not render on screen
            door = new Door();
            window = new Window();

            //Position objects here

            //Add them to displaylist
            addChild(door);
            addChild(window);

        }
    }
}

house then wants to add a roof and wall in the same manner. note that because you added the door first in this example, it will appear behind the window if there is any overlap. your Main class will then create and use the addChild() for the 5 House instances.

shortstick
This is the part I do not understand.How can I write it so that it draws the door and window relative to the wall? (use the top left corner of wall as 0,0; instead of the screen)
Amy
Makes no difference, but it may help you visualize this better if you use this.addChild(door) and this.addChild(window)
Sandro
public function House() {addChild(roof); addChild(wall);}Is this right? I get a blank screen now. i think it has to do with the ordering. so confused.
Amy
when you use addChild, then it becomes attached to the clip, therefore wall.addChild(window) window will appear at the 0,0 point of the wall and then use that as its origin point. it is right as long as you have instanced the variables roof and wall before trying to add them to stage. roof = new Roof(); addChild(roof); etc.
shortstick
A: 

it wouldn't be automatic every visual item has an x and y cordinate and all there chilrens x,y 0,0 cordinates start from the parent in other words if you put

the door at:

door.x = 200;
door.y = 100; 

any child you give to it would inherit its position so if you whould just for the example add the window to the door

door.addChild(window);

the window would sit ontop of the door you can still move it around with its x,y coordinates but it is relative to that door because its its child.

Ben Fhala
shouldn't it be window.addChild(door);
Amy
How can you add a door to a window? :)
Sandro
i mean wall.addChild(door) :)
Amy
A: 

I hope this helps

// MAIN FLA
import House;

var house:House = new House();
addChild(house);


package {
    import flash.display.Sprite;
    import Roof;
    import Wall;

    public class House extends Sprite {
        protected var wall:Wall;
        protected var roof:Roof;
        function House() {
            wall = new Wall();
            roof = new Roof();

            this.addChild(roof);
            this.addChild(wall);

            // The wall should be below the roof
            wall.y = roof.height + roof.y;

        }
    }
}

package {
    import flash.display.Sprite;

    public class Roof extends Sprite {
        function Roof() {

        }
    }
}

package {
    import flash.display.Sprite;
    import Door;

    public class Wall extends Sprite {
        protected var door :Door;
        function Wall() {
            door = new Door(true);
            this.addChild(door);

            // Door is centered.
            door.x = (this.width - door.width)/2;

            // Door is flush with bottom of the wall.
            door.y = this.height - door.height;
        }
    }
}

package {
    import flash.display.Sprite;
    import Windoe;

    public class Door extends Sprite {
        protected var win :Windoe;
        function Door(hasWindow:Boolean = false) {
            if(hasWindow) {
                win = new Windoe();
                this.addChild(win);

                // The window should be centered?
                win.x = (this.width - win.width)/2;
                win.y = 20; // Just a guess.
            }
        }
    }
}

package {
    import flash.display.Sprite;

    // The word Window may be reserved, so using Windoe just to be safe
    public class Windoe extends Sprite {
        function Windoe() {

        }
    }
}
Sandro