# Disabling Event listener for child sprite

**URL:** https://forum.kirupa.com/t/disabling-event-listener-for-child-sprite/265792
**Category:** flash
**Created:** [July 12, 2008, 3:42pm UTC](https://forum.kirupa.com/t/disabling-event-listener-for-child-sprite/265792 "2008-07-12T15:42:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![adnan794](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@adnan794](https://forum.kirupa.com/u/adnan794)
#### Post date: [July 12, 2008, 3:42pm UTC](https://forum.kirupa.com/t/disabling-event-listener-for-child-sprite/265792/1 "2008-07-12T15:42:00Z")

</div>

Hello I have a main sprite that contains contains 5 buttons sprites.

I have registered a event listener with the main sprite

mainSprite.addEventListener(MouseEvent.CLICK, on Click);

function onClick(e:MouseEvent):void  
{  
trace(e.target.name);  
}

I want to prevent the button that is clicked from receiving CLICK event again how can I do that?

Thanks in advance

---

<div class="post-metadata">

### Author: ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)
#### Post date: [July 12, 2008, 3:43pm UTC](https://forum.kirupa.com/t/disabling-event-listener-for-child-sprite/265792/2 "2008-07-12T15:43:02Z")

</div>

mainSprite.mouseChildren = false;

---

<div class="post-metadata">

### Author: ![adnan794](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@adnan794](https://forum.kirupa.com/u/adnan794)
#### Post date: [July 12, 2008, 4:13pm UTC](https://forum.kirupa.com/t/disabling-event-listener-for-child-sprite/265792/3 "2008-07-12T16:13:53Z")

</div>

Hi

Thanks for the reply.

But this will disable the all the children I just want to disable the one that is being clicked. I hope I am making sense.

By the way I checked your website very nice work.

---

<div class="post-metadata">

### Author: ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)
#### Post date: [July 12, 2008, 4:21pm UTC](https://forum.kirupa.com/t/disabling-event-listener-for-child-sprite/265792/4 "2008-07-12T16:21:36Z")

</div>

You could either store its name in an array (target.name) and do a conditional when it’s clicked again, or you could just setup individual listeners for each button.

The latter sounds better to me 🙂

And thanks, btw, appreciate the kind words.

---

<div class="post-metadata">

### Author: ![adnan794](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@adnan794](https://forum.kirupa.com/u/adnan794)
#### Post date: [July 12, 2008, 4:30pm UTC](https://forum.kirupa.com/t/disabling-event-listener-for-child-sprite/265792/5 "2008-07-12T16:30:32Z")

</div>

Hello

Thanks again

Yes I can do a conditional to check whether it was clicked before or not you said that the other option souds better.

I am not a hard coder like you I am trying to write efficient code can you please tell me which approach is better because If I assign individual listeners to each button and lets suppose I have 50 buttons then wont it be inefficient?

One more thing can you please tell me that whether you have made your website in AS2 or AS3 and what design programme you use to design your backgrouds and art? Thanks

---

<div class="post-metadata">

### Author: ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)
#### Post date: [July 12, 2008, 5:11pm UTC](https://forum.kirupa.com/t/disabling-event-listener-for-child-sprite/265792/6 "2008-07-12T17:11:28Z")

</div>

I like the idea of 50 listeners better than conditionals tbh, just for code clarity. They can all use the same callback as well, so you can just loop the listeners out.

something like this (warning, browser code :P)

`  
for (var i:int = 0; i \< numButtons; i++) {  
\_buttonArray\*.addEventListener(MouseEvent.CLICK, myCallback);  
};

function myCallback(e:MouseEvent):void {  
e.currentTarget.removeEventListener(MouseEvent.CLICK, myCallback);  
};  
`

My website has a few years on it, and it was made in AS2. As for design programs, there’s really no art going on there 😛 it’s just a few gradients and fonts 😉
