Hey guys, As we all know SWiSHmax can make objects follow the mouse by simply adding the following 'swishscript' to any object you wish:
| QUOTE | onLoad () { startDragLocked(); }
|
But the problem with this is the effect is very rough, or in other words, it would be nice to have the object slowly follow the mouse and ease down before reaching it. Whilst being able to set the speed of this at the same time.
So here is a code I simply edited from a Flash MX tutorial to work with SWiSHmax that gives the mouse follower easing:
| QUOTE | onLoad () { _x = 0; _y = 0; speed = 10; _alpha = 100; } onEnterFrame() { endX = _root._xmouse; endY = _root._ymouse; _x += (endX-_x)/speed; _y += (endY-_y)/speed; }
|
Same method as the first, just apply it to any object you wish.
Now you can do quite a few things with this script,
1. If you want to turn the dragging on/off you need to have a script that does so....obviously. So replace the code above with this:
| QUOTE | onLoad () { _x = 0; _y = 0; speed = 10; _alpha = 100; } onEnterFrame() { if (_root.drag) { endX = _root._xmouse; endY = _root._ymouse; _x += (endX-_x)/speed; _y += (endY-_y)/speed; } } |
^^ The script in red is what I have added ^^
Basically it telling the object that if 'drag' is enabled then the script below will 'turn on' in a way, and if it isn't - the script will not activate.
So to turn the script on and off simply use:
| QUOTE | | _root.drag = false; |
-or-
_root.drag = true;
Depending on whether you want following on or off, true or false.
Now a little explanation:
· This resets the mouse's coordinates to 0,0 Doing this will ensure a smooth transition, for example if you set it to 54,-21 when you start the follower it will jump to those coordinates and then ease to your pointer - very ugly.
· Sets the speed of the object when following the mouse. 0 = Very fast, using this speed you might aswell use swish's build in drag script found at the top of this thread. 100 = Very slow, using this speed your in for a helluva long wait for the object to reach the mouse.
I find that 10 is sufficient.
_alpha = 100;
· This part I added just incase your using actionscript to control the alpha of an object and want it to change when following starts. Change '100' to any value you wish.
Finally:
| QUOTE | endX = _root._xmouse; endY = _root._ymouse; _x += (endX-_x)/speed; _y += (endY-_y)/speed;
|
This part, well honestly I have no idea what it means but without it the effect doesn't work. I know it has something about calculating the mouse's position etc. But I wouldn't be too sure. Well thats about it! You can create some great effects with this 'swishscript', so have a play with it! -rob
|