Skip to content

Instantly share code, notes, and snippets.

@red-hara
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save red-hara/81d8786353343e33da7a to your computer and use it in GitHub Desktop.
Save red-hara/81d8786353343e33da7a to your computer and use it in GitHub Desktop.
Class for creating bad outline around FlxSprite.
package;
import flixel.FlxSprite;
import flixel.system.layer.frames.FlxFrame;
/**
* Class for creating bad outline around FlxSprite.
*
* @author red__hara
*/
class BadOutline extends FlxSprite
{
/**
* FlxSprite to draw outline around.
*/
public var target:FlxSprite;
/**
* Color of outline.
*/
public var outlineColor:Int;
/**
* Used for checking frame update.
*/
public var lastFrame:FlxFrame;
/**
* Creates a BadOutline around specified sprite with specified color.
*
* @param Target The FlxSprite to draw outline around
* @param Color Color of outline.
*/
public function new(Target:FlxSprite, ?Color:Int=0xffffffff)
{
super();
target = Target;
outlineColor = Color;
makeGraphic(target.frameWidth + 2, target.frameHeight + 2, 0);
updateFrame();
allowCollisions = 0;
}
override public function update():Void
{
x = target.x;
y = target.y;
offset.x = target.offset.x + 1;
offset.y = target.offset.y + 1;
if (lastFrame != target.frame)
{
updateFrame();
}
}
public function updateFrame():Void
{
lastFrame = target.frame;
var i:Int = 0;
var j:Int = 0;
cachedGraphics.bitmap.lock();
while (i < frameWidth)
{
j = 0;
while (j < frameHeight)
{
cachedGraphics.bitmap.setPixel32(i, j, 0);
j++;
}
i++;
}
i = 0;
j = 0;
while (i < target.frameWidth)
{
j = 0;
while (j < target.frameHeight)
{
if (target.frame.getBitmap().getPixel(i, j) > 0)
{
surround(i + 1, j + 1);
}
j++;
}
i++;
}
cachedGraphics.bitmap.unlock();
}
public function surround(I:Int, J:Int):Void
{
cachedGraphics.bitmap.setPixel32(I - 1, J - 1, outlineColor);
cachedGraphics.bitmap.setPixel32(I - 1, J, outlineColor);
cachedGraphics.bitmap.setPixel32(I - 1, J + 1, outlineColor);
cachedGraphics.bitmap.setPixel32(I, J - 1, outlineColor);
cachedGraphics.bitmap.setPixel32(I, J, outlineColor);
cachedGraphics.bitmap.setPixel32(I, J + 1, outlineColor);
cachedGraphics.bitmap.setPixel32(I + 1, J - 1, outlineColor);
cachedGraphics.bitmap.setPixel32(I + 1, J, outlineColor);
cachedGraphics.bitmap.setPixel32(I + 1, J + 1, outlineColor);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment