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.
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.
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:
getClass()
→ gets all Entities by ClassgetType()
→ gets all Entities by Type (faster than getClass!)getLayer()
→ gets all Entities by layerclassFirst()
→ returns the first Entity of the Class foundtypeFirst()
→ returns the first Entity of the type foundlayerFirst()
→ returns the first Entity on the layernearestToEntity()
→ returns the Entity of a type nearest to anothernearestToPoint()
→ returns the Entity nearest to the positionnearestToRect()
→ returns the Entity nearest to the rectangular areaThese 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 community and ask for assistance!