Adobe Flash CS5.5 and AS3 – Rounding to specific decimal places

I put together this paddle ball game (break out).

I set the ball speed to .5

I increment the speed by .1 after each level.

I got these results:

  • 0.5
  • 0.6
  • 0.79999999
  • 0.89999999
  • etc.

What I wanted was:

  • 0.5
  • 0.6
  • 0.7
  • 0.8
  • 0.9
  • 1.0
  • 1.1
  • 1.2
  • etc.

Something changed in Action Script 3. I found this article at Adobe that helped me resolve my issue.

http://kb2.adobe.com/cps/155/tn_15542.html

Here is my code fix:

Before:

//Initialized
ballSpeed = 0.5

//subsequent calls **
ballSpeed += .1

After:

//Initialized
ballSpeed = 0.5

//subsequent calls **
ballSpeed = int((ballSpeed + .1)*100)/100;

Leave a Reply