Flash无法获得Instance Creator吗?

时间:2011-04-09 10:12:19

标签: flash actionscript-3

我想向一个滑块的父级添加一个侦听器,该滑块从非gui类EventDispatcherManager接收事件。所以我试图得到滑块的父级,它应该返回主类,但它不起作用。如何获取实例化另一个对象(这里是一个滑块类)的对象(这里是主类)?

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中,但现在我得到null!那么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

}

1 个答案:

答案 0 :(得分:2)

构造函数的声明暗示sliderIEventDispatcher而不是其他内容。此接口上不存在属性parent。您必须在构造函数中指定另一种类型(例如DisplayObject)。