-
Number Datatype Inaccurate!
Okay - I've managed to become stumped, once again, for a very annoying problem.
I've made a class derived from a Sprite, thus containing "x" and "y" members, both of type "Number". But, what I guess is floating-point inaccuracies, the number chages slightly, causing little 'glitches'. Take the following:
X: 235.8 (Should be a whole number!)
Rounded off: 236
Using an INT to take 2 off, obviously gives 234
Put back into X (number): 234.3500000000000
So, how would one go about resolving this? The Number is inaccurate, can it be replaced with X or the Floating Point precision changed?
Cheers - once again :-)
Mark
(PS - I have Googled this, and everything else I've had problems with, plus tried numerous different variations and experiments! AS3 documentation online is minimal, at best! )
Last edited by MrSparky; 07-19-2007 at 11:54 AM.
-
half as fun, double the price
Can you be a little more specific? Where does the 234 int go to 234.35?
-
w = this.width - 2;
h = this.height - 2;
trace(this.name + " is dying ... [" + this.width + " , " + this.height + "]");
this.width = w;
this.height = h;
trace(this.name + " is dying ... [" + this.width + " , " + this.height + "]");
w and h are INT's, and this is an extended Sprite class.
I don't get this! If I put 234 (int) back into this.width it turns to 234.3500.....
BUT, if I create a sample application, it doesn't do it! So somewhere the FP position is changed, losing accuracy but gaining range (I'd assume, anyway ...)
-
half as fun, double the price
are you using the same shape in the sample?
-
Sorry, I don't quite understand what you mean? If you mean is that the actual code I'm using, it is! Made much more lengthly to ensure each step is working properly, but it's the code...
-
Furthert to the above then;
Could this be due to Sprite's width's and heights being somehow 'special' (that is, not just 'Numbers' but perhaps doing some kind of calculation/resizing when they're changed?)
My reasoning is that a sample application like this simply displays 0 all the time - width cannot be set, clearly ...
PHP Code:
package
{
import flash.display.Sprite;
public class test extends Sprite
{
public function test()
{
trace(this.width);
this.width = 200;
for(var i:int = 0; i < 150; i++)
{
this.width--;
trace(this.width)
}
}
}
}
Any help appreciated!
-
half as fun, double the price
yes, that's why I asked if you were using the same shape with your sample.
width and height are calculations based on scaleX and scaleY, so its possible to see discrepencies.
-
Thank you,
That explains a lot - if they're based on scales then discrepencies could be seen, indeed. That means I will use Scales instead of directly specifying widths/heights.
I'll let you know if there's further problems! :-) Cheers
-
Using ScaleX and ScaleY works beautifully - thank you - setting the width and height manually is bad!
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
|