Search code examples
texturesspritehaxetexturepackerhaxeflixel

Using multiple textures in one image file


In my game I'm trying to use a sprite sheet with all my GUI textures in one image file. But I have no idea how to create a sprite using just part of an image resource defined by a rectangle.

OBS: I don't want to use Texture Packer, I have an alternative simpler free Texture Packer-like program that bundles texture in an image file and gives me the mapping in a json file. I can parse the json, but once I get the rect defining a single texture and the sheet image, I don't know what to do with them.


Solution

  • According to Beeblerox

    in the current version of flixel you can do it this way:

    var cached:CachedGraphics = FlxG.bitmap.add(Graphic); // where Graphic is the path to image in assets
    var textureRegion:TextureRegion = new TextureRegion(cached, rect.x, rect,y, rect.width, rect.height, 0, 0, rect.width, rect.height); // where rect is the rectangular area you want to load into sprite
    sprite.loadGraphic(textureRegion);
    

    in the next version which is in works it will be changed to:

    var imageFrame:ImageFrame = ImageFrame.fromRectangle("path/to/image", rect);
    sprite.frames = imageFrame;