A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: on mouse move trigger an event AS2

  1. #1
    Member
    Join Date
    Dec 2007
    Posts
    30

    on mouse move trigger an event AS2

    Hi guys wondering if any of you wise folks can help.
    I need to know how to move the playhead to a frame when the mouse is moved.
    I want a showreel to be looping continuously but when the mouse is moved I want the playhead to go to a different frame.
    How can I do this. I know I could script a button to click on but i think this would be a much nicer way.

  2. #2
    Uses MX 2004 Pro Quixx's Avatar
    Join Date
    Nov 2004
    Location
    U.S.
    Posts
    877
    Here are a couple methods you could try. (NOTE: in both cases I am using the code to control a Movie Clip named "numbers_mc". I wasn't sure if you'd need or want comments in the code, but I figured I'd put them in just in case.)

    This first method uses onMouseMove to track whether the mouse has moved and, because of this, the faster one moves their mouse over the stage the quicker the movie clip frames will go by. This means the movie clip will play at a normal speed, regardless of how fast the mouse is moving.

    Actionscript Code:
    // Stop the Movie Clip from Playing by itself
    _root.numbers_mc.stop();
    // Use onMouseMove to track when the mouse has moved
    _root.numbers_mc.onMouseMove = function() {
    // If the mouse has moved, go to the next frame
        this.nextFrame();
    // If the Movie Clip has reached the last frame, restart at frame 1
        if (this._currentframe == this._totalframes) {
            this.gotoAndStop(1);
        }
    };

    If you'd prefer the movie clip play at the speed set for the swf, then you could use the following code, where the variables xpos and ypos store the previous _xmouse and _ymouse positions and then tells the movie clip to play when they do not match each other upon the next frame.

    Actionscript Code:
    // xpos and ypos variables are set up to hold the old positions of the mouse.
    var xpos:Number = _xmouse;
    var ypos:Number = _ymouse;
    // Stop the Movie Clip from playing on its own
    _root.numbers_mc.stop();
    // Use onEnterFrame to loop and check the mouse position code
    _root.numbers_mc.onEnterFrame = function() {
        // if xpos does not match the current x position of the mouse or ypos doesn't match the y position...
        if (xpos != _xmouse or ypos != _ymouse) {
            // ...then we tell our Movie Clip to play further
            this.play();
            // Otherwise...
        } else {
            // we tell the Movie Clip to Stop playing.
            this.stop();
        }
        // We update the xpos and ypos variables to match the current x and y position of the mouse so we can test
        // them against the mouse position on the next frame call.
        xpos = _xmouse;
        ypos = _ymouse;
    };

  3. #3
    Member
    Join Date
    Dec 2007
    Posts
    30
    thanks for that great reply Quixx but i'm sure I made it sound more complicated than it is.
    I don't need to control a clip.
    A clip will be playing and when the mouse moves the playhead moves to another frame where i have an interface for navigating some pages, galleries of work and stuff.something like:

    onMouseMove .gotoAndPlayframe (20);

    if you could put me right on the syntax.

  4. #4
    Uses MX 2004 Pro Quixx's Avatar
    Join Date
    Nov 2004
    Location
    U.S.
    Posts
    877
    Ah, I get it I think. So you have an opening movie sequence playing and when the person moves the mouse you want it to jump to a frame holding your interface, rather then having to click a button to get there? That might be a bit bothersome, wouldn't it? Only because once the person moves the mouse, even a little, they would end up pushing past the opening frame and may not get to see it at all.

    In any event, here's some code that will jump the frame to another when the mouse has be moved and then deletes itself (because I assume there's no need for the code to be checked once you're beyond the opening, right)? You'll notice I highlighted _root.gotoAndStop(20) in red within the code below. I used a gotoAndStop() rather then a gotoAndPlay() here, because I figured you'd probably want the main movie to go to frame 20 and stay there. If that's not the case, just switch that line out with _root.gotoAndPlay(20);.

    Code:
    _root.stop();
    // Use onMouseMove to track when the mouse has moved
    _root.onMouseMove = function() {
    	// If the mouse has moved, go to the correct frame
    	_root.gotoAndStop(20);
    	// Delete the onMouseMove event after its done its job to save on computing power
    	delete _root.onMouseMove;
    };

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center