4. Random Movement

1. Random Movement

Right-click on your enemy class on the main screen and select 'Open editor'.

The enemy character should always be moving, so start with move(1);

Next we will use an if statement to start the random movement.

You may have noticed that the enemy objects can get 'stuck' in the corners or when they hit the edge of the world.

In the act() method, below the random movement statement, add the code in the red box that checks to see if the object is at the edge, and turns it around (180 degrees) if it is.

2. Bounce at World Edge

3. Check it Works

Click Run to see your enemy objects randomly move around the world.

© CSNewbs 2020