PDA

View Full Version : Detect actual mouse position after quick motion?


CrookedGrin
03-09-2007, 11:01 PM
Hi. Sorry for the verbose explanation, but this is not a simple question.

Basically, I am replacing the mouse pointer with a custom graphic in an .swf that takes up part of a web page (.swf stage is about 350px wide, total layout is about 1000px). My code is all working fine, except that, if you move the mouse fairly quickly -- as you might do to get from a point on the .swf to the opposite side of a large monitor -- the .swf loses track of the mouse, and thinks that the ._xmouse/._ymouse values are still the same as the last point that that Flash saw the mouse, which is usually somewhere in the middle of the stage, thus "orphaning" the custom cursor. If I have a setInterval or something tracing the mouse position, the .swf still thinks that the mouse is sitting on top of it, even though the cursor is actually far away somewhere else. hitTest also still returns true as though the mouse was there (as you might expect, given that it's based on _xmouse/_ymouse).

The workaround/hack I have going now is that, once the pointer switches over to the custom cursor, it starts a setInterval, and then if the mouse appears not to move for a few seconds, it reverts back to the default cursor (i.e., Mouse.show). Obviously, this is not optimal, because that means that the custom cursor disappears if you don't move the mouse for a second or two, and it doesn't really solve the problem anyway because it still looks like the custom cursor gets stuck for a second when you move too fast.

The funny thing is that, somewhere out there, either Flash, the OS, or the browser knows that the cursor isn't actually on top of the stage, because when the setInterval reverts it to the regular cursor, the regular cursor doesn't reappear on top of the Flash movie. Somebody in the system knows where the cursor is "for real", but Flash is not receiving this information.

So my question is whether someone knows of a way to communicate the actual location of the mouse (or its presence on top of some other page object, like a background div or something) to the .swf via ExternalInterface or something, to tell it that, despite what it thinks, the mouse is actually somewhere else? Is there some kind of focus event for the stage or Flash player instance that would work for this? Is there a JavaScript hack that someone has used? I'm thinking maybe the div that takes up the rest of the layout can have some kind of rollOver detection which calls a js function to, in turn, call an as function in the original clip, but my javascript is kinda weak and I don't know of a good way to do this.

Any help will be greatly appreciated.

Mike

Nich
03-10-2007, 09:14 AM
If I understand what you're saying correctly, then the problem is in how flash handles mice. As far as I can tell, flash can only detect the cursor when it's inside the flash window. This means that if you leave the window quickly, the cursor will stay where it is.

Just to be clear, when the bug occurs is the cursor's actual position OVER the swf? Or outside?

creatify
03-10-2007, 10:33 AM
How are you telling the custom cursor to follow the mouse? Livedocs suggests using the onMouseMove Listener see here (http://livedocs.adobe.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00002429.html). Notice the updateAfterEvent(); This allows flash to retrieve properties etc. without having to wait for another frame event. That usually helps quite a bit, you might f1 help in flash and search for that method - If I recall, it may only be used in certain events. Maybe you're already using this method.

If using an onEnterFrame, flash can only return the _xmouse, _ymouse properties at the speed of the frame rate. IE, if you're frame rate is 12fps, then you can only retrieve the _xmouse, _ymouse positions roughly 12x a second - and most anyone can move their mouse much faster than that, which will cause your pointer to orphan.

Ultimately, I can't recall seeing any flash site/app where a custom cursor actually tracked as quickly as the mouse. I think your idea of using other languages to retrieve mouse position is interesting, but I can't image it could be faster since you'd have to have flash communicate w/ another language, find the mouse position, then return the values back to flash. I wonder if AS3 contains new items to help this?

Krilnon
03-10-2007, 01:06 PM
I wonder if AS3 contains new items to help this?
Yes: flash.events.Event.MOUSE_LEAVE (http://livedocs.adobe.com/flex/201/langref/flash/display/Stage.html#event:mouseLeave)

CrookedGrin
03-12-2007, 09:36 PM
Hi. Thanks for the responses. The solution/hack I eventually came up with -- using a JavaScript onMouseOut event from the div surrounding my swf object, which then calls a function in the .swf via ExternalInterface -- seems to work pretty well for my purposes. All I really need is for the swf to know when it no longer has the mouse, and this takes care of that. (FYI, I was already using onMouseMove and a listener along with updateAfterEvent to control the motion, and I had the frame rate set at 30 FPS, for the reasons you guys listed above.)

The AS3 event Krilnon mentions seems to be exactly what I was looking for, but I guess I'll have to make do with JavaScript for now.

Thanks!

Mike