-
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...
-
Feeling adventurous?
on each frame, do this:
code:
if (speed>0) {
--speed;
}
it will decrease one speed-point per frame.
-
Flash hates me.
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.
-
·»¤«·
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.
-
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.
-
·»¤«·
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.
-
·»¤«·
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 docode: 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|