问题描述
我想向滑块的父级添加一个侦听器,该滑块从非 gui 类 EventDispatcherManager 接收事件.所以我试图获取应该返回主类的滑块的父类,但它不起作用.如何获取实例化另一个(此处为滑块类)的对象(此处为主类)?
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
}
更新:现在我确实转换为 DisplayObject 并使用 .parent.parent 因为滑块在另一个类 sliderSprite 中,但现在我得到了空值!那么 Flash 是不是不可能获得 Instance Creator 了?
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
}
推荐答案
你的构造函数的声明意味着 slider
是一个 IEventDispatcher
而不是别的.此接口上不存在属性 parent
.您必须在构造函数中指定另一种类型(例如 DisplayObject
).
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 无法获得 Instance Creator 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!