我刚刚为一个新项目编写了一个背景类。背景基本上是两个圆角矩形,简单的笔触和填充颜色相互叠加。
无论如何,现在我只是想画出 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");
}