Saving a FlxSprite as an image in Flash



  • Hey people, I'd like some help in saving a FlxSprite as a image (PNG/JPEG) to your desktop through flash.

    Some more details about the project: it's a little art game where the background gets stamped with a brush sprite every frame the spacebar is pressed. So I was wondering if it was possibly to save an image through something like the pixels variable of the sprite with ByteArrays or something. I haven't messed around with that stuff too much so help would be appreciated!



  • not sure if this works on flash but this is how to do it on cpp:

    //create a BitmapData the size of the screen
    var bmp:BitmapData = new BitmapData(FlxG.width, FlxG.height, true, 0x0);
    bmp.draw(FlxG.camera.canvas); //I personally use my game camera here so the screenshot is HUDless
    var b:ByteArray = bmp.encode("png", 1);
    
    //save it to disk (I use documents since you don't need special permissions for that)
    var fo:FileOutput = sys.io.File.write(Path.join([SystemPath.documentsDirectory, "yourScreenshot"]) + ".png", true);
    fo.writeBytes(b, 0, b.length);
    fo.close();
    


  • I think I tried something similar earlier. When I imported the FileOutput, it didn't let me access sys through flash.


  • administrators

    @ninja_muffin99 Yep, Flash doesn't have direct file system access. You'd have to use a save dialog like FlxScreenGrab does:

    https://github.com/HaxeFlixel/flixel-addons/blob/2.5.0/flixel/addons/plugin/screengrab/FlxScreenGrab.hx#L177-L178



  • Thanks! Here's the simple thing that I did for anyone interested. If it breaks for me or something I'll update this.

    //This runs when a button is pressed
    var png:ByteArray;
    png = PNGEncoder.encode(_bg.pixels);
    			
    var file:FileReference = new FileReference();
    file.save(png, "Art.png");
    
    

Log in to reply
 

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