1

I find this strange:

    trace("typeof: ",typeof(sprite.x));
    trace("getClassName: ,getQualifiedClassName(sprite.x));
// Output:  typeof:  number
            getClassName:  int  

Which makes a sprite's property "x" a number? ...or an integer? ...neither? ...both???

So this is what happens:

    private function spriteTest():void {
        sprite = new Sprite;
        sprite.graphics.beginFill(0xff0000, 1);
        sprite.graphics.drawCircle(50, 50, 25);
        sprite.graphics.endFill();
        sprite.x = 0; sprite.y = 0; // default position
        trace("typeof: ",typeof(sprite.x));
        trace("getClassName: ",getQualifiedClassName(sprite.x));
        stage.addChild(sprite);
        sprite.addEventListener(MouseEvent.CLICK, moveit);

    }

    private function moveit(e:MouseEvent):void {
        var spriteTempX:Number = sprite.x;
        for (var i:int = 0; i < 1000; i++) {
            sprite.x += .1;
            spriteTempX += .1;
            trace(i,sprite.x, spriteTempX);
        }
    }
// Output: i       sprite.x    spriteTempX
            0        0.1         0.1
            1        0.2         0.2
            2        0.3         0.30000000000000004
            3        0.4         0.4
            4        0.5         0.5
            5        0.6         0.6
            6        0.7         0.7
            7        0.75        0.7999999999999999
            8        0.85        0.8999999999999999
            9        0.95        0.9999999999999999
            ...
            997      92.6        99.7999999999986
            998      92.65       99.8999999999986
            999      92.75       99.9999999999986

So now, my sprite has moved only 92 (93?) pixels, instead of 99 (100?) pixels.

I can get around this, band-aid-style, by assigning my sprite.x value to spriteTempX's value, but I'm wondering if there is a more elegant solution.

What's with the .x property of display objects and is there a more elegant way to accomplish what I'm trying to accomplish?

Chowzen
  • 321
  • 3
  • 12
  • 2
    All graphic measurements in Flash (x, y, width, height, etc) are actually integer values and go in twixels, which is 1/20 of pixel. So you assign some value in pixels and Flash recalculates it into twixels, and it is the same the other way. That put some weirdness now and then and confuse people. The **Number** type has 15 digits of precision and also fail just like that. – Organis Feb 22 '18 at 21:49
  • 1
    That "band-aid" solution is actually the right way to do it if you need this level of accuracy. You can't rely on a display object to store mathematically precise information, as Organis explained why you lose a lot of resolution. The difference between `typeof` and `getQualifiedClassName` is because `typeof` is based on ECMAScript's low fidelity `typeof` operator (basically from JavaScript world), whereas AS3 has a static type system and specific reflection APIs that give you much more useful information. I never use `typeof` in AS3. – Aaron Beall Feb 23 '18 at 18:26
  • @Aaron So, this *is* the "elegant" solution, then, huh! LOL at the thought that less than 8% error is being euphemized as a "level of accuracy." :) – Chowzen Feb 23 '18 at 19:59

0 Answers0