我有一个 Player 类,它是一个电影片段, 我试图从中设置它的宽度和高度,然后让它自己添加到舞台上, 但出于某种原因,无论我做什么,this.width 都会返回 0 并且设置大小仅适用于父类。为什么会这样?
public class Player extends MovieClip {
public var head:MovieClip = new Head_normal_front();
public function Player(stage:Stage){
this.addChild(head);
this.width = 10;
this.height = 10;
stage.addChild(this);
}
}
谢谢
最佳答案
尝试如下操作:
public class Player extends MovieClip {
public var head:MovieClip = new Head_normal_front();
public function Player(stage:Stage){
this.addChild(head);
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
stage.addChild(this);
}
private function onAddedToStage(e:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
this.width = 10;
this.height = 10;
}
}
您应该在将显示对象添加到舞台后修改它们的宽度/高度。我可能也会将 stage.addChild(this) 移到 Player 之外。
关于actionscript-3 - 在 AS3 中设置动画片段的宽度/高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15164016/