Skip to content

Instantly share code, notes, and snippets.

@kewp
Created April 10, 2015 09:27
Show Gist options
  • Save kewp/a151572563eadfbd5314 to your computer and use it in GitHub Desktop.
Save kewp/a151572563eadfbd5314 to your computer and use it in GitHub Desktop.
/**
* Load an image from an embedded graphic file.
*
* @param Graphic The image you want to use.
* @param Animated Whether the Graphic parameter is a single sprite or a row of sprites.
* @param Width Optional, specify the width of your sprite (helps FlxSprite figure out what to do with non-square sprites or sprite sheets).
* @param Height Optional, specify the height of your sprite (helps FlxSprite figure out what to do with non-square sprites or sprite sheets).
* @param Unique Optional, whether the graphic should be a unique instance in the graphics cache. Default is false.
* @param Key Optional, set this parameter if you're loading BitmapData.
* @return This FlxSprite instance (nice for chaining stuff together, if you're into that).
*/
public function loadGraphic(Graphic:Dynamic, Animated:Bool = false, Width:Int = 0, Height:Int = 0, Unique:Bool = false, ?Key:String):FlxSprite
{
bakedRotationAngle = 0;
cachedGraphics = FlxG.bitmap.add(Graphic, Unique, Key);
if (Width == 0)
{
Width = (Animated == true) ? cachedGraphics.bitmap.height : cachedGraphics.bitmap.width;
Width = (Width > cachedGraphics.bitmap.width) ? cachedGraphics.bitmap.width : Width;
}
if (Height == 0)
{
Height = (Animated == true) ? Width : cachedGraphics.bitmap.height;
Height = (Height > cachedGraphics.bitmap.height) ? cachedGraphics.bitmap.height : Height;
}
if (!Std.is(Graphic, TextureRegion))
{
region = new Region(0, 0, Width, Height);
region.width = cachedGraphics.bitmap.width;
region.height = cachedGraphics.bitmap.height;
}
else
{
region = cast(Graphic, TextureRegion).region.clone();
if (region.tileWidth > 0)
Width = region.tileWidth;
else
region.tileWidth = region.width;
if (region.tileHeight > 0)
Height = region.tileWidth;
else
region.tileHeight = region.height;
}
width = frameWidth = Width;
height = frameHeight = Height;
animation.destroyAnimations();
updateFrameData();
resetHelpers();
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment