Radius < 1 on drawCircle draw unwanted results



  • Hi! I've been on and off on Haxe for the last year and half, I still feel like a total noob, and just recently I tried using HaxeFlixel, and I found it to be amazing! ;)

    But I'm having a small problem using some of FlxSpriteUtil on my FlxSprite object: I just found out that using a float value that's less than 1 for the radius' value in drawCircle() makes it draw a huge circle in the graphic I made, way larger than what I really need.

    I'm drawing a circle and changing its radius dynamically on my object and when it gets below zero it just expands :sweat_smile:

    Could somebody explain why this happens?

    This is the current object's code:

    import flixel.FlxG;
    import flixel.FlxSprite;
    import flixel.system.FlxAssets.FlxGraphicAsset;
    import flixel.util.FlxColor;
    
    using flixel.util.FlxSpriteUtil;
    
    class TransitionSpriteObject extends FlxSprite
    {
        public function new( ?X:Float = 0, ?Y:Float = 0 ) 
        {
            super( X, Y );
            makeGraphic( FlxG.width, FlxG.height, FlxColor.TRANSPARENT );
            drawCircle(
                32,
                32,
                .5, 
                FlxColor.WHITE
            );
        }
    }
    

    And this is what this outputs for me:
    A huge unwanted circle

    Thanks! :D

    P.s.: for any admins that read this, I've accidentally posted this same thing on "Dev Logs". I have deleted the topic, but could you please purge that one, if possible? :sweat_smile:



  • If you look into the code for drawCircle in FlxSpriteUtils you'll see

    if (Radius < 1) 
    		{
    			var minVal = Math.min(sprite.frameWidth, sprite.frameHeight);
    			Radius = (minVal / 2);
    		}
    

    This is why you'll see the circle getting bigger if your radius goes below zero. I assume it's there because trying to draw a circle on a bitmapdata with a radius less than 1 doesn't make sense and gives undesirable results.



  • Yeah, it's actually documented explicitly in the API.

    Radius Radius of the circle (makes sure the circle fully fits on the sprite's graphic if < 1, assuming and and y are centered)



  • If you want to change the "radius dynamically", you will want to draw a bigger circle (since a 1 px radius circle is just a pixel or a square) and then change the scale of the sprite or use setGraphicSize() and then updateHitbox(). For example:

    setGraphicSize(0.5); // keeps the ratio if you don't pass in the height
    updateHitbox();
    

    or

    scale.set(0.25, 0.25); // make the circle 4 times smaller
    updateHitbox();
    


  • Thanks, HappinessSam and DleanJeans. I'll try rearranging my code a bit. :)

    I kinda read the API docs quite fast, so maybe I jumped it a bit. But it still feels like it could be a bit more well explained, from a non-native english speaking POV. :confounded:

    After Dlean's practical explanation I kinda got what I have to do in here. I can't scale the graphic because I'm drawing and clearing things in it, so I guess I'll have to check the radius and not draw anything if < 1.

    Thanks again! :relieved:


Log in to reply
 

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.