View Full Version : Isn't the stage MouseClick listens toooo fast??
zhangxiaorui
September 15th, 2008, 10:58 AM
I put a button, "btn" on the stage, and add a listener to the stage after the btn is clicked. But as soon as the button clicked, the stage handler (function b) also fires!
btn.addEventListener(MouseEvent.CLICK, a,)
function a(e:MouseEvent)
{
trace(3)
stage.addEventListener(MouseEvent.CLICK, b)
}
function b(e:MouseEvent)
{
trace(4)
}
Shouldn't the stage handler wait until the next hit?
senocular
September 15th, 2008, 11:14 AM
It's part of event propagation. Since the button is part of what makes up the contents of the stage, by hitting the button you're also hitting the stage so the stage also registers the click. Its like if I poked you in the arm. I'm really poking your arm, but its also not incorrect to say I poked you, because your arm is a part of you as a whole.
http://www.adobe.com/devnet/actionscript/articles/event_handling_as3_03.html
canazza
September 15th, 2008, 11:33 AM
that's not what he's saying senocular, I had to read it through a few times, the code's not well formatted
ActionScript Code:
btn.addEventListener(MouseEvent.CLICK, a)
function a(e:MouseEvent)
{
trace(3)
stage.addEventListener(MouseEvent.CLICK, b)
}
function b(e:MouseEvent)
{
trace(4)
}
what he's saying is that he registers A, clicks the button THEN registers B, but B gets called right away
[edit]
I think it's because of event bubbling.
the Button is a child of Stage, so clicking on the Button does the click events for Button, then it checks for click events for stage. Because you added a click event for stage WITHIN the button, it then says "hey, there's a click event here for stage" and executes it
oh, and editing screws up my AS formatting :( I had to type it in again
senocular
September 15th, 2008, 11:45 AM
that's not what he's saying senocular, I had to read it through a few times, the code's not well formatted
what he's saying is that he registers A, clicks the button THEN registers B, but B gets called right away
Ah! Thanks for the clarification canazza.
At least the reason is related to my last post. When Flash fires events it works through each target object in the dispatch hierarchy (display list hierarchy for bubbling events, which CLICK is) and runs through all the listeners registered for each of those objects calling them with the appropriate Event object being passed in. This occurs for the three events: Capture, At Target, and Bubbling. By default listeners are added to the At Target and Bubbling phases which means events will be heard at lowest level of the hierarchy first, then working its way up towards the stage.
Since you're clicking on the button, that is the first object to receive the event so its listeners are looped through and called. That will be followed by whatever other containers you might have for your button up to root and then finally stage. Because stage is last, any listeners you add to the stage before the stage is targeted for that event (such as in the button listener) will get called as part of the current event.
See the second graphic in the link I posted above. Assuming "box" there is your button, if you add a stage listener in a box listener, that listener will be called in that same event pass once the event reaches the stage.
zhangxiaorui
September 16th, 2008, 11:25 AM
Amazing explanation! Thanks! I love you senocular!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.