2011-04-09 28 views
0

我想添加一個偵聽器到從non-gui類EventDispatcherManager接收事件的滑塊的父級。所以我試圖得到應該返回主類的滑塊的父類,但它不起作用。如何獲得實例化另一個對象(這裏是一個滑塊類)的對象(這裏是主類)?使用Flash無法獲取實例創建器嗎?

package { 

import fl.controls.Slider; 
import fl.events.SliderEvent; 
import flash.events.*; 

internal class EventDispatcherManager extends EventDispatcher 
{ 
    public function EventDispatcherManager(slider:IEventDispatcher) 
    { 
     slider.addEventListener(SliderEvent.CHANGE, onSliderChange); 

     // 1119: Access of possibly undefined property parent through a reference with static type flash.events:IEventDispatcher. 
     slider.parent.addEventListener(CustomEvent.CUSTOM_EVENT_TYPE, onCustomEventType); 

     this.addEventListener(CustomEvent.CUSTOM_EVENT_TYPE, onCustomEventType); 

    }// end function 

    private function onSliderChange(e:SliderEvent):void 
    { 
     this.dispatchEvent(new CustomEvent(CustomEvent.CUSTOM_EVENT_TYPE, e.value)); 

    }// end function 

    private function onCustomEventType(e:CustomEvent):void 
    { 
     trace(e.value); 

    }// end function 

}// end function 

} 


package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 

    public class main extends Sprite 
    { 
     private var _sliderSprite:SliderSprite; 
     private var _eventDispatcherManager:EventDispatcherManager; 

     public function main():void 
     { 
      if (stage) init(); 
      else addEventListener(Event.ADDED_TO_STAGE, init); 

     }// end function 

     private function init(e:Event = null):void 
     { 
      removeEventListener(Event.ADDED_TO_STAGE, init); 

      _sliderSprite = new SliderSprite(); 
      _sliderSprite.x = (stage.stageWidth/2); 
      _sliderSprite.y = (stage.stageHeight/2); 
      addChild(_sliderSprite); 

     }// end function 

     private function onCustomEventType(e:CustomEvent):void 
     { 
      trace("hello"); 

     }// end function 

    }// end class 


}// end package 


package { 

    import flash.display.Sprite; 
    import flash.events.IEventDispatcher; 
    import fl.controls.Slider; 

    public class SliderSprite extends Sprite 
    { 
     private var _slider:Slider; 
     private var _eventDispatcherManager:EventDispatcherManager; 

     public function SliderSprite() 
     { 
      init(); 

     }// end function 

     private function init():void 
     { 
      _slider = new Slider(); 
      addChild(_slider); 

      _eventDispatcherManager = new EventDispatcherManager(IEventDispatcher(_slider)); 

     }// end function 

    }// end class 

} 


package { 

import flash.events.Event; 

internal class CustomEvent extends Event { 

    public static const CUSTOM_EVENT_TYPE:String = "customEventType"; 

    private var _value:Number; 

    public function get value():Number 
    { 
     return _value; 

    }// end function 

    public function CustomEvent(type:String, 
           value:Number, 
           bubbles:Boolean = false, 
           cancelable:Boolean = false) 
    { 
     _value = value; 

     super(type, bubbles, cancelable); 

    }// end function 

    override public function clone():Event 
    { 
     return new CustomEvent(type, value, bubbles, cancelable); 

    }// end function 

}// end class 

} 

更新:現在我也投來的DisplayObject和使用.parent.parent由於滑塊是另一個類sliderSprite內,但現在我得到空!那麼用Flash來獲取Instance Creator是不可能的?

package { 

import flash.display.*; 
import fl.controls.Slider; 
import fl.events.SliderEvent; 
import flash.events.*; 

internal class EventDispatcherManager extends EventDispatcher 
{ 
    public function EventDispatcherManager(slider:IEventDispatcher) 
    { 
     slider.addEventListener(SliderEvent.CHANGE, onSliderChange); 

     // 1119: Access of possibly undefined property parent through a reference with static type flash.events:IEventDispatcher. 
     (slider as DisplayObject).parent.addEventListener(CustomEvent.CUSTOM_EVENT_TYPE, onCustomEventType); 
     trace((slider as DisplayObject).parent.parent); 
     this.addEventListener(CustomEvent.CUSTOM_EVENT_TYPE, onCustomEventType); 

    }// end function 

    private function onSliderChange(e:SliderEvent):void 
    { 
     this.dispatchEvent(new CustomEvent(CustomEvent.CUSTOM_EVENT_TYPE, e.value)); 

    }// end function 

    private function onCustomEventType(e:CustomEvent):void 
    { 
     trace(e.value); 

    }// end function 

}// end function 

} 
+0

嘗試parentDocument – 2011-04-09 10:24:31

+0

它不被識別,因爲它可能不在舞臺上這就是爲什麼我需要引用INSTANCE CREATOR。 – user310291 2011-04-09 10:30:00

回答

2

你的構造函數的聲明意味着sliderIEventDispatcher而已。此接口上不存在此屬性parent。您必須在構造函數中指定另一個類型(例如DisplayObject)。

+0

另外,如果顯示對象不在顯示列表中,則父屬性爲空。 – OXMO456 2011-04-09 11:04:02

+0

這是正確的,但在這種情況下,滑塊已經作爲一個孩子添加到'Sprite'中。 – Kodiak 2011-04-09 11:13:19

+0

我不能通過將滑塊投射到DisplayObject http://stackoverflow.com/questions/5604761/flash-how-can-i-cast-a-slider-class-to-displayobject-in-flash – user310291 2011-04-09 11:35:08