• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5274
  • Last Modified:

Detect when mouse is not over stage

Hi guys. I need to be able to detect when the mouse is not over the stage.  The reason being is Im panning and menu vertically with the y  position of the mouse but when the mouse is not over the stage I want the panning to become automatic. I cant figure out how to activate the auto panning. I tried this code which seems logical to me but it doesnt work.

Any ideas?

onEnterFrame = function() {
	if (_ymouse>Stage.height || _ymouse<0 || _xmouse>Stage.width || _xmouse<0) {
		trace("mouse gone");
	}
};

Open in new window

0
kalisan
Asked:
kalisan
  • 8
  • 5
1 Solution
 
Aneesh ChopraCommented:
here is the magic to check if mouse is over Stage or not,,
and it works on AS1 and AS2 both


- Aneesh Chopra
__width = Stage.width;
// stage __width
__height = Stage.height;
// stage __height
/*  extrapolation parameter:
      * Low value (ie <1) = decrease chance of returning out when in
      * High value (ie >1) = decrease chance of returning in when out
      */
extrap = 1;
//-------------------------------//
pointerOut = false;
x0;
x1;
y0;
y1;
dx;
dy;
//
this.onEnterFrame = function()
{
      x1 = _root._xmouse;
      y1 = _root._ymouse;
      if (x1 == x0 && y1 == y0)
      {
            // pointer movement not detected
            // extrapolate to find expected mouse pos and test for inside/outside
            if (x1+extrap*dx<0 || x1+extrap*dx>__width || y1+extrap*dy<0 || y1+extrap*dy>__height)
            {
                  if (!pointerOut)
                  {
                        trace("The mouse has left the movie!");
                  }
                  pointerOut = true;
            }
      }
      else
      {
            if (pointerOut)
            {
                  trace("The mouse has entered the movie!");
            }
            pointerOut = false;
            dx = x1-x0;
            dy = y1-y0;
            x0 = x1;
            y0 = y1;
      }
};

Open in new window

0
 
kalisanAuthor Commented:
Hi Aneesh, thx for the code. Only problem is that when I rollover buttons (generated via an XML file) it registeres as mouse leaving stage. Is there a way round this?
0
 
Aneesh ChopraCommented:
provided code should work perfectly and should not get effected by any button's rollover events..

one point to take care:
above code should be placed on _root of the FLA.

if still it does not work for you.
I request to attached your sample file for review..
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kalisanAuthor Commented:
Actually, you are right, it doesn't have anything to do with the buttons, that is what I assumed it was but Ive now discovered that if I hover the mouse anywhere towards the bottom of the stage and just move it up and down slightly it registers the mouse going out and coming in quick succession. This basically happens within the bottom third of the stage.

Im getting with in a completely blank FLA with just your script on the root with the stage set to 914 width and 340 height. (CS3, ActionScript 2.0)
0
 
kalisanAuthor Commented:
hi Aneesh, have you been able to replicate the above?
0
 
Aneesh ChopraCommented:
I just tested it and working all fine.... unable to replicate...

one more thing to metion here is, I am using 30 fps for FLA
0
 
kalisanAuthor Commented:
Woudl you mind having a look at the attached an let me know what is wrong with it?
0
 
kalisanAuthor Commented:
Hmm, wont accept FLA. I've changed my frame rate from 31 to 30 but the bottom part of the stage still registers the mouse going out and coming in quick succession. Weird.
0
 
Aneesh ChopraCommented:
rename the extention .FLA to .txt and attach it
0
 
kalisanAuthor Commented:
cool, here you go...
test.txt
0
 
kalisanAuthor Commented:
any luck Aneesh?
0
 
Aneesh ChopraCommented:
Hi Kalisan,

I tested it using your FLA and agree that discrepency exists if mouse leaves or enters the stage very slowly..
and this seems the limitation of the provided code.

but results are quite good in case of natural mouse movement, (means with realistic mouse speed)
0
 
kalisanAuthor Commented:
Actually what you are getting is not what I was referring to. What is happening with me is that if I move the mouse (little movement) within the bottom third of the stage (i.e. not leaving the stage) it registers going in and out in quick succession. Are you not getting this?
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now