Vectors are so easy to use it’s not even funny. They’re basically arrays where you specify the type of variable that will be contained in them:
private var _myVector: Vector.<Entity> = new Vector.<Entity>;
From there they basically work exactly the same as an array.
for(var i: int = 0; i < _myVector.length; i++)
{
if(_myVector[i] != null)
{
_myVector[i].x = 50;
_myVector[i].y = 50;
}
}
_myVector.length = 0; //fast clear of the vector (same as with arrays)
As for recycling entities, destroying them as they go offscreen etc… it might be better to make them inactive and invisible if they’re offscreen. If you set myEntity.active = false and myEntity.visible = false, they will not be updated or drawn at all. They will still be contained within the entity list so there will be some overhead just for having them there, but it will be a very small amount of overhead compared to what you have currently.
If you really wanted to recycle and recreate them you could keep a Dictionary of entities in memory. Before you recycle an entity, update its entry in the Dictionary (keyed by a unique reference for each entity; we’ll call this entityId) with any properties that are important (eg x position, y position, state, whatever is relevant to your game). Then when you recreate an entity with World.create, check if it has an entry in that Dictionary and if it finds it, copy the data back. Some helpful code for this:
//Game class or wherever!
private var _entityList: Dictionary = new Dictionary; //create a dictionary
_entityList[myEntity.entityId] = new GameEntity(myEntity); //you probably want to use a custom base class for all your entities that has an entityId variable. Make the GameEntity constructor clone whatever is passed to it!
//GameEntity.as
//in your GameEntity class do something like this
private static var _currentId: uint = 0;
private var _entityId: uint;
//constructor
public function GameEntity(clone: GameEntity = null): void
{
if(clone == null)
{
//we're not cloning an entity this time, this is a new entity. Give it a unique ID!
_entityId = ++_currentId; //increment static current ID so they're always unique
}
else
{
//copy all relevant variables here!
}
}
//we want a getter for entityId!
public function get entityId(): uint
{
return _entityId;
}