A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Using the Round function in Action Script

  1. #1
    Senior Member zoranvedek's Avatar
    Join Date
    Aug 2001
    Location
    Wagoner OK
    Posts
    893
    How do I get an object in motion to go to the nearest 25(i.e. 25, 50, 75, 100, etc.)when it stops? I've been playing with the 'round' function(I am getting the objects postion 'pos' then setting its position and rounding it 'pos Round(25)'), but apparently I am doing it wrong, as the edit box I have monitoring its position shows it landing nowhere near the nearest '25'.



    Any help greatly appreciated.

    -Jason

  2. #2
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244

    round , floor and cieling

    Originally posted by zoranvedek
    How do I get an object in motion to go to the nearest 25(i.e. 25, 50, 75, 100, etc.)when it stops? I've been playing with the 'round' function(I am getting the objects postion 'pos' then setting its position and rounding it 'pos Round(25)'), but apparently I am doing it wrong, as the edit box I have monitoring its position shows it landing nowhere near the nearest '25'.
    Any help greatly appreciated.

    -Jason
    Round is used as Round(pos) which will round it to the nearest integer number only. Floor does the same but always rounds down and cieling always rounds up. This is not going to work for what you are trying to do.

    I'm sure we can work it out though. What is the largest multiple of 25 you'll need to check for?
    using javascript you could do this:
    && means AND
    || means OR
    -------------
    Code:
    if (pos<25 || pos>25 && pos<38){
     pos=25;
    }
    if (pos>25 && pos>38){
    pos = 50;
    }
    and so on



  3. #3
    Senior Member kusco's Avatar
    Join Date
    Nov 2001
    Posts
    681
    Hi guys,

    An easier way to do this is to use the following code...

    Code:
    pos = random(100);
    result = round(pos / 25) * 25;
    What we're doing here is dividing the position by 25 and rounding the result. We then multiply the result by 25 so that we can then determine the position of the nearest 25.

    Here's a link to see a working example of these two lines.
    http://www.flash-fx.com/3dfa/forum/s...rounding.movie

    Also, if you have a moving object that is moving to the left then use the floor() function if you want to make it stop neatly on the lowest nearest 25. Alternatively you can use the ceil() function to round to the next 25 if the object is moving to the right and you would like it to stop neatly there.

    eg. floor(45) will result in 25
    ceil(45) will result in 50

    HTH a little.

  4. #4
    Senior Member zoranvedek's Avatar
    Join Date
    Aug 2001
    Location
    Wagoner OK
    Posts
    893
    Tried using both solution's in the actionscript, neither worked(might have to use javascript for this one afterall).

    Heres the file:

    http://members.aol.com/zoranvedek/slottest.html

    and the source:

    http://members.aol.com/zoranvedek/slottest.movie

    The edit box is monitoring the third bar, there is currently nothing there to set it's postion. The bars position are given the handles of:

    b1=posa
    b2=posb
    b3=posc

    -Jason

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