Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Avoider game, Actionscript 2, Flash and Game development.

Obstacle avoidance can be very important in Flash gaming because allows designers to create smart enemies.

Obstacle avoidance behavior gives a character the ability to maneuver in a cluttered environment by dodging around obstacles.

In this prototype, I’ll try to simulate everyday life.

In everyday life, you walk straight until an obstacle appears in your line of sight.

Then, you slow down, and choose a random direction to cross the obstacle. If necessary, you make two or three steps back and approach the obstacle again.

Well, I hope you don’t act this way in your real life but this is what we are going to do in this prototype.

It’s up to you to improve it and make a more realistic movement.

In the project there are 20 random obstacles placed in the same way as seen on Create a Flash Game like Nano War, and an object linked as runner running through them.

Runner’s behavior is explained in the commented script

// movieclip that will host all obstacles
createEmptyMovieClip("obstacles", 1);
// placing 20 obstacles and the player
for (x=1; x<=21; x++) {
	// can_place = true: I can place the obstacle / false: I can't
	// initializing it to false to force entering the while loop
	can_place = false;
	// while loop to look for an empty space where to place the obstacle
	while (!can_place) {
		// setting can_place to true
		// you can always place an obstacle until you can't...
		can_place = true;
		// generating random x and y positions, and random width
		px = Math.floor(Math.random()*400)+50;
		py = Math.floor(Math.random()*400)+50;
		// if I am at the end of the cycle, it's time to place the runner
		if (x == 21) {
			w = 20;
		} else {
			w = Math.floor(Math.random()*100)+10;
		}
		// scanning all already placed obstacles
		for (y=x-1; y>=1; y--) {
			// determining the distance between the y-th obstacle and the one I am trying to place
			dist_x = px-obstacles["sphere_"+y]._x;
			dist_y = py-obstacles["sphere_"+y]._y;
			distance = Math.ceil(Math.sqrt(dist_x*dist_x+dist_y*dist_y));
			// this is the minimum distance I want between two obstacles:
			// the sum of both radiuses plus 20 pixels
			min_distance = (w+obstacles["sphere_"+y]._width)/2+20;
			// if the new obstacle would be too close to an existing one...
			if (distancemax_speed) {
			speed = max_speed;
		}
		// turn = 0 means "next time I will find an obstacle I will have to decide where to go"
		turn = 0;
	}
	// updating speed
	x_speed = speed*Math.cos(angle);
	y_speed = speed*Math.sin(angle);
	//updating position
	this._x += x_speed;
	this._y += y_speed;
	if (this._x>500) {
		this._x -= 500;
	}
	if (this._y>500) {
		this._y -= 500;
	}
	if (this._x<0) {
		this._x += 500;
	}
	if (this._y<0) {
		this._y += 500;
	}
};

and here it is

As you can see, sometimes the runner is quite dumb, but it's a good start to develop a more complex AI script for obstacle avoidance

Download the source code and enjoy

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.