Skip to content

Instantly share code, notes, and snippets.

@gamedevsam
Last active August 29, 2015 13:57
Show Gist options
  • Save gamedevsam/9750193 to your computer and use it in GitHub Desktop.
Save gamedevsam/9750193 to your computer and use it in GitHub Desktop.
Performance optimization for FlxTypedGroup.add()
public function add(Object:T, Unsafe:Bool = false):T
{
if (Object == null)
{
FlxG.log.warn("Cannot add a `null` object to a FlxGroup.");
return null;
}
// Use this when you're certain this group won't have any null or duplicate entries. Use only when maximum performance is needed.
if(Unsafe == true)
{
members.push(Object);
length++;
return Object;
}
// Don't bother adding an object twice.
if (FlxArrayUtil.indexOf(members, Object) >= 0)
{
return Object;
}
// First, look for a null entry where we can add the object.
var index:Int = getFirstNull();
if (index != -1)
{
members[index] = Object;
if (index >= length)
{
length = index + 1;
}
return Object;
}
// If the group is full, return the Object
if (maxSize > 0 && length >= maxSize)
{
return Object;
}
// If we made it this far, we need to add the object to the group.
members.push(Object);
length++;
return Object;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment