开发者

Flash Sprite loses focus on MOUSE_DOWN event

My Sprite class keeps losing focus when I click with the mouse - specifically after the MOUSE_DOWN event (before the click is complete).

I have set mouseEnabled to false on the children, no change. I added a listener for FOCUS_OUT and noticed that the FocusEvent.relatedObject property is NULL, which is confusing me - doesn't that mean there is no new focus target, the focus is just getting lost?

The exact sequence of events I get, by tracing them, as I click:

[FocusEvent type="focusOut" bubbles=true cancelable=false eventPhase=2 relatedObject=null shift开发者_Go百科Key=false keyCode=0]
[MouseEvent type="mouseDown" bubbles=true cancelable=false eventPhase=2 localX=355 localY=362 stageX=360 stageY=367 relatedObject=null ctrlKey=false altKey=false shiftKey=false buttonDown=true delta=0])
[MouseEvent type="click" bubbles=true cancelable=false eventPhase=2 localX=355 localY=362 stageX=360 stageY=367 relatedObject=null ctrlKey=false altKey=false shiftKey=false buttonDown=false delta=0]


Try setting mouseChildren = false; on the sprite instead of mouseEnabled = false; on the children. If the sprite's children have mouseEnabled set to false, none of the visible elements contained in your sprite are clickable, except for the shapes drawn directly in its own graphics. You would then actually click "through" the children and on the stage.

[EDIT]:

I've created a test to reproduce your problem. You're right - the focus is mysteriously lost when a sprite is clicked on, even though one would expect this to actually set the focus on it. The same is true for MovieClips, but not for TextFields. Unpractical though that may be, you can work around it by adding a custom mouseDown handler:

private function onMouseDown (ev:Event) : void {
    if (stage.focus != sprite) stage.focus = sprite;
}


If you extend any InteractiveObject you have to set tabEnabled = true; . This will make your clicked upon object gain focus.

Be careful though: If tabEnabled is false, but mouseChildren is true, then the stage.focus will be set to null.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜