My flash custom event doesn't trigger
There is no trace output of my custom event why ?
CustomEvent class with value property:
package {
import开发者_StackOverflow社区 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);
}
}
}
Test class with a slider which just re-dispatches the slider event:
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");
}
}
}
The event object is initialized to "OnCustomEvent" but the static constant ON_CUSTOM_EVENT is "OnCustomChange"
I would recommend using the static constant in both places to make sure it's the same.
ON_CUSTOM_EVENT:String = "onCustomEvent"
versus
cEvent = new CustomEvent("OnCustomEvent");
(case problem)
You shouldn't write the string the second time but use CustomEvent.ON_CUSTOM_EVENT
I like to use a * wildcard arg type, like so:
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);
}
}
}
Which then allows for multiple arguments of any type to be passed. Just make sure you enforce type on the callers end.
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);
}
Hope that helps
The following is an example of how to use a custom event(you can copy and paste all the code into your document class):
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
精彩评论