# Activity 5 - Making the Enemies Move

At the moment, the enemies are a bit boring because they’re just sitting still. Let’s make them move a little bit! Our goal will be to make them move from left to right like this:

Let’s start by making the enemies move to the left once they’re created. We can do this by simply adding one line of code:

enemy.body.velocity.x = -90;


Place in our createEnemies() method:

But if you test out the code now, we can see that the enemies just keep going to the left infinitely because of Phaser’s infinite canvas. Let’s make them change direction before they go out of the screen. We can do this with a timer, a tool that lets us call a function after a certain period of time. The function that the timer calls will change the enemy’s direction. We can also make the timer run infinitely so that it continues to run the function after each time interval (which we are going to do here). Put the below timer at the end of the createEnemy() method.

this.enemyTimer = this.time.addEvent({
delay: 1500,
callback: this.changeEnemyDirection,
callbackScope: this,
loop: true
});


This is a timer that continuously calls the changeEnemyDirection() method after a certain time interval. However, we haven’t put any content in it yet. Let’s do that now: put code in the changeEnemyDirection() method that will change the direction of every enemy in the enemies group (this is one of the reasons we made a group!):