我通过向Flex中的Canvas添加MovieClip进行了测试。
private function onInit():void {
var mc:MovieClip = new MovieClip();
mc.graphics.clear();
mc.graphics.lineStyle(2, 0, 1);
mc.graphics.beginFill(0xFF0000, 0.5);
mc.graphics.drawRect(0, 0, 400, 300);
mc.graphics.endFill();
mainCanvas.rawChildren.addChild(mc);
}
private function onMouseMove():void {
var child:DisplayObject = mainCanvas.rawChildren.getChildAt(i);
trace("MovieClip size:", (child as MovieClip).width, (child as MovieClip).height);
}
mainCanvas在MXML中声明并实例化:
<Canvas id="mainCanvas" width="400" height="300" />
第一次尝试正常,当我将鼠标移到舞台上时,控制台会显示以下输出:
MovieClip size: 402 302
但是当我想明确设置我不再看到的影片剪辑的大小时,ouptut显示错误的大小。
这是更新init方法:
private function onInit():void {
var mc:MovieClip = new MovieClip();
mc.width = 400;
mc.height = 300;
mc.graphics.clear();
mc.graphics.lineStyle(2, 0, 1);
mc.graphics.beginFill(0xFF0000, 0.5);
mc.graphics.drawRect(0, 0, 400, 300);
mc.graphics.endFill();
mainCanvas.rawChildren.addChild(mc);
}
和相应的输出:
MovieClip size: 0 0
有人对这种奇怪的行为有任何解释吗?
答案 0 :(得分:2)
最初,您设置空MovieClip
的大小:
var mc:MovieClip = new MovieClip();
mc.width = 400;
mc.height = 300;
您正在尝试调整没有内容的剪辑。
剪辑中有内容后设置尺寸:
var mc:MovieClip = new MovieClip();
mc.graphics.lineStyle(2, 0, 1);
mc.graphics.beginFill(0xFF0000, 0.5);
mc.graphics.drawRect(0, 0, 400, 300);
mc.graphics.endFill();
mc.width = 400;
mc.height = 300;