开发者

Alternatives for registering Events on the stage in AS3?

I am programming a little soft开发者_Go百科ware prototype as Flash/Actionscript3 application. Currently I registered some Events on stage - but it is cumbersome since stopPropoagation() needs to be used all the time.

As Example: I am having a element shown via mouseclick would and a event for closing the menu on stage. Without using stopPropagation, the menu opens and closes again immediately. The hide-function is registered on some objects so just checking if target= stage would not do it, unfortunately.

Are there any good solutions to get around this?


So you have a listener for a MOUSE_CLICK on the stage thats getting fired when you click on an 'element'.

Which looks something a bit like:

addEventListener(MouseEvent.CLICK, onClick)
function onClick(e:MouseEvent)
{
    trace("CLICK")
}
mc.addEventListener(MouseEvent.CLICK, onMcClick)

function onMcClick(e:MouseEvent)
{
    trace("mc")
    e.stopPropagation();
}

If thats the case then yes, stage will always recieve this event since it has to do with how native flash events propogate and bubble. http://www.adobe.com/devnet/actionscript/articles/event_handling_as3_03.html

Instead of listening to the stage and having to call stopPropogation you can restructure your code. You will need to remove the listener on the stage and instead add it to the actual item so:

mc2.addEventListener(MouseEvent.CLICK, onClick)

function onClick(e:MouseEvent)
{
    trace("CLICK")
}


mc.addEventListener(MouseEvent.CLICK, onMcClick)

function onMcClick(e:MouseEvent)
{
    trace("MC 2 CLICK")

}

Of course this might then require you to change some of your other code but since I can't see it I am not sure what that is. Just remember that events propogate and bubble. So if you had a movieclip 'c' inside a movieclip 'b' thats on stage, and both c and b have listeners for a MOUSE_CLICK then if you click on c then both b and c events will recieve this event since it bubbles up the display list. But if c was not in b but c was on the stage and b was on the stage then this would not happen since b is not on the path for the bubbling of c. Hope that helps :)


1 solution is to check stage.focus , that is if when the menu opens the focus is on it you can add a focus out event listener so when the stage is clicked and the menu loses focus it will close .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜