1

我刚刚为一个新项目编写了一个背景类。背景基本上是两个圆角矩形,简单的笔触和填充颜色相互叠加。

无论如何,现在我只是想画出 1 个圆角矩形,但由于某种原因,我在舞台上的任何地方都看不到它 :( 没有错误,我的痕迹正确追踪。这是我一直在关注的例子。我'还包括了我的文档类中绘制背景的代码。

背景.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);

    }
}

}

我的痕迹都正确出现:

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

这是我的 Document 类中的代码,它初始化了 Background 类:

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

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

        trace("drawBackground end");
    }
4

1 回答 1

3

您必须将您的bg某处添加到 flash 显示列表中,例如添加到舞台中:

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

另请注意,您不需要每次调用 draw 函数时都添加子项,将其添加到 init 函数中:

private function init(e:Event = null):void {
 removeEventListener(Event.ADDED_TO_STAGE, init);
 addChild(child);
}
于 2010-01-07T20:16:59.323 回答