Mouse-wobbling blobby things

I’ve been spending the last few days on some rather interesting ActionScript challenges. I’ve been building a sort of a lava lamp gloopy movement machine. I’ve been up to my neck in physics and trigonometry, so today when I had to change the way that the mouse moves objects around in the “gloop”, I got too carried away with triangles and tangents before coming home to think, and realising how simple it ought to be. Here’s some fun code, paste it into any MovieClip in Flash: put it on the first frame and then on the last frame, add a simple gotoAndPlay(2) so that the initialisation doesn’t take place twice. Or turn it into a proper object: this is just my quick & dirty version:


reach = 250; // radius of the "force field" around the mouse (pixels)
strength = 10; // strength of the mouse effect, 1-100
speedx = Math.random() * 3; // initial x speed
speedy = Math.random() * 3; // inital y speed

this.onEnterFrame = function ()
{
_x += speedx;
_y += speedy;
if (_x < 0 || _x > Stage.width)
{
speedx = -speedx;
}
if (_y < 0 || _y > Stage.height)
{
speedy = -speedy;
}
distance = Math.sqrt(Math.pow (_xmouse, 2) + Math.pow (_ymouse, 2));
if (power > 0)
{
updateSpeed();
}
power = Math.max (0, reach - distance) / reach;
oldX = _xmouse;
oldY = _ymouse;
}

function updateSpeed()
{
speedx += (_xmouse - oldX) * power * strength/100;
speedy += (_ymouse - oldY) * power * strength/100;
}

You should be able to drag the object around the screen, and throw it off in different directions. Try playing with the range and strength variables for different effects.