3 Replies Latest reply on Jul 8, 2010 7:47 AM by Kwangle

    'Floating' effect

    AESS1981 Level 1

      hi there,

       

      I am using the following script to create a floating effect. The script is on the mc:

       

       

      onClipEvent(load){

      var my_x:Number = 0;

      var my_y:Number = 0;

      }


      onClipEvent(enterframe){

      my_x += 0.0;

      my_y += 0.15;


      this._x =this._x +Math.sin(my_x);

      this._y =this._y +Math.cos(my_y)*.5;

      }

      The code works, but over a period of time, the mc moves to the top of the page.
      Is there any way I can prevent this?
      Thanks

        • 1. Re: 'Floating' effect
          Kwangle

          Try setting an origin point based on the starting position of the object you want to 'float'

           

          xOrigin = myObject._x;

          yOrigin = myObject._y;

           

          you could also set a distance for how far the object floats away from the origin (in pixels)

           

          dist = 10;

           

          then when you apply your sine wave motion just add it to the origin:

           

          with(myObject) {

          _x = xOrigin + Math.sin(value)*dist;

          _y = yOrigin + Math.sin(value)*dist;

           

          }


          the sine of an angle is always between the values of -1 and 1 so multiplying your sine result by your dist value gives you a drift of -10 to 10 in each axis.

          • 2. Re: 'Floating' effect
            AESS1981 Level 1

            Thanks for your help.

            I have been trying your advice, but my attempt is giving the output "Error: A 'with' action failed because the specified object did not exist."

             

             

            onClipEvent(load){

            xOrigin = myObject._x;

            yOrigin = myObject._y;

            dist = 10;


            }


            onClipEvent(enterframe){

            with(myObject) {

            _x = xOrigin + Math.sin(value)*dist;

            _y = yOrigin + Math.sin(value)*dist;

             

            }

            }

             

             

            The actionscript has been applied to a mc on the stage. Any ideas?

            Many thanks

            • 3. Re: 'Floating' effect
              Kwangle Level 1

              This is not working code I was just pointing you in the right d

              irection - you need to adjust the code to target the object you want to ma

              ke float, myObject should be changed to the instance name of your floating objec

              t. Also the 'value' variable should be changed to a variable that changes so the object moves over time, and this value has to be in radians not degrees.

               

              Hope this helps.