Making enemies smarter
We created enemy objects in our example game earlier; these enemies are just falling down the screen and shooting, but now we will make cleverer enemies. The topic of AI is a complex one, and it is probably difficult to cover this topic in this chapter. So, instead of covering AI theories, I'll show you techniques that can make your enemies act smarter than usual.
Enabling enemy patrolling
The first thing we want to do to the enemies is to give them the ability to move around the screen, just like our own units. To do this, we'll first add a Timer behavior to the enemyBase
object so that it can do something repeatedly. We also need to add an instance variable to it to record the state of the enemies; we don't want them to move around patrolling when they're shooting our units. Let's name this instance variable patrolling
and make it a Boolean; don't forget to change the initial value to true
.
We want the enemies to go on a patrol every few seconds, but to make it look...