A small handy snippet of code, throw an event just one time and automatically remove it.

classic Classic list List threaded Threaded
5 messages Options
alx
Reply | Threaded
Open this post in threaded view
|

A small handy snippet of code, throw an event just one time and automatically remove it.

alx
Hi, just sharing a very particular problem that got today and how I solved it.

Some background:

I wanted to add a "play" button graphic to a video, so that the user knows that clicking there will start the video.

var playButton:Sprite; // A play button for my example.
var video:Sprite; // <-- This is the video, it has a CLICK listener to start playing it.

video.addChild(playButton); // Here I add the button to the video.

And now I want to remove the button from the video when it's clicked, but to remove also the event listener since I don't need it anymore.

Here's the code that I could work out:

var l:Dynamic = {};
var _F:Dynamic = function(video:Sprite, playButton:Sprite, l:Dynamic) {
    return function(e:MouseEvent) {
        video.removeChild(playButton);
        video.removeEventListener(MouseEvent.CLICK, l.ref);
    }
}
l.ref = _F(video, playButton, l);
o.addEventListener(MouseEvent.CLICK, l.ref);

Ok, now, if anyone knows of a better solution I'd be glad to know it. Here's some reasons why it so "complicated":

I wanted to define everything on the same function, of course you can define _CLICK_LISTENER somewhere else and it would be easier but I didn't wanted, I'm just acquainted to type everything "on-the-fly".

I'm using a function "factory" _F, to inject local variables into the closure. And I'm using l.ref to explicity pass a reference there ;D.

Mmm, let me know what you think, best!

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: A small handy snippet of code, throw an event just one time and automatically remove it.

Heinz Hölzer-2
With the latest svn version of haxe you can write this:

---------------------
....

// this is a local named function definition which allows referencing
the function from the body.
function f (_)  {
       video.removeChild(playButton);
       video.removeEventListener(MouseEvent.CLICK, f);
}

video.addEventListener(MouseEvent.CLICK, f);

....
---------------------

Am 19.09.2011 05:16, schrieb alx:
> var l:Dynamic = {};
> var _F:Dynamic = function(video:Sprite, playButton:Sprite, l:Dynamic) {
>     return function(e:MouseEvent) {
>         video.removeChild(playButton);
>         video.removeEventListener(MouseEvent.CLICK, l.ref);
>     }
> }
> l.ref = _F(video, playButton, l);
> o.addEventListener(MouseEvent.CLICK, l.ref);


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: A small handy snippet of code, throw an event just one time and automatically remove it.

Heinz Hölzer-2
In reply to this post by alx
and with other versions:

....
// initialize f before the function definition, so it can be referenced.
var f = null;
f  = function(_)  {
      video.removeChild(playButton);
      video.removeEventListener(MouseEvent.CLICK, f);
}

video.addEventListener(MouseEvent.CLICK, f);

....

Am 19.09.2011 05:16, schrieb alx:
Hi, just sharing a very particular problem that got today and how I solved it.

Some background:

I wanted to add a "play" button graphic to a video, so that the user knows that clicking there will start the video.

var playButton:Sprite; // A play button for my example.
var video:Sprite; // <-- This is the video, it has a CLICK listener to start playing it.

video.addChild(playButton); // Here I add the button to the video.

And now I want to remove the button from the video when it's clicked, but to remove also the event listener since I don't need it anymore.

Here's the code that I could work out:

var l:Dynamic = {};
var _F:Dynamic = function(video:Sprite, playButton:Sprite, l:Dynamic) {
    return function(e:MouseEvent) {
        video.removeChild(playButton);
        video.removeEventListener(MouseEvent.CLICK, l.ref);
    }
}
l.ref = _F(video, playButton, l);
o.addEventListener(MouseEvent.CLICK, l.ref);

Ok, now, if anyone knows of a better solution I'd be glad to know it. Here's some reasons why it so "complicated":

I wanted to define everything on the same function, of course you can define _CLICK_LISTENER somewhere else and it would be easier but I didn't wanted, I'm just acquainted to type everything "on-the-fly".

I'm using a function "factory" _F, to inject local variables into the closure. And I'm using l.ref to explicity pass a reference there ;D.

Mmm, let me know what you think, best!




--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: A small handy snippet of code, throw an event just one time and automatically remove it.

T-Lemon
and there's a snippet here :

http://haxe.org/doc/snip/_flash_only_once_eventlistener

2011/9/19 Heinz Hölzer <[hidden email]>
and with other versions:

....
// initialize f before the function definition, so it can be referenced.
var f = null;
f  = function(_)  {
      video.removeChild(playButton);
      video.removeEventListener(MouseEvent.CLICK, f);
}

video.addEventListener(MouseEvent.CLICK, f);

....

Am 19.09.2011 05:16, schrieb alx:
Hi, just sharing a very particular problem that got today and how I solved it.

Some background:

I wanted to add a "play" button graphic to a video, so that the user knows that clicking there will start the video.

var playButton:Sprite; // A play button for my example.
var video:Sprite; // <-- This is the video, it has a CLICK listener to start playing it.

video.addChild(playButton); // Here I add the button to the video.

And now I want to remove the button from the video when it's clicked, but to remove also the event listener since I don't need it anymore.

Here's the code that I could work out:

var l:Dynamic = {};
var _F:Dynamic = function(video:Sprite, playButton:Sprite, l:Dynamic) {
    return function(e:MouseEvent) {
        video.removeChild(playButton);
        video.removeEventListener(MouseEvent.CLICK, l.ref);
    }
}
l.ref = _F(video, playButton, l);
o.addEventListener(MouseEvent.CLICK, l.ref);

Ok, now, if anyone knows of a better solution I'd be glad to know it. Here's some reasons why it so "complicated":

I wanted to define everything on the same function, of course you can define _CLICK_LISTENER somewhere else and it would be easier but I didn't wanted, I'm just acquainted to type everything "on-the-fly".

I'm using a function "factory" _F, to inject local variables into the closure. And I'm using l.ref to explicity pass a reference there ;D.

Mmm, let me know what you think, best!




--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
alx
Reply | Threaded
Open this post in threaded view
|

Re: A small handy snippet of code, throw an event just one time and automatically remove it.

alx
"// this is a local named function definition which allows referencing the function from the body."

Whoa, I've been waiting for that, that's cool :D

On Mon, Sep 19, 2011 at 7:36 AM, T-Lemon <[hidden email]> wrote:
and there's a snippet here :

http://haxe.org/doc/snip/_flash_only_once_eventlistener

2011/9/19 Heinz Hölzer <[hidden email]>
and with other versions:

....
// initialize f before the function definition, so it can be referenced.
var f = null;
f  = function(_)  {
      video.removeChild(playButton);
      video.removeEventListener(MouseEvent.CLICK, f);
}

video.addEventListener(MouseEvent.CLICK, f);

....

Am 19.09.2011 05:16, schrieb alx:
Hi, just sharing a very particular problem that got today and how I solved it.

Some background:

I wanted to add a "play" button graphic to a video, so that the user knows that clicking there will start the video.

var playButton:Sprite; // A play button for my example.
var video:Sprite; // <-- This is the video, it has a CLICK listener to start playing it.

video.addChild(playButton); // Here I add the button to the video.

And now I want to remove the button from the video when it's clicked, but to remove also the event listener since I don't need it anymore.

Here's the code that I could work out:

var l:Dynamic = {};
var _F:Dynamic = function(video:Sprite, playButton:Sprite, l:Dynamic) {
    return function(e:MouseEvent) {
        video.removeChild(playButton);
        video.removeEventListener(MouseEvent.CLICK, l.ref);
    }
}
l.ref = _F(video, playButton, l);
o.addEventListener(MouseEvent.CLICK, l.ref);

Ok, now, if anyone knows of a better solution I'd be glad to know it. Here's some reasons why it so "complicated":

I wanted to define everything on the same function, of course you can define _CLICK_LISTENER somewhere else and it would be easier but I didn't wanted, I'm just acquainted to type everything "on-the-fly".

I'm using a function "factory" _F, to inject local variables into the closure. And I'm using l.ref to explicity pass a reference there ;D.

Mmm, let me know what you think, best!




--

haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org