dcsimg
A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Round number to nearest divisible number

  1. #1
    Senior Member ::bluemoth::'s Avatar
    Join Date
    Jun 2001
    Posts
    521

    Round number to nearest divisible number

    I have a draggable object that has a minimum (negative) and maximum (0) y position (-1200 to 0 for example).
    When dragged between these y positions, I want to snap the object via a tween, to nearest y positon divisible by minus 300 for example. So when I release the dragged object, it will either be -300, -600 or -900.

    I have tried a few functions I found online, but I am not getting this working.

    The nearest I found is as follows:
    Code:
    roundToNearest(roundTo:Number, value:Number):Number{
    return Math.round(value/roundTo)*roundTo;
    }
    I call it like:
    Code:
    roundToNearest(ob.y, -300);
    The idea is to be able to drag the object as at present, but if I let go of the object at -400 y position, then it will snap to -300.

    Anybody got any ideas?
    Last edited by ::bluemoth::; 03-08-2015 at 06:11 PM.

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,784
    Hi,

    Heres a clue and it should work throguh my tests

    aa = clip.y % 300;// or whatever the section gaps are
    bb = clip.y - aa;
    cc = clip tween current y to bb

    you will need to decipher which marker it is nearest to before tweening, example(-450 current y value)

  3. #3
    Senior Member ::bluemoth::'s Avatar
    Join Date
    Jun 2001
    Posts
    521
    Thanks fruitbeard, I will give it a try. The deciding to ceil or floor is the part that I was hoping had a dvisible function, but I guess some extra code will be needed as per usual.

    Quote Originally Posted by fruitbeard View Post
    Hi,

    Heres a clue and it should work throguh my tests

    aa = clip.y % 300;// or whatever the section gaps are
    bb = clip.y - aa;
    cc = clip tween current y to bb

    you will need to decipher which marker it is nearest to before tweening, example(-450 current y value)

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,784
    Hi,

    This should help you further along then, you will need to put your own drag feature to it.

    If you place a small MovieClip on the stage and name it clip
    PHP Code:
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    import fl.transitions.TweenEvent;

    var 
    section:Number 300;

    var 
    partA:Number;
    var 
    partB:Number;
    var 
    partC:Number section 2;

    var 
    doTween:Tween;

    function 
    doTweenOnRelease(releaseY:Number):void
    {
        
    partA releaseY section;

        if (
    partA partC)
        {
            
    partB releaseY partA;
        }
        else
        {
            
    partB releaseY + (section partA);
        }
        
    trace("Start y pos: " releaseY " - moving to " partB);

        
    doTween = new Tween(clip,"y",None.easeNone,releaseY,partB,1,true);
        
    doTween.addEventListener(TweenEvent.MOTION_FINISHendTween);
    }

    doTweenOnRelease(clip.y);

    function 
    endTween(e:TweenEvent):void
    {
        
    trace("End y: " clip.y);

    and perhaps test it more by changing the y value of clip

  5. #5
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,784
    Hi,

    Mess around with this, it is done with positive numbers, but I'm sure you can just output the number with a - infront of it.

    CS6 AS3

  6. #6
    Senior Member ::bluemoth::'s Avatar
    Join Date
    Jun 2001
    Posts
    521
    Thanks fruitbeard,

    I had a quick demo and it works great. I am going to look over your code closer to see how your division works (my math is not as good as I wished it was) and see where my previous attempts were going wrong. This will really help.

    Quote Originally Posted by fruitbeard View Post
    Hi,

    Mess around with this, it is done with positive numbers, but I'm sure you can just output the number with a - infront of it.

    CS6 AS3

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