Menu

Start
Why tiles?
Map format
More Maps
Creating tiles
More tiles
The Hero
Keys to Move
Hit the wall
Open the door
Jumping
Clouds
Ladders
Stupid enemy
More on enemy
Shoot him
Getting items
Moving tiles
Scrolling
More scrolling
Depth
Isometric view
Mouse to move
Iso mouse
Iso scroll
Rotated hero
Rotated ground
Pathfinding
More pathfinding
Slopes
Hextiles

Source flas

Links

Creative Commons License
All content on the "Tile based games" website (including text, actionscript code, and any other original works), is licensed under a Creative Commons License.



Enemy on platform

If you want to have enemies in the side view jumping game like this (in the second room):

You only need to change couple of lines. The enemy will walk on the platform and detect the end of platform. In the end enemy will turn around and walk back. This requires enemy to check for platform below the next tile he would be on:

getMyCorners (ob.x+ob.speed*ob.xMove, ob.y+ob.speed*ob.yMove+1, ob);
if (!ob.downleft and !ob.downright and ob.upright and ob.upleft) {

Important number to notice here, is 1 in the ob.y+ob.speed*ob.yMove+1. That will check for wall below next tile. Also note how if statement will be true only if both downleft and downright corner are walls, if one of them is walkable tile, enemy would walk into air. We also check if there is no wall straight left or right from enemy using upright and upleft corners.

You can download the source fla with all the code and movie set up here.

Teaching the enemy some tricks

What if our enemy could change direction, not only reverse the direction:

Lets modify enemyBrain function. When we last time just reversed ob.xMove and ob.yMove, now we will choose randomly new direction to move:

} else {
	ob.xMove = random(3)-1;
	if (ob.xMove) {
		ob.yMove = 0;
	} else {
		ob.yMove = random(2)*2-1;
	}
}

When enemy would hit the wall, xMove will get random value. random(2) will have value 0 or 1. If xMove was 0, we set yMove randomly to 1 or -1.
random(2) is 0 or 1.
random(2)*2 is 0 or 2.
random(2)*2-1 is -1 or 1.

In case xMove had value 1, we now set yMove to 0 and get random 1 or -1 for xMove.

You can download the source fla with all the code and movie set up here.

Thats much nicer, but if we want to make enemy better, we should avoid reversing the last direction.

Write code:

} else {
  if (ob.xMove == 0) {
    ob.xMove = random(2)*2-1;
    ob.yMove = 0;
    getMyCorners (ob.x+ob.speed*ob.xMove, ob.y+ob.speed*ob.yMove, ob);
    if (!ob.downleft or !ob.upleft or !ob.downright or !ob.upright) {
      ob.xMove = -ob.xMove;
    }
  } else {
    ob.xMove = 0;
    ob.yMove = random(2)*2-1;
    getMyCorners (ob.x+ob.speed*ob.xMove, ob.y+ob.speed*ob.yMove, ob);
    if (!ob.downleft or !ob.upleft or !ob.downright or !ob.upright) {
      ob.yMove = -ob.yMove;
    }
  }
}

This time we first check the current direction. If for example we moved vertically (xMove==0) then we choose randomly 1 or -1 for xMove and set yMove to 0. But if enemy moves into corner, his new direction might send him again into wall. Thats why we get the corner points with new direction and if we detect wall, we reverse the new direction.

You can download the source fla with all the code and movie set up here.

Ok, enemy moves better since player cant predict where enemy is going to step next. But as you can notice, enemy keeps hugging the walls, he always moves until hitting the wall, then and only then choose another direction. If your map contains large empty areas, player can be sure enemy never comes there. Good example is second room, until hero stays in the center, enemy will never catch him.

We will add a chance for enemy to change direction even when he doesnt hit the wall.

I havent figured good description for ability to change direction while walking, so lets add each enemy new property called "turning":

game.Enemyp1.prototype.turning=5;
game.Enemyp2.prototype.turning=5;

Turning will represent the chance to randomly change direction in each step. Value of 0 will mean enemy never changes direction, value higher then 50 will make him choose new direction in almost each step (thats funny, you should try that out).

And to make enemy choose new direction, add to the if statement:

if (ob.downleft and ob.upleft and ob.downright
 and ob.upright and random(100)>ob.turning) {

In case random(100) will have value less then value of ob.turning, we will choose new direction even when we could continue same way.

You can download the source fla with all the code and movie set up here.

Update at 23.06.2004. Thanks to qhwa for suggesting simpler method of choosing new random direction.

Bugfix at 5.02.2005. Thanks to Eric Liobis for finding bug when wall was left or right from enemy. Correct line to check if enemy can move should be:
if (!ob.downleft and !ob.downright and ob.upright and ob.upleft) {


Next: Shoot him.

Top