Solved

Detect mouse RollOver and RollOut with actionscript

Posted on 2004-10-12
13
867 Views
Last Modified: 2008-01-09
So here is my problem I have built a movie with some sliding buttons that react to MouseOver events.  But the problem is that if you run your mouse over them quickly they stay all the way out instead of sliding back in.

Right now its all done with tweening and using Mouse events but is there a sure fire way to make sure that if that mouse leave the button it will slide in and if the mouse slides over it will slide out?

Thanks

LAter

IC
0
Comment
Question by:InnerChild
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 6

Expert Comment

by:hsmtp
ID: 12287954
Hi InnerChild,

use hitTest combining with onEnterFrame of some movieClip.
Read more in help about hitTest.
0
 
LVL 2

Author Comment

by:InnerChild
ID: 12288008
Would you have any examples that I could try?  I have found some tutorials online but they didn't seem to help.

Thanks

IC
0
 
LVL 6

Expert Comment

by:hsmtp
ID: 12288115
InnerChild,

_root.createEmptyMovieClip("cycler", 12345);
_root.cycler.onEnterFrame = function() {
    if (_root.myButton.hitTest(_root._xmouse, _root._ymouse, true)) {
         // mouse inside the button
    } else {
         // mouse outside the button
    }
}
0
Industry Leaders: 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!

 
LVL 6

Expert Comment

by:hsmtp
ID: 12288132
if you have only one frame in button, use can use it directly without cycler MC:


_root.myButton.onEnterFrame = function() {
    if (this.hitTest(_root._xmouse, _root._ymouse, true)) {
         // mouse inside the button
    } else {
         // mouse outside the button
    }
}

0
 
LVL 2

Author Comment

by:InnerChild
ID: 12292118
I tried the second AS and got this error message

**Error** Symbol=mona_roll, layer=Layer 2, frame=12:Line 1: Statement must appear within on handler
     _root.mona_roll.onEnterFrame = function() {

I then tried it in a on (rollOut) and the animation worked but the problem with the button is still there.

IC
0
 
LVL 2

Author Comment

by:InnerChild
ID: 12292239
Also tried the first AS, and tried both on the movie clip instead of the button......
Nothing working properly let.

IC
0
 
LVL 6

Expert Comment

by:hsmtp
ID: 12296091
InnerChild,

What version of Flash do you use?
0
 
LVL 2

Expert Comment

by:benlowry
ID: 12296909
Try this:

ButtonClip.onRollOver = function()
{
    this.onEnterFrame = function()
    {
         if(this._x < sometargetvalue)
         {
                 this._x += 1;
          }
          else
          {
                delete this.onEnterFrame;
          }
     }
}

Button.onRollOut = function()
{
    this.onEnterFrame = function()
    {
         if(this._x > sometargetvalue)
         {
                 this._x -= 1;
          }
          else
          {
                delete this.onEnterFrame;
          }
     }
}

What's happening here is we're creating an onEnterFrame function each time the mouse passes on or off a button.  When the onEnterFrame's if condition is satisfied the function is deleted.  If a user mouses off a button while the on function is still occuring, the onEnterFrame will simply change to the new one.

Hope this makes sense,
Ben
0
 
LVL 10

Expert Comment

by:Savong
ID: 12302376
If that doesn't help you may want to try simply changing the frames per second option for the stage, if it is at the default of 12 it can cause something like what you've described (no guarantees though).
0
 
LVL 2

Author Comment

by:InnerChild
ID: 12312424
Sorry about the late reply this is one of a few projects I am working on....

benlowry
Hate to sound like such a newbie but where am I putting this code?  (mona_roll) is the buttons instance name)

Also Savong the frame rate is at 16 so that probably isn't the problem.

Thanks

IC
0
 
LVL 2

Accepted Solution

by:
benlowry earned 50 total points
ID: 12313411
on the keyframe where the buttons are stored.
-----------------------------------------------------------------------------------------------
mona_roll.onRollOver = function()
{
    this.onEnterFrame = function()
    {
         if(this._x < sometargetvalue)
         {
                 this._x += 1;
          }
          else
          {
                delete this.onEnterFrame;
          }
     }
}

mona_roll.onRollOut = function()
{
    this.onEnterFrame = function()
    {
         if(this._x > sometargetvalue)
         {
                 this._x -= 1;
          }
          else
          {
                delete this.onEnterFrame;
          }
     }
}


or
-----------------------------------------------------------------------------------------------
if they're Button clips, on the buttons themselves:

on(rollOver)
{
    this.onEnterFrame = function()
    {
         if(this._x < sometargetvalue)
         {
                 this._x += 1;
          }
          else
          {
                delete this.onEnterFrame;
          }
     }
}

on(rollOut)
{
    this.onEnterFrame = function()
    {
         if(this._x > sometargetvalue)
         {
                 this._x -= 1;
          }
          else
          {
                delete this.onEnterFrame;
          }
     }
}

or
-----------------------------------------------------------------------------------------------
if they're movieclips, on the movieclips themselves:
mona_roll.onLoad = function()
{
    this.onRollOver = function()
    {
        this.onEnterFrame = function()
        {
             if(this._x < sometargetvalue)
             {
                    this._x += 1;
              }
            else
             {
                delete this.onEnterFrame;
             }
        }
    }

    this.onRollOut = function()
    {
        this.onEnterFrame = function()
        {
             if(this._x > sometargetvalue)
            {
                 this._x -= 1;
             }
             else
             {
                delete this.onEnterFrame;
             }
        }
    }
}

Any of these 3 options should do you fine.  Pick whichever you're most comfortable with.  I prefer having my code in keyframes on the _root timeline.  You might prefer them on clips.
0
 
LVL 2

Author Comment

by:InnerChild
ID: 12437934
Hey benlowry

This may be a little late now and I don't know if you will get this but I thought it was fixed but it turns out is isn't.  I was trying to use the movieclips AS you gave me but I am getting this error....

**Error** Scene=Scene 1, layer=slide menu, frame=1:Line 1: Statement must appear within on/onClipEvent handler
     tails_roll.onLoad = function()

Total ActionScript Errors: 1        Reported Errors: 1

I know it said mona in your AS but I changed it for a different button....

0
 
LVL 2

Author Comment

by:InnerChild
ID: 12438240
Man this always happens after I post......I got the button AS working just the way I need it so I don't need any extra hand holding.

Thanks for everything

IC
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question