2012-09-26 77 views

回答

3

爲什麼不在父母計時器滴答時在每個孩子中調用一個函數?基本上只是讓你的孩子實現創建(姑且稱之爲IHaveTickingParent)具有特定功能的接口,如:

[IHaveTickingParent.as]

public interface IHaveTickingParent 
{ 
    function onParentTick(event:TimerEvent):void; 
} 

[HaveTickingParentImpl.as]

public class HaveTickingParentImpl /* extends XYZ */ implements IHaveTickingParent 
{ 
    public function onParentTick(event:TimerEvent):void 
    { 
    } 
} 

而且在你父母的TimerEvent處理器只是做線沿線的東西:

for(var i:int = 0; i < numChildren; i++) 
{ 
    var child:DisplayObject = getChildAt(i); 

    if(child is IHaveTickingParent) 
    { 
     // Make children update as well. 
     (child as IHaveTickingParent).onParentTick(event); 
    } 
} 
+0

我希望你不介意我只是修改說使用接口,而不是一個子類。要麼是一個選項,但如果有必要,這會讓您打開子類別的其他東西。 – shaunhusain

+0

@shaunhusain乾杯,我更喜歡在這裏使用接口,我只是想避免在這個階段引入可能會混淆OP的概念。 – Marty

相關問題