我刚刚为一个新项目编写了一个Background类。背景基本上是2个圆角矩形,简单的笔触和填充颜色相互叠加。
无论如何,我现在只是想画出一个圆角矩形,但出于某种原因我在舞台上看不到任何地方:(没有错误,我的痕迹正确地追踪出来。This is the example我已经一直在关注。我还包含了我的Document类中的代码,该代码绘制了背景。
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");
}
答案 0 :(得分:3)
你必须将你的bg
添加到flash显示列表中,例如进入舞台:
bg = new Background();
bg.draw(globalWidth, globalHeight);
stage.addChild(bg);
另外请注意,每次调用绘图函数时都不需要添加子项,将其添加到init函数中:
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
addChild(child);
}