Patrolling Enemies (or Friends)

Do you need sprites to do your bidding?
In this article we’ll go through a number of different types of monster (sprite) behavior that you might use in your project.

Click on the video below to see what we plan to create or click below to play! You need to use the arrow keys to move the scuba diver around and avoid the obstacles.

Click here to play

First let’s learn to walk.

By far the easiest type of enemy (or friend) sprite behavior you can add is to have a sprite just move between set points. In this code you can see we start our sprite at a fixed position and then just move it between two points. (Hint: You can add more glide blocks to make it move between even more spots!).

How do we stop it?

I’ve added in this code here to stop my sprite from moving as soon as it touches the “Diver2” sprite. I then also added a broadcast for “game-over”. You see, I want to add more sprites, so I’m going to do this same code in the other sprites as well, so any sprite that broadcasts the “game over” then makes all the other sprites stop moving. Neat!

Smarter Enemies?

Directly attack

If you want your sprite to just directly attack then we can just point it towards the sprite to attach and move. Note that I only move 1 step at a time (If you look inside my project code you’ll see that I move my diver2 character 3 steps at a time, that way he has a chance to avoid the attacking sprite. I came up with these numbers just by trying out a few different ones until I was happy that the game wasn’t too easy or hard.

Randomly Avoid

I want my octopus to move about randomly, but not too erratically. So I use a random generator to make a conditional test to see if I should change it’s direction. I set the random from 1..15; this means that one out of fifteen times we’ll change the sprites direction, so not all the time. Then we have a second random number to point in a random direction.

Intelligently Avoid

What if want to make it look like a character is “semi-intelligent”. This type of thought, typically having something behave a little erratically is usually solved by adding some “Randomness”. In this case I use random in an extra way here; stay with me! We use the same technique as before to only change directions occasionally, but then we point our sprite towards our diver and then point back away from the diver with an added random angle. This is the key to make it less obvious he’s trying to avoid the diver. He still avoids him but has some more randomness to it.

I hope you enjoyed this article! Be sure to leave some feedback and comments below for me.

Facebook Comments