Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
480 views
in Technique[技术] by (71.8m points)

actionscript 3 - Is it impossible with Flash to get the Instance Creator?

I want to add a listener to the parent of a slider which is receiving the event from a non-gui class the EventDispatcherManager. So I tried to get the parent of the slider which should return the main class but it doesn't work. How to get the object (here main class) which instantiates an other (here a slider class) ?

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

}

Update: now I did cast to DisplayObject and use .parent.parent since the slider is within another class sliderSprite but now I get null! So Is it impossible with Flash to get the 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

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The declaration of your constructor implies slider is an IEventDispatcher and nothing else. The property parent doesn't exist on this interface. You have to specify another type in the constructor (such as DisplayObject).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...