Draw a box over all entities of a given type on the screen


(B6ka) #1

Hi, I am working on a platformer, and I am trying to implement some sort of robot-vision where all of the enemy entities visible on the screen are marked with a target sign (or some sort of box outline around it). This will be used to detect some of the enemies in the dark areas. Is there an easy way to get an access to all enemy entities to implement this feature? Thank you for any tips.


(Bora Kasap) #2

Thats the way to do it by “getClass()” function, you can access that from any world class. (or you can use “getType()” to pick all entities of given type)

private var all_enemies:Array = new Array(); //create an empty array
override public function update()
{
	super.update();
	FP.world.getClass(Enemy, all_enemies); //put all insances of "Enemy class" into the array in every frame.
}

Then you can use “for each” to access them all.

override public function render()
{
	super.render();
	for each(var enemy:Enemy in all_enemies)
	{
		Draw.circle(enemy.x, enemy.y);
        }
	all_enemies.length = 0; //clear array after robot vision in every frame
}

actually using a static array is so much better for performance issues, you shouldn’t remove and add all instances into the array again and again in every frame…


(Mike Evmm) #3

I’m just gonna be that guy and remind you that you should use world instead of FP.world


(Bora Kasap) #4

Of course, i wrote it FP.world because i don’t know where is he gonna run this code from. within world class, or within an entity? so, FP.world works same in both. I didn’t want it to be another trouble until he/she done this chapter :smiley:


(B6ka) #5

Thank you very much, works very well. I implemented this inside the Engine class (so I did need FP.world). I used it with getType() rather than getClass(). Imported utils.Draw. Also removed

 all_enemies.length = 0;

in render() as it was not required.

Edit: it is actually required to clear the array. Otherwise, I get slowdown.


(Bora Kasap) #6

Glad you solved.

But using Engine class for something like that may need unnecessary code when you create a Main Menu for your game. So, it’s better try to use this code in world class or in any entity instance.

You may create an entity called “RobotVision” and you can add this entity to the world when robot vision enabled, and remove it when robot vision disabled. So you can use “this.world” instead of “FP.world” by this way. Then you won’t need to worry about the actual world is really gameplay world or main menu or credits…


(B6ka) #7

@AlobarNon Thanks for the advice :), I am aware that this is kind of a sloppy practice and will probably rearrange the code later on.