Link to home
Start Free TrialLog in
Avatar of squarelight
squarelight

asked on

How do I change an instance of movieClip based on the direction the cursor moves?

I am trying to make something fly around the screen based on the user's control of the mouse. I've attached the movie clip via the method below. I'm looking for some kind of if statement to recognize if the mouse is reaching a positive or negative increment from it's previous position and then change the movieClip (cursor_mc) to another frame.

Thanks!
cursor_mc.startDrag("true"); 
Mouse.hide();

Open in new window

Avatar of rascalpants
rascalpants
Flag of United States of America image

you could use an onEnterFrame event, or a MouseMove (preferred) event to check for the previous x position compared to the current.

rp / ZA
Avatar of squarelight
squarelight

ASKER

fario - i tagged this as an as2 file but I didn't mention it in my post. I am working in as2, not as3.

I've found some code that works and have attached it below. One more thing I would like to do is unload or override the function so that xscale = 100 when a button is clicked. Can anyone help me with that?
onClipEvent(enterFrame) {
 
horizontal_check = function () {
        this.createEmptyMovieClip('tempx', this.getNextHighestDepth());
    tempx.lastX=tempx._root._xmouse;
        tempx.onMouseMove = function() {
                if (this._root._xmouse>this.lastX) {
            trace('moving right');
			_parent.cursor_mc._xscale = -100;
        } else {
            trace('moving left');
			_parent.cursor_mc._xscale = 100;
                }
    this.lastX=this._root._xmouse;
        };
};
horizontal_check();
 
}

Open in new window

squarelight,

you are going it wrong...  you are using both an onEnterFrame and an onMouseMove method to do your if/then conditional statement...

you really need to separate then, or better yet, choose one or the other...

I would do something more like the below(using your code)...  it only does the check when you move your mouse, so your processing isn't going crazy.


now my code assumes you want the cursor_mc MovieClip to be the item that scales based on the position...  

Is this the desired effect you wanted?


rp / ZA


import mx.utils.Delegate;
 
cursor_mc.startDrag("true");
Mouse.hide();
 
cursor_mc.initScale = cursor_mc._xscale;
cursor_mc.lastX = _xmouse;
 
 
cursor_mc.onMouseMove = Delegate.create( this, horizontal_check );
 
horizontal_check()
{
     if ( _xmouse > cursor_mc.lastX)
     {
         trace('moving right');
         cursor_mc._xscale = cursor_mc.initScale;
     }
     else
     {
         trace('moving left');
         cursor_mc._xscale = 100;
     }
    cursor_mc.lastX = _xmouse;
}

Open in new window

Thanks, rascalpants. I tried your code and it only traces when you are moving left. Am I missing something? I'll play with it some more.
ASKER CERTIFIED SOLUTION
Avatar of rascalpants
rascalpants
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial