00:00
00:00
Newgrounds Background Image Theme

Hauntir just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Single Mouse Event Listener for all

448 Views | 7 Replies
New Topic Respond to this Topic

I'm kinda rusty with all things AS3 as I've been working with C# the past year but I was browsing through my code from my first game I ever made (this was before knew how to use classes) and I noticed all of these mouse click listeners

gameCon.heroAttack.addEventListener(MouseEvent.CLICK, onHeroAttackClick);
			gameCon.villianAttack.addEventListener(MouseEvent.CLICK, onVillianAttackClick);
			gameCon.heroMagic.addEventListener(MouseEvent.CLICK, onHeroMagicClick);
			gameCon.villianMagic.addEventListener(MouseEvent.CLICK, onVillianMagicClick);
			gameCon.heroHeal.addEventListener(MouseEvent.CLICK, onHeroHealClick);
			gameCon.villianHeal.addEventListener(MouseEvent.CLICK, onVillianHealClick);
			gameCon.playAgain.addEventListener(MouseEvent.CLICK, onPlayAgainClick);
			gameCon.showBtnMap.addEventListener(MouseEvent.CLICK, onShowBtnMap);
			gameCon.menuBtn.addEventListener(MouseEvent.CLICK, onMenuBtn);
			muteBtn.addEventListener(MouseEvent.CLICK, onMuteBtn);

It got me wondering.... is there a way in AS3 to just listen for a single mouse click in the entire game, check for object(s) where mouse was clicked, and then execute code according to what was clicked? Even if what was clicked on was nested in another MC?

Response to Single Mouse Event Listener for all 2015-08-16 22:07:05


At 8/16/15 08:59 PM, Hero101 wrote: It got me wondering.... is there a way in AS3 to just listen for a single mouse click in the entire game, check for object(s) where mouse was clicked, and then execute code according to what was clicked? Even if what was clicked on was nested in another MC?

Adding a click event to the stage should work.

Response to Single Mouse Event Listener for all 2015-08-16 22:25:35


At 8/16/15 10:07 PM, Diki wrote:
Adding a click event to the stage should work.

That simple, eh? I'm guessing you would just use e.target then to find out what you are clicking on?

Response to Single Mouse Event Listener for all 2015-08-16 22:38:32


At 8/16/15 10:25 PM, Hero101 wrote: That simple, eh? I'm guessing you would just use e.target then to find out what you are clicking on?

e.currentTarget will give you the thing you clicked on. E.target is the thing you added the listener to, which would just be stage.

Response to Single Mouse Event Listener for all 2015-08-16 22:48:56


At 8/16/15 10:38 PM, MSGhero wrote:
At 8/16/15 10:25 PM, Hero101 wrote: That simple, eh? I'm guessing you would just use e.target then to find out what you are clicking on?
e.currentTarget will give you the thing you clicked on. E.target is the thing you added the listener to, which would just be stage.

Oh that's right I remember that now. What if there was an MC nested in another MC....how would you go about interacting with the nested MC using e.currentTarget? I guess if, for purely example purposes, if the nested MC was inside an MC called gameContainer would you just do something like:

if (e.currentTarget == gameContainer.nestedMC)

Essentially you would need to know what MC the nested MC was in correct?

Response to Single Mouse Event Listener for all 2015-08-17 00:00:57


At 8/16/15 10:48 PM, Hero101 wrote: Essentially you would need to know what MC the nested MC was in correct?

Current target would be the nested mc. If you want the outer mc, set the inner mc's mouseEnabled to false. If you have a button with a textfield inside, you have to do this or the tf will take the click instead of the button.

Response to Single Mouse Event Listener for all 2015-08-17 02:26:31


At 8/16/15 10:38 PM, MSGhero wrote: e.currentTarget will give you the thing you clicked on. E.target is the thing you added the listener to, which would just be stage.

It's the other way round: currentTarget is the object that the listener was added to.

Response to Single Mouse Event Listener for all 2015-08-17 08:34:28


At 8/17/15 02:26 AM, milchreis wrote: It's the other way round: currentTarget is the object that the listener was added to.

Dang, thanks. I haven't used those in a long time.