0

So I have an issue with Flash's Graphics class. Consider this code (I'm using Flixel):

public static function CreateSolid(dimensions:FlxPoint, color:uint): FlxSprite {
        var sprite:FlxSprite = new FlxSprite();
        sprite.makeGraphic(dimensions.x, dimensions.y, 0xffffffff);

        var gfx:Graphics = FlxG.flashGfx;
        gfx.clear();

        gfx.beginFill(color, 1);
        gfx.drawRect(0, 0, sprite.frameWidth, sprite.frameHeight);
        gfx.endFill();

        sprite.pixels.draw(FlxG.flashGfxSprite);
        sprite.dirty = true;

        return sprite;
    }

All the code does is return a new FlxSprite object that is solidly colored according the the input argument. This of course isn't the best way to do it, it's just simplified from another piece of code I have.

If I use this method to create a solid red 50x50 square, it works fine:

a = CreateSolid(new FlxPoint(50, 50), 0xff0000);
//draw a

Now, if I create two squares side by side, one red and one green, in this order:

a = CreateSolid(new FlxPoint(50, 50), 0xff0000);
b = CreateSolid(new FlxPoint(50, 50), 0x00ff00);
b.x += 50
//draw a & b

You'd expect the two boxes to be colored correctly. However, to me, they are both green. This was strange to me. Even stranger: if I make one square a different size (even by a little bit):

a = CreateSolid(new FlxPoint(50, 50), 0xff0000);
b = CreateSolid(new FlxPoint(51, 50), 0x00ff00);
b.x += 50
//draw a & b

The colors are drawn correctly. Is there an explanation to this? And how do I color them correctly?

Thanks!

kjin
  • 1
  • 1
  • Is the `return Sprite;` part correct or is it a typo? It should be `sprite`. – Fernando Bevilacqua Oct 28 '14 at 16:19
  • Your return is not the right case. That aside, I am not familiar with Flixel, but I am wondering if there is an issue with creating the Sprite within the static function. Try creating an empty FlxSprite first and passing it into the function as a third argument. Draw to it. Another test would be to try putting using addChild before attempting to create another one. Perhaps garbage collection is messing things up (oddly). – C. Parcell Oct 28 '14 at 16:22
  • Yes, that was a typo, I lowercased some vars in my code when pasting it here but missed the return statement. – kjin Oct 28 '14 at 19:00
  • Thanks for the responses, I will give your suggestions a try! – kjin Oct 28 '14 at 19:00

0 Answers0