A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: making an object move in a circle

  1. #1
    Junior Member
    Join Date
    Apr 2004
    Posts
    26

    making an object move in a circle

    hello
    does anybody know how to make an object moving in a smooth circle?

    I guess its not much of code but I just dont get there...

    thanks in advance

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Sin and Cosine are your friends.

    Basic circlular motion is given by the following formula:

    x = cx + Math.sin(time)*rad;
    y = cy + Math.cos(time)*rad;

    cx,cy are the center of the circle and

    rad is the radius of the circle.

    As time goes from 0 to 2*PI (or multiples thereof), the x,y values will describe a complete circle.


    Here is an example of using this formula. Attach this script to the object you wish to move.

    Code:
    onClipEvent(load)
    {
      cx = Stage.width/2; // coords of center of circle (center of screen, in this case)
      cy = Stage.height/2;
    
      rad = 100; // radius of circle
    
      speed = 6; // speed of travel (seconds to make a complete circuit)
      speedScale = (0.001*2*Math.PI)/speed;
    }
    
    onClipEvent(enterFrame)
    {
      var angle = getTimer()*speedScale;
      this._x = cx + Math.sin(angle)*rad;
      this._y = cy + Math.cos(angle)*rad;
    }
    For an example of using sin and cosine to move text in a curvy pattern, see this thread:

    text effect
    Last edited by jbum; 04-14-2004 at 06:26 PM.

  3. #3
    Senior Member
    Join Date
    Nov 2003
    Location
    Nevada
    Posts
    138
    hey jim,

    do you have or know where i can find motion for elipses?

    thanks,
    brint

  4. #4
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    Hi,

    you could scale the values of the sin and cos parts in the equations,

    eg,

    this._x = cx + 1.5 * Math.sin(angle)*rad;
    this._y = cy + Math.cos(angle)*rad;

    here the clip would move further from the origin in the x direction than it does in the y direction

  5. #5
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    To get an ellipse, use separate radii for the horizontal and vertical coordinates.

    radA = 100;
    radB = 50;
    .
    .
    .

    this._x = cx + Math.sin(angle)*radA;
    this._y = cy + Math.cos(angle)*radB;

    - Jim

    Edit: Whoops, crossposted with catbert. We're basically saying the same thing. Catbert is defining radA in terms of it's relationship to radB (aspect ratio = 1.5).
    Last edited by jbum; 04-14-2004 at 06:40 PM.

  6. #6
    Senior Member
    Join Date
    Nov 2003
    Location
    Nevada
    Posts
    138
    thanks guys that was 2 ez

  7. #7
    Junior Member
    Join Date
    Apr 2004
    Posts
    26
    hey thanks a million for the help. it really IS kinda easy

  8. #8
    Bmcc*81 bmcc81's Avatar
    Join Date
    Jul 2007
    Location
    Montreal
    Posts
    299

    trying to put my icons in a circle

    Hi,

    I'm trying to put my icons in a circle. I'm using this code for a straight line.

    Code:
    var spacing = 10 + count * 113.15 ;//left spacing +i*item._width
    
    var t = home.menuHolder.attachMovie("item","item"+count, count+16, {_x:spacing, _y:0, _xscale:75, _yscale:75, _alpha:0});

    Is there away to get this into a circle?
    Bmcc81
    Flash Designer / Web Developer
    -----------------------------------
    http://www.webinkgraphics.com

  9. #9
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    Here is a tutorial I put together about this:

    AS3 Circular Movement

  10. #10
    Bmcc*81 bmcc81's Avatar
    Join Date
    Jul 2007
    Location
    Montreal
    Posts
    299
    very cool!
    Bmcc81
    Flash Designer / Web Developer
    -----------------------------------
    http://www.webinkgraphics.com

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