2011-04-07 58 views
0

還有就是我的自定義事件爲什麼沒有跟蹤輸出?我的Flash自定義事件不會觸發

自定義事件類值屬性:與剛剛滑塊

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); 
     } 

    } 

} 

Test類重新調度滑塊事件:

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

這將是更容易與調度程序的代碼來診斷。 'OnSliderEventChange'觸發了嗎? – Kodiak 2011-04-07 14:22:15

+0

在代碼窗口中向下滾動。起初我也沒有注意到。 – jhocking 2011-04-07 14:50:06

+0

好的,那麼:是否觸發了「OnSliderEventChange」? – Kodiak 2011-04-07 15:48:38

回答

2

ON_CUSTOM_EVENT:字符串= 「onCustomEvent

CEvent的新=自定義事件( 「OnCustomEvent」);

(事例問題)

你不應該寫入字符串第二次,但使用CustomEvent.ON_CUSTOM_EVENT

+0

哇謝謝它的作品,雖然我沒有看到與字符串差異! – user310291 2011-04-07 17:37:42

+0

奇怪我做了EventDispatcher類相同,但它不工作http://stackoverflow.com/questions/5586830/doesnt-receive-event-from-eventdispatcher-class-in-flash – user310291 2011-04-07 19:57:57

+0

的區別是「O」在字符串的開頭。一個是大寫字母,一個是小寫字母。那個字符串就是事件的標識符..所以你派遣你說「任何正在監聽這個確切的字符串,運行該函數」。如果字符串不準確,包括大寫/小寫,那麼它認爲它們是兩個不同的東西。 – WORMSS 2013-02-08 09:24:15

3

事件對象被初始化爲「OnCustomEvent」但靜態恆ON_CUSTOM_EVENT是「OnCustomChange」

我會建議使用在這兩個地方的靜態常量,以確保它是相同的。

+0

孩子 - 真的很有趣的工作。 – Bosworth99 2011-04-07 14:39:34

+0

哦謝謝。 fyi其他人,他在談論我的個人資料 – jhocking 2011-04-07 14:48:47

+0

網站好的謝謝,但不工作。事件對象被創建但調度不起作用; – user310291 2011-04-07 15:47:31

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); 
      } 

希望幫助

+0

謝謝,但這是如何解釋我的代碼不起作用? – user310291 2011-04-07 15:35:38

+0

這是如何設置和使用靈活的自定義事件。如果你已經實現了它,並且你的事件仍然沒有觸發,那麼看看你的OnSliderEventChange。你說它的解僱,你是在追查它還是在你的代碼中無法解釋的東西? – Bosworth99 2011-04-07 17:16:54

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