2012-10-12 124 views
0

我有两个类,VideoPod和RemotePod,RemotePod继承自VideoPod。如果没有显示这些类的所有代码,basicall这里的VideoPod的一部分:为什么将这些对象放入MXML中打破了这个功能?

 public function showPanel():void { 
      if (!pnl.visible) { 
       pnl.visible = true; 
       pnl.addElement(removeElement(vg)); 
      } 
     } 
       . 
       . 
       . 
<s:Panel id="pnl" width="100%" height="100%" fontWeight="normal" visible="false" /> 
<s:VGroup id="vg" left="0" resize="onResize()" right="0" top="0" bottom="0"> 

和这里的RemotePod的一部分:

 private function onCreationComplete():void { 
      m_tmrHeartbeat.addEventListener(TimerEvent.TIMER, checkPulse); 

      var arrBtns:Array = new Array(4); 
      for (var i:int = 0; i < arrBtns.length; i++) { 
       arrBtns[i] = new Button(); 
       arrBtns[i].width = 28; 
       arrBtns[i].height = 25; 
       arrBtns[i].top = 10;//-28; 
      } 

      arrBtns[0].right = 10; 
      arrBtns[0].setStyle("icon", Images.PATH + "view-fullscreen-3.png"); 
      arrBtns[0].addEventListener(MouseEvent.CLICK, maximize); 
       . 
       . 
       . 
      for each (var btn:Button in arrBtns) { 
       addElement(btn); 
      } 

      m_lblSize.right = 154; 
      m_lblSize.top = 18;//-20; 
      m_lblSize.text = FULLSCREEN; 
      addElement(m_lblSize); 

其中onCreationComplete()被调用为RemotePod creationComplete事件。几分钟前,我尝试将RemotePod中的按钮和标签移动到实际的MXML中,但这打破了showPanel()函数。它提出的错误基本上有以下信息:“在本组中找不到vg”。 (VideoPod继承自s:Group。)

我不明白。我也开始测试vg在运行时的宽度,它显然只是停留在0.什么是造成这种情况的晦涩语言功能?谢谢!

+2

我不认为RemotePod可以*有* MXML,除非您已经将VideoPod作为模板组件编写。因此,查看两者的代码以查看这是否甚至可以正常工作非常重要。另外,你不应该在creationComplet中添加子元素,而是在createChildren覆盖中。 –

回答

1

MXML类不会继承其父母的MXML子组件。您应该在您的类构造函数(如.as)或初始化侦听器(如.mxml)中使用纯AS3创建Panel和VGroup。

protected var pnl:Panel; 
protected var vg:VGroup; 

private function onInitialize():void 
{ 
    pnl = new Panel(); 
    //set pnl properties such as width, height... 
    addComponent(pnl); 

    vg = new VGRoup(); 
    //set vg properties such as width, height... 
    addComponent(vg); 
} 

另一种解决方案是为您的基类使用皮肤。

+0

因此,我甚至无法重写一个函数,只需调用super即可访问父级组件。()? – Panzercrisis

+0

如果函数被声明为protected或public,则可以覆盖该函数。 – Kodiak

+0

这就是我想要做的。然而,在那里调用super.showPanel()会引起问题。 – Panzercrisis