Dancer
Dancer

Reputation: 17681

Flash AS3 Event propagation

I wonder if you can help with something I imagine is relatively simple - but amazingly frustrating!

I have a list of movie clips that each contain a button with an instance name Mbtn. I would like the button to appear only when the mouse hovers over the parent clip. The structure is basically a parent clip called pcs, 5 child clips (named below) each containing a button with an instance name of mBtn. here is my code -

hr_pc.mBtn.visible=false;
m_pc.mBtn.visible=false;
me_pc.mBtn.visible=false;
d_pc.mBtn.visible=false;
di_pc.mBtn.visible=false;




pcs.addEventListener(MouseEvent.ROLL_OVER, mOver);
pcs.addEventListener(MouseEvent.ROLL_OUT, mOut);

function mOver(evt:MouseEvent):void
{
var overNow = evt.currentTarget.name;
overNow.mBtn.visible=true;


}
function mOut(evt:MouseEvent):void
{
var overNot = evt.currentTarget.name;
overNot.mBtn.visible=false;
} 

I'm sure there's a much better way to do this - any suggestions very gratefully received!

Thanks Paul

Upvotes: 0

Views: 368

Answers (1)

Bosworth99
Bosworth99

Reputation: 4244

evt.currentTarget.name

means that, right now, you are targeting your container object (pcs), and not the contents (hr_pc, m_pc etc). This is the key difference between evt.target and evt.currentTarget. If you trace out the currentTarget name, it will be identical for each intended target. So, to rectify,you can certainly make this work with

evt.target.name

You may have issues, however, if you mouse over the button, as the target will no longer be the buttons direct parent (it will be the button itself), and the button is likely to trigger visible=false again.

I would probably go a different direction with this: write a class that contains the button and its own listeners. This would be a good plan if there is a lot of shared behavior (but depending on your needs, may not be).

So, yeah, read up on currentTarget vs target

Upvotes: 1

Related Questions