Get the full commented source code of

HTML5 Suika Watermelon Game

Talking about Nano War game, Actionscript 2, Flash and Game development.

Some days ago I played a bit Nano War.

Nano War

It’s an interesting RTS, and pretty easy to delvelop.

In this part we’ll analyze the unit selection system.

In Nano War your units are circles, and this helps a lot because it’s very easy to determine collisions knowing radius and center.

Anyway, you can select units in these ways:

Click on an unselected unit: you select the unit

Click on an selected unit: you keep selecting the unit (good!)

Click outside an unit: you deselect all units

Click outside an unit and drag mouse: you select all units whose center is inside the shape you are drawing bby dragging the mouse.

In this example, 10 units are randomly placed around the screen, avoiding them to overlap.

Then, you can select and deselect them in the way explained before.

Units are made by a circle object linked as “sphere” with two frames: frame 1 is a black circle and represents the unit when deselected, while frame 2 is a red circle and represents the unit when selected.

Now, a little commented actionscript:

// variable stating if I clicked the mouse
clicked = false;
// variable stating if I am dragging the mouse
dragging = false;
// 10 times loop
for (x=1; x<=10; x++) {
	// can_place = true: I can place the unit / 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 unit
	while (!can_place) {
		// setting can_place to true
		// you can always place an unit 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()*300)+50;
		w = Math.floor(Math.random()*20)+30;
		// scanning all already placed units
		for (y=x-1; y>=1; y--) {
			// determining the distance between the y-th unit and the one I am trying to place
			dist_x = px-_root["sphere_"+y]._x;
			dist_y = py-_root["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 units:
			// the sum of both radiuses plus 20 pixels
			min_distance = (w+_root["sphere_"+y]._width)/2+20;
			// if the new unit would be too close to an existing one...
			if (distanceMath.min(click_x, posx)) and (this._xMath.min(click_y, posy)) and (this._y

And this is the result... easy to play... just select unit clicking on them or dragging.

Deselect by clicking elsewhere.

I would like to spend two words on beginFill() that I never covered on the blog...

beginFill() indicates the beginning of a new drawing path. If an open path exists and it has a fill associated with it, that path is closed with a line and then filled.

During next part we'll manage unit invasion.

Meanwhile download the source code.

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