【问题标题】:Is it impossible with Flash to get the Instance Creator?用 Flash 是不是无法获取 Instance Creator?
【发布时间】:2011-04-09 10:12:19
【问题描述】:

我想向滑块的父级添加一个侦听器,该滑块从非 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

}

【问题讨论】:

  • 它也没有被识别,因为它可能还没有出现在舞台上这就是为什么我需要引用 INSTANCE CREATOR。

标签: flash actionscript-3


【解决方案1】:

构造函数的声明意味着sliderIEventDispatcher,仅此而已。此接口上不存在 parent 属性。您必须在构造函数中指定另一种类型(例如DisplayObject)。

【讨论】:

  • 另外,如果显示对象不在显示列表中,则父属性为空。
  • 没错,但在这种情况下,滑块已作为子项添加到Sprite
  • 我不能通过将滑块投射到 DisplayObject stackoverflow.com/questions/5604761/… 来做到这一点
  • 当您创建 EventDispatcherManager 时,您的 SliderSprite 不会添加到任何内容中。您应该只在 onAddedToStage 侦听器上调用它的 init。在巴甫洛夫在这里问之前自己检查几件事;)
  • parent 不返回“创建者”,而是返回父元素,即调用addChild 的元素。如果您在将元素添加到任何内容之前尝试读取 parent,它将失败。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-30
  • 2021-02-15
  • 1970-01-01
  • 2022-01-01
  • 2018-09-28
  • 1970-01-01
相关资源
最近更新 更多