我有一个自定义事件,在移动滑块时调度但我没有从我创建的继承调度程序类中收到任何事件,而我遵循与My flash custom event doesn't trigger
的解决方案相同的语法活动类:
package {
import flash.events.Event;
public class CustomEvent extends Event
{
public static const ON_DISPATCHER_EVENT = "onDispatcherEvent";
public var value:Number;
public function CustomEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false):void
{
super(type, bubbles, cancelable);
}
}
}
分派器:
package {
import flash.events.EventDispatcher;
import flash.events.Event;
public class CustomEventDispatcher extends EventDispatcher {
private var cEvent: CustomEvent;
public function CustomEventDispatcher() {
}
public function raiseEvent(_value:Number) {
cEvent = new CustomEvent(CustomEvent.ON_DISPATCHER_EVENT);
cEvent.value = _value;
dispatchEvent(cEvent);
}
}
}
测试类:
package {
import flash.display.*;
import flash.net.*;
import flash.events.*;
import fl.events.SliderEvent;
import fl.controls.Slider;
public class TestCustomEvent extends MovieClip {
private var cEvent: CustomEvent;
public function TestCustomEvent() {
addEventListener( Event.ADDED_TO_STAGE, init);
}
public function init( e:Event ):void {
removeEventListener( Event.ADDED_TO_STAGE, init );
this.addEventListener(CustomEvent.ON_DISPATCHER_EVENT,OnDispatcherEvent);
slider.addEventListener(SliderEvent.CHANGE,OnSliderEventChange);
}
public function OnDispatcherEvent(event:CustomEvent): void {
trace(event.value);
}
public function OnSliderEventChange(event:SliderEvent) {
cEvent = new CustomEvent(CustomEvent.ON_DISPATCHER_EVENT);
cEvent.value = event.value;
dispatchEvent(cEvent);
trace("hello");
}
}
}
答案 0 :(得分:4)
我认为您对事件流程的理解有点偏差。我已经在My flash custom event doesn't trigger中给出的答案(我假设您没有阅读)我认为是使用自定义Event
和IEventDispatcher
对象的正确方法:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
private var _sliderSprite:SliderSprite;
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
}// end class
}// end package
import fl.controls.Slider;
import fl.events.SliderEvent;
import flash.display.Sprite;
internal class SliderSprite extends Sprite
{
private var _slider:Slider;
public function SliderSprite()
{
init();
}// end function
private function init():void
{
_slider = new Slider();
addChild(_slider);
_slider.addEventListener(SliderEvent.CHANGE, onSliderChange);
addEventListener(CustomEvent.CUSTOM_EVENT_TYPE, onSliderSpriteCustomEventType);
}// end function
private function onSliderSpriteCustomEventType(e:CustomEvent):void
{
trace(e.value);
}// end function
private function onSliderChange(e:SliderEvent):void
{
dispatchEvent(new CustomEvent(CustomEvent.CUSTOM_EVENT_TYPE, e.value));
}// end function
}// end class
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] 强> [编辑08/04/2011 08:22]
我修改了代码以封装所有涉及调度和侦听事件的代码,以及将事件处理程序排除到另一个类(EventDispatcherManager
)。
import flash.display.Sprite;
import flash.events.IEventDispatcher;
internal 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
import fl.controls.Slider;
import fl.events.SliderEvent;
import flash.events.EventDispatcher;
internal class EventDispatcherManager extends EventDispatcher
{
public function EventDispatcherManager(slider:IEventDispatcher)
{
slider.addEventListener(SliderEvent.CHANGE, onSliderChange);
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)
你好被追踪了吗?
对于哪个调度程序调用{{1}}方法,似乎可能存在模糊的引用。尝试指定disptachEvent()
并使用slider.dispatchEvent()
来监听自定义事件。
答案 2 :(得分:0)
不应该在测试文件中导入CustomEventDispatcher类
答案 3 :(得分:0)
event.value不存在
尝试单独追踪事件,看看你得到了什么
很确定你需要像event.target.value