Create a Flash Game like Nano War - step 2
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 (distance<min_distance) {
-
// set can_place to false
-
can_place = false;
-
}
-
}
-
}
-
// I exited the while loop so I can place the unit...
-
// ... let's do it...
-
nano = _root.attachMovie("sphere", "sphere_"+x, x, {_x:px, _y:py, _width:w, _height:w});
-
// function to be executed at every frame for every unit
-
nano.onEnterFrame = function() {
-
// if I am dragging...
-
if (dragging) {
-
// if the center of the unit is inside the square I made dragging the mouse...
-
if ((this._x>Math.min(click_x, posx)) and (this._x<Math.max(click_x, posx)) and (this._y>Math.min(click_y, posy)) and (this._y<Math.max(click_y, posy))) {
-
// ... mark the unit as selected
-
this.gotoAndStop(2);
-
}
-
else {
-
// else mark the unit as unselected
-
this.gotoAndStop(1);
-
}
-
}
-
else {
-
// if there is at least one unit selected...
-
if (unit_selected) {
-
// if the mouse is over this unit and this is an unselected unit...
-
if (this.hitTest(_root._xmouse, _root._ymouse, true) and this._currentframe == 1) {
-
// mark the unit as destination unit
-
this.gotoAndStop(3);
-
}
-
// if the mouse is not over this unit and this unit is marked as destination unit...
-
if (!this.hitTest(_root._xmouse, _root._ymouse, true) and this._currentframe == 3) {
-
// mark the unit as unselected unit
-
this.gotoAndStop(1);
-
}
-
}
-
}
-
};
-
// start with unselected unit
-
nano.gotoAndStop(1);
-
}
-
// creation of a movieclip where I will draw the square made by mouse dragging
-
_root.createEmptyMovieClip("selmovie",_root.getNextHighestDepth());
-
// creation of a movieclip where I will draw lines from units
-
_root.createEmptyMovieClip("unitlines",_root.getNextHighestDepth());
-
// function to be executed every time I press the mouse
-
_root.onMouseDown = function() {
-
// saving mouse coordinates
-
click_x = _root._xmouse;
-
click_y = _root._ymouse;
-
// I clicked
-
clicked = true;
-
};
-
// function to be executed every time I move the mouse
-
_root.onMouseMove = function() {
-
// if I clicked...
-
if (clicked) {
-
// I am dragging
-
dragging = true;
-
// saving acutal x and y mouse positions
-
posx = _root._xmouse;
-
posy = _root._ymouse;
-
// clearing the movieclip I am about to draw on
-
selmovie.clear();
-
// setting the line style of the movieclip
-
selmovie.lineStyle(2,0x000000,50);
-
// setting the filling of the movieclip
-
selmovie.beginFill(0xff0000,30);
-
// drawing a square connecting the point I first clicked on and the point I am currently on
-
selmovie.moveTo(click_x,click_y);
-
selmovie.lineTo(posx,click_y);
-
selmovie.lineTo(posx,posy);
-
selmovie.lineTo(click_x,posy);
-
selmovie.lineTo(click_x,click_y);
-
// at this time the square is filled, and you can omit next line
-
selmovie.endFill();
-
-
}
-
};
-
// function to be executed every time I release the mouse
-
_root.onMouseUp = function() {
-
// clearing the drag square
-
selmovie.clear();
-
// saving mouse position
-
xmouse = _root._xmouse;
-
ymouse = _root._ymouse;
-
// if I am not dragging...
-
if (!dragging) {
-
// scanning for all units
-
for (x=1; x<=10; x++) {
-
// determining distance between the units and the mouse
-
dist_x = _root["sphere_"+x]._x-xmouse;
-
dist_y = _root["sphere_"+x]._y-ymouse;
-
distance = Math.floor(Math.sqrt(dist_x*dist_x+dist_y*dist_y));
-
// if the distance is less than unit's radius...
-
if (distance<_root["sphere_"+x]._width/2) {
-
// mark the unit as selected
-
_root["sphere_"+x].gotoAndStop(2);
-
}
-
else {
-
// else mark the unit as unselected
-
_root["sphere_"+x].gotoAndStop(1);
-
}
-
}
-
}
-
// I am not clicking
-
clicked = false;
-
// I am not dragging
-
dragging = false;
-
};
-
_root.onEnterFrame = function() {
-
// no units selected at the moment...
-
unit_selected = false;
-
// clearing the mc
-
unitlines.clear();
-
// if I am not dragging the mouse to select multiple units...
-
if (!dragging) {
-
// setting the line style
-
unitlines.lineStyle(1,0x888888,100);
-
// scanning through all units
-
for (x=1; x<=10; x++) {
-
// if the unit is selected
-
if (_root["sphere_"+x]._currentframe == 2) {
-
// drawing a line from the centre of the unit to mouse position
-
unitlines.moveTo(_root["sphere_"+x]._x,_root["sphere_"+x]._y);
-
unitlines.lineTo(_root._xmouse,_root._ymouse);
-
// at least a unit was selected
-
unit_selected = true;
-
}
-
}
-
}
-
};
And this is the result
Download the source code and think about enemy's AI... this will be the hardest part
Tell me what do you think about this post. I'll write better and better entries.
They can be easily customized to meet the unique requirements of your project.
2 Responses to “Create a Flash Game like Nano War - step 2”
Leave a Reply
Trackbacks
-
User links about "radius" on iLinkShare on
August 18th, 2008 11:30 pm
[...] | user-saved public links | iLinkShare 4 votesCreate a Flash Game like Nano War - step 2>> saved by Flaco1408 1 days [...]

(7 votes, average: 4.86 out of 5)
Nice as always it looks almost like the real game
It seems there are bots on triquitips Thay are posting things like Powere level your character at blahblahblah.com! or Get all your virtual gold at blahblahblah.com today!(just out of curiosity why is triqui on all your things)