A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Reducing ball speed

  1. #1

    Reducing ball speed

    I have a ball which travels at a speed of 20, for example

    speed = 20;

    I would like it to eventually go to "0".
    So as the ball is moving it would slow down from 20,19,18 to 0.
    I have used a timer, but couldnt get the results, for example

    speed = (getTimer()-1)/1000;

    or

    t = getTimer()-1)/1000;
    _x = speed*t;

    Can anyone tell me if I'm on the right track here cos this is doing my head.
    I would appreciate any help thankyou...

  2. #2
    Feeling adventurous? T1ger's Avatar
    Join Date
    Mar 2004
    Posts
    850
    on each frame, do this:

    code:

    if (speed>0) {
    --speed;
    }



    it will decrease one speed-point per frame.

  3. #3
    Flash hates me. crashlanding's Avatar
    Join Date
    Nov 2003
    Location
    UK
    Posts
    439
    Originally posted by T1ger
    on each frame, do this:

    code:

    if (speed>0) {
    --speed;
    }



    it will decrease one speed-point per frame.
    or you could use setInterval, like this:
    code:

    var interval = setInterval(reduceSpeed, 500);
    function reduceSpeed(){
    if (speed>0) {
    --speed;
    }else{
    clearInterval(interval);
    }

    }





    I think thats all correct. haven't checked and im in a hurry so sorry if its wrong.
    "wen i found my gerbil dead my other gerbil was eating it i just cried and screamed"
    http://www.livescripts.net

    --------------------------------------------------------------------------------
    Last edited by some moderator : Today at 9:01 PM.

  4. #4
    ·»¤«· >flashl!ght<'s Avatar
    Join Date
    Jun 2003
    Posts
    746
    another way, a little closer to what you were thinking:[

    speed*=decay;
    _x+=speed;

    and you can defined decay onLoad as a number less than 1(and greater than zero).

    decay=.85;

    NOTE: this method will never actually reach 0, it will only approach infinitly close. so if theres any code which depends on speed==0 then you will need to add something like:

    if(Math.round(speed)==0)

    so once speed<.5 it gets treated like its zero
    >flashl!ght<
    All the normal names were taken.
    Ron Paul was right.

  5. #5
    Thanks for the replies and the code looks good but I was hoping to reduce the balls speed by using the timer, that way with what I'm trying to do will give me more flexibility. For example I have a speed set at 20 and it will eventually hit a wall. To bounce off the wall I would have to say "speed = -speed" after collision, but when I do this the speed changes to "-20" and it completely messes up my reducing speed to "0". If I could get the ball bouncing off the wall without it going to "-" then it would make it a whole lot easier from there...
    Last edited by Boz-Lee; 09-21-2004 at 09:45 PM.

  6. #6
    ·»¤«· >flashl!ght<'s Avatar
    Join Date
    Jun 2003
    Posts
    746
    if i understand, the code i presented still works under those conditions. it does not reduce speed in a linear fashion like the others. please re-read my post, see if it works. its how i would(and have many times) do it.
    >flashl!ght<
    All the normal names were taken.
    Ron Paul was right.

  7. #7
    ·»¤«· >flashl!ght<'s Avatar
    Join Date
    Jun 2003
    Posts
    746
    also, you could have an additional var called "direction" or "dir" which is always 1 or -1, for right and left respectively, then the movement code instead of

    _x+=speed;

    would be

    _x+=(dir*speed);

    then on bounce, you simple do
    code:
    if(dir){//this is saying if(dir==1) or if(dir==true) - same thing
    dir=-1;
    }else{
    dir=1;
    }

    >flashl!ght<
    All the normal names were taken.
    Ron Paul was right.

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