views:

437

answers:

1

Hi everyone, I just wrote a Background class for a new project. The background is basically going to be 2 rounded rectangles with a simple stroke and fill color laid on top of each other.

Anyways right now I'm just trying to draw out 1 rounded rectangle, but for some reason I don't see it anywhere on stage :( No errors and my traces trace out correctly. This is the example I've been following. I've also included the code from my Document class which draws the Background.

Background.as

package src.display{

import flash.display.DisplayObject;
import flash.display.Graphics;
import flash.display.JointStyle;
import flash.display.LineScaleMode;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;

public class Background extends Sprite {

    private const position:Number = 0;

    private var child:Shape       = new Shape();
    private var bgColor:uint      = 0xE8E7E7;
    private var borderColor:uint  = 0xCCCCCC;
    private var borderSize:uint   = 1;
    private var cornerRadius:uint = 3;
    private var sizeW:uint;
    private var sizeH:uint;

    public function Background():void {
        if (stage) {
            init();
        } else {
            addEventListener(Event.ADDED_TO_STAGE, init);
        }
    }

    private function init(e:Event = null):void {
        removeEventListener(Event.ADDED_TO_STAGE, init);
    }

    public function draw(w, h):void {
        sizeW=w; //w & h are passed from Document class
        sizeH=h;

        child.graphics.beginFill(bgColor);
        child.graphics.lineStyle(borderSize, borderColor);
        child.graphics.drawRoundRect(position, position, sizeW-1, sizeH-1, cornerRadius);
        child.graphics.endFill();

        addChild(child);

        trace("child.x = "+child.x);
        trace("child.x = "+child.y);
        trace("child.w = "+child.width);
        trace("child.h = "+child.height);

    }
}

}

My traces all come out correctly:

child.x = 0
child.x = 0
child.w = 520
child.h = 510

Here is the code from my Document class which inits the Background class:

private function drawBackground():void
    {
        trace("\r"+"drawBackground called");

        bg = new Background();
        bg.draw(globalWidth, globalHeight);

        trace("drawBackground end");
    }
+2  A: 

You have to add your bg somewhere into the flash display list, for example into the stage:

bg = new Background();
bg.draw(globalWidth, globalHeight);
stage.addChild(bg);

One another note you don't need to add the child each time you call the draw function, add it into the init function:

private function init(e:Event = null):void {
 removeEventListener(Event.ADDED_TO_STAGE, init);
 addChild(child);
}
Patrick
Ah sweet thanks! :D /facepalm on that first line, can't believe I forgot that.. and cool on moving the addChild :)
Leon
You re welcome :)
Patrick