I have this red hanging lamp on my website and want it to swing either left or right depending on what side the mouse rolls over from so that it looks like the mouse gave it a small push (I attached the file of what I have so far).
I found a script for a pendulum that was posted a while back and tried to adapt it to my lamp but haven't been very successful yet. This is the script that I'm using:
/*
We need the function that will move our lamp. Basically,
we have a parameter t that represents the time. We increment this on
each frame to simulate time going forward. We put this value of time
into our sine function, and then we mutliply by our scale factor a,
whic represents the amplitude of the swing, and by another factor d,
which will dampen the oscillation once we mouse off.
*/
function swinglamp(p:MovieClip) {
//Apply our swing formula;
var rot:Number = p.a * p.d * Math.sin(p.t);
//Now set the rotation t this value;
p._rotation = rot;
//Increment the time;
p.t += Math.PI / p.speed;
}
/*
Now we need the onRollOver event;
*/
lamp.onRollOver = function() {
//We need to give the lamp some inital parameters;
//Amplitude (in degrees):
this.a = 10;
//The speed of oscillation (the bigger the number, the slower the oscillations);
this.speed = 8;
//Set the time;
this.t = 0;
//Set the damping (initally no damping, so set to 1);
this.d = .5;
//Set the onEnterFrame to our swinglamp function;
this.onEnterFrame = function() {
swinglamp(this);
}
}
/*
And then for the RollOut, all we do is slowly decrease the value of d.
Once d falls below zero, we delete the onEnterFrame to save memory;
*/
lamp.onRollOut = function() {
this.onEnterFrame = function() {
swinglamp(this);
//To make the lamp slow down more quickly, decrease this number (but not by a lot!)
this.d *= 0.90;
//Test to see if d < 0;
if (this.d < 0) {
this._rotation = 0;
delete this.onEnterFrame;
}
}
}