A Flash Developer Resource Site

Results 1 to 1 of 1

Thread: Swing Hanging Lamp - Help

Hybrid View

  1. #1
    Junior Member
    Join Date
    May 2007
    Posts
    23

    Question Swing Hanging Lamp - Help

    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;
    }
    }
    }
    Attached Files Attached Files

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