A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: actionscript movement with easing

  1. #1
    Senior Member
    Join Date
    Mar 2002
    Posts
    270
    Okay, I have a fla with an array on the main timeline that contians values for the _x position of a movieclip on the stage. I have buttons that I want the user to click on that will then check the _x of the movieclip and move it to a new value from the array. So the actionscript first needs to know if it needs to move left or right then needs to do the math to move that movie clip across the stage. Can someone help me with the script for this? I also want the movie clip to slow as it nears it's stopping point.

    Thanks

  2. #2
    Senior Member
    Join Date
    Feb 2001
    Posts
    1,835
    hi,

    for a simple start, you can do something like this:

    (assumes _root.posArray is array with positions, in this form:

    Code:
    posArray = new Array();
    posArray[0] = {x: 100, y: 200};
    posArray[1] = {x: 50, y: 250};
    Then you can put this on your movie clip:

    Code:
    onClipEvent(load)
    {
       currPos = 0; // current position
       ease = 4; // easing variable
       bound = 0.2; // for checking if arrived
    }
    onClipEvent(enterFrame)
    {
       dx = _root.posArray[currPos].x - _x;
       dy = _root.posArray[currPos].y - _y;
    
       // move
       _x += dx/ease;
       _y += dy/ease;
    
       // check if arrived  
       if(Math.abs(dx) <= bound && Math.abs(dy) <= bound)
       {
          // go to next point, loop round at end
          currPos = (currPos + 1) % _root.posArray.length;
       }
    }
    That should do it...

    - n.

  3. #3
    Senior Member
    Join Date
    Mar 2002
    Posts
    270
    how can I trigger that motion from a button then?

  4. #4
    Senior Member
    Join Date
    Mar 2002
    Posts
    270
    this is my array:

    // scrollXPoss = new Array("0", "760", "1520", "2280", "3040", "3800", "4560", "5320", "6080", "6840");

    it represents _x values for the movieClip that I want o move.

    So, on a click of a button I want to check the current position (which could be 0-9 in the array) and move the movieClip to a new position from the array.

    I think it would look something like this

    // on (release) {
    // curX = _parent.scrollingMovie._x;
    // newX = _parent.scrollXPoss[4];
    // I'm not sure what to do from here
    // }

    how can I move that movieClip (scrollingMovie) to the new position and slow it down as it approaches the new _x position?

    Thanks

  5. #5
    Senior Member
    Join Date
    Feb 2001
    Posts
    1,835
    hi,

    with your array, just change one line:

    dx = _root.scrollXPos[currPos] - _x;

    and delete all code with references to _y.

    This will continue moving, without the button control.
    To use the button, add some lines:

    Code:
    // in 'onClipEvent(load)'
    anim = false;
    
    // in onClipEvent(enterFrame) (as first 2 lines)
    if(!anim)
       return;
    
    // on your button (if inside the same movie clip)
    on(release)
    {
       anim = true;
    }
    
    // on your button (if outside of movie clip)
    on(release)
    {
       _root.myMCName.anim = true;
    }
    .. - n.

  6. #6
    Senior Member
    Join Date
    Mar 2002
    Posts
    270
    sorry to keep bothering you, but here's what I have right now and it's not working : (

    on my movieclip:

    // onClipEvent(load)
    // {
    // currPos = 0; // current position
    // ease = 4; // easing variable
    // bound = 0.2; // for checking if arrived
    // anim = false;
    // }
    // onClipEvent(enterFrame)
    // {
    // if(!anim)
    // return;
    // dx = _root.scrollXPoss[currPos].x - _x;
    // move
    // _x += dx/ease;
    // check if arrived
    // if(Math.abs(dx) <= bound)
    // {
    // go to next point, loop round at end
    // currPos = (currPos + 1) % _root.scrollXPoss.length;
    // }
    // }

    my array on the maintimeline:

    // scrollXPoss = new Array("0", "760", "1520", "2280", "3040", "3800", "4560", "5320", "6080", "6840");

    on my button in a seperate movieclip:

    // on(release)
    // {
    // _root.scrollingMovie.anim = true;
    // }

    What am I doing wrong?

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