?
Solved

Detect mouse RollOver and RollOut with actionscript

Posted on 2004-10-12
13
Medium Priority
?
869 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.
Suggested Courses

771 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