Multiple image layers on an enity


(Elias) #1

Hello there!

I got a level entity and 3 different images (on the entity), that made out of tiles. However the images are split into the background, solid image (that the player collide with) and image tiles (that you can run into). The problem is that i want everyone of the images to be on a different layers. Is that possible to achieve only on a single Entity, or should i have 3 different entities?


(Ultima2876) #2

You should have 3 different entities. If you like though, you can have one controller entity that creates and manages them:

//MAIN ENTITY VARS
private var _subEntities: Vector.<Entity> = new Vector.<Entity>();

//MAIN ENTITY BEGIN
public override function begin(): void
{
  var entity: Entity;
  if(world) { entity = world.create(SubEntityName); }
  if(entity)
  {
    entity.layer = blah;
    _subEntities.push(entity);
  }

  if(world) { entity = world.create(SubEntityName); }
  if(entity)
  {
    entity.visible = false;
    entity.layer = blah;
    _subEntities.push(entity);
  }
}

public override function removed(): void
{
  //recycle all subentities
  for(int i = 0; i < _subEntities.length; i++)
  {
    if(_subEntities[i])
    {
      _subEntities[i].visible = false;
      world.recycle(_subEntities[i]);
    }
  }
}

(Elias) #3

Thanks Ultima! Any disadvantages if i have 3 entities instead of 1?


(Ultima2876) #4

Mostly just management and making sure the entities are removed appropriately as above. If it’s something you’ll have a lot of then obviously having 3 times the amount of update calls and such is eventually going to take its toll, but there’s no other way around it if you want them on separate layers. The overhead is pretty negligible for a reasonable amount of entities (less than 1000, say) though; I’ve used this technique often and in many games (you also need multiple entities if, for example, you want multiple hitboxes for headshots or whatever) :slight_smile: