A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Fairly easy one

  1. #1
    Senior Member dduck1934's Avatar
    Join Date
    Sep 2000
    Location
    Georgia, USA
    Posts
    155
    I was fooling around with the Math functions and I was trying to make a MC move across my movie stage, and that was a simple task, but I went back and I am trying to figure out how to make the movieclip follow a sin curve across the stage.

    I can get it to move a little, but I am missing something.

    thanks

    dduck1934


  2. #2
    Hack
    Join Date
    Mar 2000
    Location
    Madison, WI
    Posts
    1,753
    Frame 1:

    x++;
    object._x=x;
    object._y=Math.sin(x);

    Frame 2:

    gotoAndPlay(1);

  3. #3
    Senior Member
    Join Date
    Jun 2000
    Posts
    911
    A good idea erlenmeyer but not complete. Flash's math objects like radians so you have to translate from degrees to radians. With that way if you only move three pixels to the right you already completed one entire period of the sine curve...so it will move very fast. If you remember that a radian is not an angle measure but merely the length of the arc of a unit circle you can find the conversion factor (between degrees and radians) to be PI/180. Here is something you may want to try instead:
    Code:
    onClipEvent (load)
    {
          x = 0;
          // used for translating between degrees and radians
          transe = Math.PI / 180;
    }
    
    onClipEvent (enterFrame)
    {
          this._x = x;
          this._y = Math.sin (x * trans);
    }
    But, that is a little bland and does not have any good features. Let's add a few things. First of all the above will make the movie clip oscillate around the y-axis at the top of the screen...it needs to have a reference point it should occilate around...we will call this "yctr". I will show you the script with all the new features after I talk about them.

    Anyway...the next thing you may want to control is the amplitude of the curve. This is done by multiplying the ratio from the sine function by a constant. This constant will represent the maxima and minima that the curve will reach. We will call it "amplitude" (how original).

    Also you may want to control the speed at which the curve is drawn. This can be done by taking bigger "steps" when incrementing the "x" variable or draw a couple of points each frame loop. I will use the last way in my script because it looks the best. That variable will be called "speed" and it will only represent how many points to draw each frame loop.

    Also how about having the object loop around when it goes off the screen. To do this you will only check if "x" goes greater than you stage width...if it does set "x" to be zero. The screen width will be put in a variable called "screen_x".

    Here is the completed script:
    Code:
    onClipEvent (load)
    {
          speed = 5;
          yctr = 200;
          amplitude = 50;
          screen_x = 550;
          x = 0;
          trans = Math.PI / 180;
    }
    
    onClipEvent (enterFrame)
    {
          if (x > screen_x)
                x = 0;
          for (var j = 0; j < speed; j++)
          {
                this._x = x;
                this._y = yctr - Math.sin (x * trans) * amplitude;
                x++;
          }
    }
    But, there are still more ways of manipulating sine curves (or any trigonometric curve for that matter). Go here to learn how to fine tune a sine curve.

    Good luck.

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