我有父精灵宽度的问题。我创建了两个精灵:父母和孩子
var parent:Sprite = new Sprite();
var child:Sprite = new Sprite();
addChild(parent);
parent.addChild(child);
child.x = 20;
child.y = 20;
child.graphics.beginFill(0xFF0000);
child.graphics.drawRect(0, 0, 100, 100);
trace("colorBox.graphics.beginFill(0xFF0000);");
trace("colorBox.graphics.drawRect(0, 0, 100, 100);");
trace("parent", parent.width, parent.height, parent.x, parent.y);
trace("child", child.width, child.height, child.x, child.y);
如果我在 Sprite 上添加 Sprite,则 parent.width 为 100,即使 child.x = 20。高度和 y 也是如此。
但是,如果我像这样更改代码:
var parent:Sprite = new Sprite();
var child:Sprite = new Sprite();
parent.graphics.drawRect(0, 0, 0, 0); // the difference
addChild(parent);
parent.addChild(child);
child.x = 20;
child.y = 20;
child.graphics.beginFill(0xFF0000);
child.graphics.drawRect(0, 0, 100, 100);
trace("colorBox.graphics.beginFill(0xFF0000);");
trace("colorBox.graphics.drawRect(0, 0, 100, 100);");
trace("parent", parent.width, parent.height, parent.x, parent.y);
trace("child", child.width, child.height, child.x, child.y);
父宽度变为等于 120。
为什么会这样?如何在没有“parent.graphics.drawRect”黑客的情况下让孩子调整父母的宽度?