A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Number Datatype Inaccurate!

  1. #1
    Member
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    33

    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.

  2. #2
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    Can you be a little more specific? Where does the 234 int go to 234.35?

  3. #3
    Member
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    33
    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 ...)

  4. #4
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    are you using the same shape in the sample?

  5. #5
    Member
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    33
    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...

  6. #6
    Member
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    33
    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 0150i++)
                {
                    
    this.width--;
                    
    trace(this.width)
                }
            }
        }


    Any help appreciated!

  7. #7
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    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.

  8. #8
    Member
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    33
    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

  9. #9
    Member
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    33
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center