In games, there are several ways to move enemies around the playable area. In Chapter 1, Game Plan – Creating Basic Gameplay, we discussed a simple way of using the x and y coordinates. In the previous recipe, we discussed setting paths, which can also be used to shuttle enemies around the screen for your player to avoid or destroy. These are both good options, depending on what you want for your game, but what about enemies that can actually navigate a level, avoid obstacles, and pursue the player? Let's add some depth to a top-down game by creating a slightly more complex enemy AI.
First, you'll need a character object called obj_player
that can move in four directions (with collision detection), and an enemy object called obj_enemy
. Once again, I've animated the player character using the 3/4 top-down sprites from previous recipes. For the enemy sprite, though, let's take a red circle and add a line (the one with the arrow) to the right side of the...