Talking about Nano War game, Actionscript 2, Flash and Game development.
In the 1st step we created the units and the engine to select them.
Now it’s time to write the code to let selected unit(s) send troops to other unit.
I created a new frame for the sphere object, a green circle, representing the target unit.
Let’s make a recap:
Frame 1: black circle, unselected unit
Frame 2: red circle, selected unit
Frame 3: green circle, target unit
Obviously, an unit cannot be target of itself
So, once we have one or more selected units, the script will have to:
If the player does not press the mouse
Draw a line from selected unit(s) to current mouse position
If the mouse is not over an unselected unit, leave all unselected units to frame 1
If the mouse is over an unselected unit, play frame 3 for that unit and leave all unselected units to frame 1
If the player presses the mouse
If the mouse is clicked outside an unselected unit, deselect all selected units
If the mose is clicked inside a unselected unit, deselect all selected units and select the new unit (during next part we’ll see how to move troops to this unit)
Here it is the script:
// variable stating if I clicked the mouse
clicked = false;
// variable stating if I am dragging the mouse
dragging = false;
// flag determining if an unit was selected
unit_selected = 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
Download the source code and think about enemy's AI... this will be the hardest part
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.