A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: point A to point B movement using action script

  1. #1
    Senior Member
    Join Date
    Feb 2001
    Posts
    185
    Hello,

    I don't know if i'm just stupid or what but i can't figure out how to get an object to move from point A to point B using action script (so that there is acceleration)

    this is what my code looks like now... to you it prob. looks like i pulled it out of my butt... (i tried to make an MC thats the action to later drop in onto my object that i want to move... but it just moves the whole canvas.

    frame1.
    shapeX = _parent._x;
    shapeY = _parent._y;
    targetX = _root._x;
    targetY = _root._y;

    frame2.
    distX = targetX-shapeX;
    distY = targetY-shapeY;
    shapeX = Math.round (shapeX+(distX));
    shapeY = Math.round (shapeY+(distY));
    _parent._x = shapeX;
    _parent._y = shapeY;

    frame3.
    stop()


    suggestions?
    examples?

    Thanks in advance.

  2. #2
    YH Jelly Llama Jockey defuzz's Avatar
    Join Date
    May 2001
    Posts
    464
    not sure I understand your example, her is how I would do something similar.



    new_x=300
    new_y=300
    these are hard coded but could be set to whatever.

    now_x=this._x
    now_y=this._y
    where the movie is now

    diff_x=now_x - new_x
    diff_y=now_y - new_y
    works out how far away

    if (diff_x >1){
    this._x=this._x- (diff_x /3)
    diff_x=now_x - new_x
    }
    if (diff_y >1){
    this._y=this._y- (diff_y /3)
    diff_y=now_y - new_y
    }
    sets new position to be one third of the distance it was before (the 3 can be chaged, the higher the number the quicker the initial move)

    This is all off the top of my head though so it may not work!!!! Give it a go and tell me how it goes.






  3. #3
    Junior Member
    Join Date
    Jun 2001
    Posts
    26
    ok, I'll try to explain it in my language, you need two frames and you got an instance named "film" in the main timelime anywhere and you want to move it to 800 / 600 (x/y) with increasing speed:

    // frame 1
    movex = int((100/(800-_root.film._x))*10);
    movey = int((100/(600-_root.film._y))*10);
    if (((_root.film._x + movex) > 800) or ((_root.film._y + movey) > 600)) {
    movex = 800 - _root.film._x;
    movey = 800 - _root.film._y;
    }
    _root.film._x += movex;
    _root.film._y += movey;

    // frame 2
    gotoAndPlay (//Number of the previous frame);

    good luck!
    .t.

  4. #4
    Senior Member
    Join Date
    Feb 2001
    Posts
    185
    thanks a lot both of you...

    this gives me a 'little more' <g> of an idea as to how this action script stuff works.. it can get confusing.

    both methods worked..

    hey defuzz how come it seems like the 'thing' is jumping instead of gliding... just curious i'm going to try to fix it myself. I can usually rip fla's appart quite successfully and change them but when it comes to writing the code from scratch i don't think i have enogh time


    thanks again, i appreciate the help

  5. #5
    YH Jelly Llama Jockey defuzz's Avatar
    Join Date
    May 2001
    Posts
    464
    my code divides te distance by three and moves a third of the way along each time so if it is quite a big distance the first jump may be quite a big one, if you change the 3 to a higher number the jump is smaller but overall the move takes longer. (not the other way round as I said in my first post)

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