0

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.

MouseCaneta
  • 135
  • 1
  • 8

2 Answers2

1

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;
MouseCaneta
  • 135
  • 1
  • 8
0

Basically you need to :

  • create a new BitmapData object.
  • call copyPixels(sourceBitmapData:BitmapData, sourceRect:Rectangle, destPoint:Point) on this object, where sourceBitmapData is your loaded spritesheet BitmapData.
  • build a new display.flash.Bitmap object from this BitmapData.
  • call addChild(bm) where bm is the bitmap you just created to display it in the container you want.

See here :

the_yellow_logo
  • 675
  • 5
  • 13