Flex / ASの自己破壊EventLisener

たまたまFlexプロジェクトに取り組んでいます...

チャレンジ:
1回限りのEventListenerを作成します。

問題:
どうやって? :)

解決策 (真空中の球体の例):

MXMLパーツ:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init()">
<!-- -->
<mx:Button id="button1" />
</mx:Application>

ASパート:
private function init():void{
var onClick: Function = function ( event :MouseEvent):void{
trace( "event handled" );
button1.removeEventListener(MouseEvent.CLICK, onClick);
}
button1.addEventListener(MouseEvent.CLICK, onClick);
}


* This source code was highlighted with Source Code Highlighter .
private function init():void{
var onClick: Function = function ( event :MouseEvent):void{
trace( "event handled" );
button1.removeEventListener(MouseEvent.CLICK, onClick);
}
button1.addEventListener(MouseEvent.CLICK, onClick);
}


* This source code was highlighted with Source Code Highlighter .
private function init():void{
var onClick: Function = function ( event :MouseEvent):void{
trace( "event handled" );
button1.removeEventListener(MouseEvent.CLICK, onClick);
}
button1.addEventListener(MouseEvent.CLICK, onClick);
}


* This source code was highlighted with Source Code Highlighter .


その結果、ボタンが1回クリックされます。 やった!

Source: https://habr.com/ru/post/J58080/


All Articles