Skip to content

Instantly share code, notes, and snippets.

@red-hara
Last active August 29, 2015 14:13
Show Gist options
  • Save red-hara/c870374403bf494b0cc5 to your computer and use it in GitHub Desktop.
Save red-hara/c870374403bf494b0cc5 to your computer and use it in GitHub Desktop.
Outline for FlxSprite.
package;
import flixel.FlxSprite;
import flixel.system.layer.frames.FlxFrame;
/**
* Class for creating outline around FlxSprite.
*
* @author red__hara
*/
class Outline extends FlxSprite
{
public var target:FlxSprite;
public var outlineColor:Int;
private var _lastFrame:FlxFrame;
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
{
putPixel(I - 1, J - 1, outlineColor);
putPixel(I - 1, J, outlineColor);
putPixel(I - 1, J + 1, outlineColor);
putPixel(I, J - 1, outlineColor);
putPixel(I, J, outlineColor);
putPixel(I, J + 1, outlineColor);
putPixel(I + 1, J - 1, outlineColor);
putPixel(I + 1, J, outlineColor);
putPixel(I + 1, J + 1, outlineColor);
}
public function putPixel(I:Int, J:Int, Color:Int):Void
{
if (target.frame.getBitmap().getPixel(I - 1, J - 1) == 0)
{
cachedGraphics.bitmap.setPixel32(I, J, Color);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment