Tilebased games in Flash 5 – Part 6

Ok, I haven´t written on these in a long while now(have quit my job at Paregos, moved, traveled and lots of other things…), sorry about that :(
I think I was about to write something about making simple enemies. So here we go again!

I´m gonna divide this into two different types of enemies, or more different types of games. We´re gonna start with a top-down racing game, you know usually a car-game(drive around on a road, be first… bla bla).

These sort of games can be quite boring if you´re racing all alone, no stress, no one to compete with.

We start with drawing a map(read earlier tutorials if you don´t know what I´m talking about), make it look like some sort of track. I´ll stick to 1;s and 0;s just to keep it simple.
Mine looks something like this:

map = [  [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
         [1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1],
         [1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1],
         [1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1],
         [1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1],
         [1,1,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1],
         [1,1,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,1],
         [1,1,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,1],
         [1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1],
         [1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1],
         [1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1],
         [1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,1],
         [1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1],
         [1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1],
         [1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1],
         [1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1],
         [1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1],
         [1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1],
         [1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1],
         [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1],
         [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1],
         [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1],
         [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1],
         [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] ];

Hmm, pretty big, well ok… I think you can almost see where the track is supposed to be, 0 is road and 1 is grass.
What I usually do when I make an enemy is to take the user-controlled sprite’s code an develop it from there, cause think about it, we often want our enemies to behave somewhat like we do when we play. So for that we need a simple car that you can drive around with the arrowkeys. There are plenty of examples on how you make stuff like that, so I´m not gonna bother explaining that here(I think the code pretty much explains itself in this case).

So now we have the mapDrawing-function and a simple car we can drive around all over the screen. It looks something like this:

And here´s the .fla so far.

Now let´s think about what we want… We want other cars that can drive around the track by them selves. Ok, so we could start by making an enemy car movieclip and remove all the keyboard-interaction-stuff. And instead just make it put the pedal to the metal all the time, cause that´s how I drive in games like these(Not in real life though…). But then we have the problem of making the car turn in the correct direction by itself and finding it´s own way around the track.

This problem can be solved with another array, just like the map-array(or changing the map-array). But instead of pointing at different tiles, each element is a prefered angle that the car should turn towards. So when the car is “dropped” onto the map, it will open full throttle. Then it will check where it is in the angle-array and then start turning towards that angle, left or right whichever is the closest way. Then it will check there again when it enters a new tile, if the angle is different, turn again, and so on…

So let´s make an angle-array then. It should be the same as the map-array, but instead consist of angles between 0 and 360. I´m not showing that here, I´m sure you get the idea!

Then it´s just a simple matter of making the enemy-car follow the angles in the angle array, it works something like this in pseudo-code:

goalAngle // from the angle-array
currentAngle // the cars current angle

difference = currentAngle - goalAngle;

if ( the difference is > X ) turn Left

if ( the difference is < X ) turn Right

Here´s a swf of how it could look:

And here´s the .fla for that one. I have tried to comment the code… :/
Have a look at the differences(in code) between the human-controlled car and the computer-car and I hope you get the idea.

Note, I haven´t added any collision-checking in these examples, as I didn´t want that to mess things up and just focus on making the ai-car drive around. But as for adding collision and such things, I usually work in the same way. That is, I start with the user-controlled car and get everything to work as I want it to, and THEN start making the ai-car, makes it easy to keep everything in almost the same way.

Also, when making stuff like this it´s a good idea to make an editor where you can draw the maps and the angles and then push a button to generate the array. Just like a big pixel-editor, but instead of pixels you can draw for example eight different angles. I think I will write something about editors soon…

I hope this makes sense to someone out there.
Now we will move on and try to make more enemies, but different types of enemies. Maze-enemies, you know like the ghosts in Pac-man and lots of other games for that matter…

Twitter

    Sorry, no Tweets were found.

Categories