它是用Flash来获得实例创建不可能的? [英] Is it impossible with Flash to get the Instance Creator?

查看:130
本文介绍了它是用Flash来获得实例创建不可能的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲一个侦听器添加到一个滑动件,其从非贵类EventDispatcherManager接收事件的父。所以,我试图让这应该返回主类滑块的父,但它不工作。如何获取对象(这里是主类)的实例化等(这里滑块类)?

 包{

进口fl.controls.Slider;
进口fl.events.SliderEvent;
进口flash.events *。

内部类EventDispatcherManager扩展EventDispatcher
{
    公共职能EventDispatcherManager(滑块:IEventDispatcher,请)
    {
        slider.addEventListener(SliderEvent.CHANGE,onSliderChange);

        // 1119:可能未定义的属性家长通过静态类型flash.events:IEventDispatcher参考访问。
        slider.parent.addEventListener(CustomEvent.CUSTOM_EVENT_TYPE,onCustomEventType);

        this.addEventListener(CustomEvent.CUSTOM_EVENT_TYPE,onCustomEventType);

    } //函数结束

    私有函数onSliderChange(E:的SliderEvent):无效
    {
        this.dispatchEvent(新自定义事件(CustomEvent.CUSTOM_EVENT_TYPE,e.value));

    } //函数结束

    私有函数onCustomEventType(五:自定义事件):无效
    {
        跟踪(e.value);

    } //函数结束

} //函数结束

}


包
{
    进口flash.display.Sprite;
    进口对象类型:flash.events.Event;

    公共类主要的扩展Sprite
    {
        私人VAR _sliderSprite:SliderSprite;
        私人VAR _eventDispatcherManager:EventDispatcherManager;

        公共函数main():无效
        {
            如果(阶段)的init();
            其他的addEventListener(Event.ADDED_TO_STAGE,INIT);

        } //函数结束

        私有函数初始化(E:事件= NULL):无效
        {
            removeEventListener(Event.ADDED_TO_STAGE,INIT);

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

        } //函数结束

        私有函数onCustomEventType(五:自定义事件):无效
        {
            跟踪(你好);

        } //函数结束

    } //结束类


} //结束包


包 {

    进口flash.display.Sprite;
    进口flash.events.IEventDispatcher;
    进口fl.controls.Slider;

    公共类SliderSprite扩展Sprite
    {
        私人VAR _slider:滑块;
        私人VAR _eventDispatcherManager:EventDispatcherManager;

        公共职能SliderSprite()
        {
            在里面();

        } //函数结束

        私有函数的init():无效
        {
            _slider =新的Slider();
            的addChild(_slider);

            _eventDispatcherManager =新EventDispatcherManager(IEventDispatcher,请(_slider));

        } //函数结束

    } //结束类

}


包  {

进口对象类型:flash.events.Event;

内部类自定义事件扩展事件{

    公共静态常量CUSTOM_EVENT_TYPE:字符串=customEventType;

    私人VAR _value:数字;

    公共职能的get()值:数字
    {
        返回_value;

    } //函数结束

    公共功能自定义事件(类型:字符串,
                                值:号码,
                                泡沫:布尔=假,
                                取消:布尔= FALSE)
    {
        _value =值;

        超(类型,泡沫,取消);

    } //函数结束

    重写公共职能的clone():事件
    {
        返回新的自定义事件(类型,值,泡沫,取消);

    } //函数结束

} //结束类

}
 

更新:现在我也投来的DisplayObject并使用.parent.parent由于滑块是在另一个类sliderSprite但现在我得到空!因此,它是用Flash不可能得到的实例创建?

 包{

导入flash.display使用*。
进口fl.controls.Slider;
进口fl.events.SliderEvent;
进口flash.events *。

内部类EventDispatcherManager扩展EventDispatcher
{
    公共职能EventDispatcherManager(滑块:IEventDispatcher,请)
    {
        slider.addEventListener(SliderEvent.CHANGE,onSliderChange);

        // 1119:可能未定义的属性家长通过静态类型flash.events:IEventDispatcher参考访问。
        (滑动器的DisplayObject).parent.addEventListener(CustomEvent.CUSTOM_EVENT_TYPE,onCustomEventType);
        跟踪((滑动器的DisplayObject).parent.parent);
        this.addEventListener(CustomEvent.CUSTOM_EVENT_TYPE,onCustomEventType);

    } //函数结束

    私有函数onSliderChange(E:的SliderEvent):无效
    {
        this.dispatchEvent(新自定义事件(CustomEvent.CUSTOM_EVENT_TYPE,e.value));

    } //函数结束

    私有函数onCustomEventType(五:自定义事件):无效
    {
        跟踪(e.value);

    } //函数结束

} //函数结束

}
 

解决方案

你的构造函数的声明意味着滑盖 IEventDispatcher,请,别无其他。该酒店并没有在该接口上存在。你必须指定其他类型的构造函数(如的DisplayObject )。

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

}

解决方案

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).

这篇关于它是用Flash来获得实例创建不可能的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆