Skip to content

Commit

Permalink
FlxSpriteGroup: add insert() (#2020)
Browse files Browse the repository at this point in the history
  • Loading branch information
MSGhero authored and Gama11 committed Jan 11, 2017
1 parent 402b34f commit 4ca1eae
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion flixel/group/FlxSpriteGroup.hx
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,38 @@ class FlxTypedSpriteGroup<T:FlxSprite> extends FlxSprite
* @return The same object that was passed in.
*/
public function add(Sprite:T):T
{
preAdd(Sprite);
return group.add(Sprite);
}

/**
* Inserts a new `FlxSprite` subclass to the group at the specified position.
*
* @param Position The position that the new sprite or sprite group should be inserted at.
* @param Sprite The sprite or sprite group you want to insert into the group.
* @return The same object that was passed in.
*/
public function insert(Position:Int, Sprite:T):T
{
preAdd(Sprite);
return group.insert(Position, Sprite);
}

/**
* Adjusts the position and other properties of the soon-to-be child of this sprite group.
* Private helper to avoid duplicate code in `add()` and `insert()`.
*
* @param Sprite The sprite or sprite group that is about to be added or inserted into the group.
*/
private function preAdd(Sprite:T):Void
{
var sprite:FlxSprite = cast Sprite;
sprite.x += x;
sprite.y += y;
sprite.alpha *= alpha;
sprite.scrollFactor.copyFrom(scrollFactor);
sprite.cameras = _cameras; // _cameras instead of cameras because get_cameras() will not return null
return group.add(Sprite);
}

/**
Expand Down

0 comments on commit 4ca1eae

Please sign in to comment.