-2

In my game, the speed of the ball increases by a certain number every 15 seconds when the game has started. Right now I have an int which is the speed of the ball and the the method:

ballSpeed += 1;

This works but when I decrease the amount that the ball speed is increased by, which is going to be less than 1, for some reason it doesn't work.

For example, if I do:

ballSpeed += .9

, for some reason it doesn't work.

I thought that I just couldn't see the difference because it was too small but when i have .99 it still doesn't work and I would be able to tell the difference with that number.

Any answer is greatly appreciated.

Thanks

4

3 回答 3

1

You're using integers? .9 is not an integer value so you'd have to use a different datatype (eg:float) to do so.

于 2013-11-12T01:05:24.320 回答
0

因为ballSpeed是整数,所以只能取整数。所以使用代码

ballSpeed += .9;

将导致它向下舍入,最终结果ballSpeed根本不会改变。所以 1 是您可以做出的最小更改。

于 2013-11-12T01:05:35.493 回答
0

您不能使用 int 来保存十进制数。尝试使用浮点数而不是整数。

于 2013-11-12T01:07:07.923 回答