Tonypa's tile-based tutorials (updated)
[ September 03, 2004 ] by Tonu Paldra, alias Tonypa
A long, well organized and in depth series of articles that cover all the aspects of tile-based games: moving, jumping, scrolling, shooting and much more!


Intro
Why tiles?
Map format
More maps
Creating 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
Isometric mouse
Isometric scroll
Rotate hero
Rotate bground
Pathfinding
More pathfinding
Slopes

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)
{
	...

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 tru only if both upleft and upright corner are walls, if one of them is walkable tile, enemy would walk into air.

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(2);
	if (ob.xMove == 0)
	{
		ob.xMove = 0;
		ob.yMove = random(2) * 2 - 1;
	}
	else
	{
		ob.xMove = random(2) * 2 - 1;
		ob.yMove = 0;
	}
}

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.Enemy1.prototype.turning = 5;
game.Enemy2.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 100 will make him choose new direction in 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.


     
 
 
Name: Tonu Paldra, alias Tonypa
Location: Estonia
Age: 34
Flash experience: Discovered Flash4 in the year 2000 and fell in love with it. Abandoned Flash4, when they ended Flash4 help forum in Flashkit. Been playing with Flash only in spare time, thinking its great hobby
Job: Magazine designer
Website: http://www.tonypa.pri.ee/
 
 
| Homepage | News | Games | Articles | Multiplayer Central | Reviews | Spotlight | Forums | Info | Links | Contact us | Advertise | Credits |

| www.smartfoxserver.com | www.gotoandplay.biz | www.openspace-engine.com |

gotoAndPlay() v 3.0.0 -- (c)2003-2008 gotoAndPlay() Team -- P.IVA 03121770048