我的自定义事件没有跟踪输出原因?
具有value属性的CustomEvent类:
package {
import flash.events.Event;
public class CustomEvent extends Event
{
public static const ON_CUSTOM_EVENT:String = "onCustomEvent";
public var value:Number;
public function CustomEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false):void
{
super(type, bubbles, cancelable);
}
}
}
带有滑块的测试类,只需重新调度滑块事件:
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_CUSTOM_EVENT,OnCustomEvent);
slider.addEventListener(SliderEvent.CHANGE,OnSliderEventChange);
}
public function OnCustomEvent(event:CustomEvent): void {
trace(event.value);
}
public function OnSliderEventChange(event:SliderEvent) {
cEvent = new CustomEvent("OnCustomEvent");
cEvent.value = event.value;
dispatchEvent(cEvent);
trace("hello");
}
}
}
答案 0 :(得分:3)
事件对象初始化为“OnCustomEvent”,但静态常量ON_CUSTOM_EVENT为“OnCustomChange”
我建议在两个地方使用静态常量,以确保它是相同的。
答案 1 :(得分:2)
ON_CUSTOM_EVENT:String =“ onCustomEvent ”
与
cEvent = new CustomEvent(“ OnCustomEvent ”);
(案件问题)
您不应该第二次写字符串,而是使用CustomEvent.ON_CUSTOM_EVENT
答案 2 :(得分:1)
我喜欢使用*通配符arg类型,如下所示:
package {
import flash.events.Event;
public class CustomEvent extends Event
{
public static const ON_CUSTOM_EVENT:String = "onCustomChange";
public var args:*;
public function CustomEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false, ...a:*):void
{
super(type, bubbles, cancelable);
arg = a;
}
override public function clone():Event //dont forget your clone override in custom events
{
return new CustomEvent(type, bubbles, cancelable, arg);
}
}
}
然后允许传递任何类型的多个参数。只需确保在呼叫者端强制执行类型。
public function OnCustomEvent(event:CustomEvent): void {
trace(event.arg[0] as Number);
}
public function OnSliderEventChange(event:SliderEvent)
{
// here, we use the CustomEvents own static const as the event
//and pass the event.value as a final parameter, in one statement.
this.dispatchEvent = new CustomEvent(CustomEvent.ON_CUSTOM_EVENT, false, false, event.value);
}
希望有所帮助
答案 3 :(得分:0)
以下是如何使用自定义事件的示例(您可以将所有代码复制并粘贴到文档类中):
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);
_sliderSprite.addEventListener(CustomEvent.CUSTOM_EVENT_TYPE, onSliderSpriteCustomEventType);
}// end function
private function onSliderSpriteCustomEventType(e:CustomEvent):void
{
trace(e.value);
}// 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);
}// 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