我想添加一個偵聽器到從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
}
嘗試parentDocument – 2011-04-09 10:24:31
它不被識別,因爲它可能不在舞臺上這就是爲什麼我需要引用INSTANCE CREATOR。 – user310291 2011-04-09 10:30:00