Accessing Entities


(Jacob Albano) #1

One of the most common problems new ActionScript programmers come across when developing games is having variables accessible where they need to be and communicating information between game objects. In this tutorial, we’ll take a look at some of FlashPunk’s available tools for dealing with this problem.

Finding unique entities

Sometimes you need a particular entity to be available for all other entities to interact with. For example, you might want all instances of an Enemy class to constantly move towards the Player. While you could use a static variable to achieve this, using static variables is often a bad idea. Fortunately, FlashPunk has you covered.

The Entity class has a property called name that allows you to find specific instances of entities in the World with the getInstance() function.

public class Player extends Entity
{
	public function Player()
	{
		name = "player";
	}
}

Now, whenever you want to access your Player instance, you can do this:

var player = world.getInstance("player") as Player;

if (!player)
{
	// the player hasn't been added to the world yet!
}

If you’re going to be calling this function a lot, it’s a good idea to store the result in a variable to improve performance.

Finding multiple entities

FlashPunk has other ways of getting information from other Entities, and several useful functions to simplify the process of communicating between Entities. FlashPunk’s World class is pumped full of useful functions for fetching Entities by class, type, or layer.

For example, say you’re creating a scrolling shooter game and your character picks up a bomb powerup; when this powerup is collected, you want to destroy all the enemies in the World. This is where FlashPunk comes in handy! In your bomb object, you could use the following code to find them all:

public class Bomb extends Entity
{
	public function Bomb()
	{

	}

	public function explode():void
	{
		
		// First, we will create an empty array.
		var enemyList:Array = [];

		// Then, we populate the array with all existing Enemy objects!
		world.getClass(Enemy, enemyList);

		// Finally, we can loop through the array and call each Enemy's die() function.
		for each (var e:Enemy in enemyList)
		{
			e.die();
		}
		
	}
}

Here, we used World’s getClass() function and told it to fetch all Enemy entities in the world; by passing it the enemyList array, we’re telling the function to fill that array with all the Enemy objects so that we can use them in the for-each loop below. So as a result, when Bomb’s explode() function is called, it will then call the die() function of every Enemy!

You could easily add more conditions inside the for-each loop too, such as checking to make sure the enemy is close enough to the Bomb (to give the bomb an explosion radius), or checking it’s current state (maybe blinking enemies cannot be bombed, etc.) Regardless, this method does not always suffice, so you can use World’s other similar fetching functions as well:

These functions should provide lots of help when communicating Entities in your games, but if you ever have more involved or specific questions regarding this topic, don’t be afraid to drop by the help forums and post for assistance!

Original tutorial by Chevy Ray Johnston


Help us migrate and update old tutorials!
Accessing the x and y coordinates of another entity
What's the best way to determine the nearest enemy in a tower defense game?
(Blackstream) #2

Is world.getInstance() a Draknek addon to flashpunk? I don’t have it in my world.as

By the way, speaking of the later part of the tutorial, it’s worth noting that the getClass and the like functions don’t retrieve anything from the add lists that worlds have (that they then add said entities to at the end of the frame). Occasionally I’ll have code where I add something to a world and want to be able to immediately access it with said functions (generally, an object is added via script, and then accessed by reference from said script is the main way this happens), so I had to modify the functions a bit. But good to keep in mind either way cause it’ll drive you up a wall trying to access an object that doesn’t exist.


(Jacob Albano) #3

Looks like it is. Chevy’s repository doesn’t include it. I think it’s okay to leave it in since Draknek’s branch is being actively maintained, and it’s the one linked on the front page. Thanks for pointing that out, though!


(BlueZumbrellas) #4

Is it possible to do the reverse? ie. Send a variable to the current World?


(Jacob Albano) #5

Do you mean to have a variable like this?

world.player = new Player();

You can always do that by extending the World class and adding a member variable to it.


Accessing Entity!?