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:

ACTIONSCRIPT:
  1. // variable stating if I clicked the mouse
  2. clicked = false;
  3. // variable stating if I am dragging the mouse
  4. dragging = false;
  5. // flag determining if an unit was selected
  6. unit_selected = false;
  7. // 10 times loop
  8. for (x=1; x<=10; x++) {
  9.     // can_place = true: I can place the unit / false: I can't
  10.     // initializing it to false to force entering the while loop
  11.     can_place = false;
  12.     // while loop to look for an empty space where to place the unit
  13.     while (!can_place) {
  14.         // setting can_place to true
  15.         // you can always place an unit until you can't...
  16.         can_place = true;
  17.         // generating random x and y positions, and random width
  18.         px = Math.floor(Math.random()*400)+50;
  19.         py = Math.floor(Math.random()*300)+50;
  20.         w = Math.floor(Math.random()*20)+30;
  21.         // scanning all already placed units
  22.         for (y=x-1; y>=1; y--) {
  23.             // determining the distance between the y-th unit and the one I am trying to place
  24.             dist_x = px-_root["sphere_"+y]._x;
  25.             dist_y = py-_root["sphere_"+y]._y;
  26.             distance = Math.ceil(Math.sqrt(dist_x*dist_x+dist_y*dist_y));
  27.             // this is the minimum distance I want between two units:
  28.             // the sum of both radiuses plus 20 pixels
  29.             min_distance = (w+_root["sphere_"+y]._width)/2+20;
  30.             // if the new unit would be too close to an existing one...
  31.             if (distance<min_distance) {
  32.                 // set can_place to false
  33.                 can_place = false;
  34.             }
  35.         }
  36.     }
  37.     // I exited the while loop so I can place the unit...
  38.     // ... let's do it...
  39.     nano = _root.attachMovie("sphere", "sphere_"+x, x, {_x:px, _y:py, _width:w, _height:w});
  40.     // function to be executed at every frame for every unit
  41.     nano.onEnterFrame = function() {
  42.         // if I am dragging...
  43.         if (dragging) {
  44.             // if the center of the unit is inside the square I made dragging the mouse...
  45.             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))) {
  46.                 // ... mark the unit as selected
  47.                 this.gotoAndStop(2);
  48.             }
  49.             else {
  50.                 // else mark the unit as unselected
  51.                 this.gotoAndStop(1);
  52.             }
  53.         }
  54.         else {
  55.             // if there is at least one unit selected...
  56.             if (unit_selected) {
  57.                 // if the mouse is over this unit and this is an unselected unit...
  58.                 if (this.hitTest(_root._xmouse, _root._ymouse, true) and this._currentframe == 1) {
  59.                     // mark the unit as destination unit
  60.                     this.gotoAndStop(3);
  61.                 }
  62.                 // if the mouse is not over this unit and this unit is marked as destination unit...
  63.                 if (!this.hitTest(_root._xmouse, _root._ymouse, true) and this._currentframe == 3) {
  64.                     // mark the unit as unselected unit
  65.                     this.gotoAndStop(1);
  66.                 }
  67.             }
  68.         }
  69.     };
  70.     // start with unselected unit
  71.     nano.gotoAndStop(1);
  72. }
  73. // creation of a movieclip where I will draw the square made by mouse dragging
  74. _root.createEmptyMovieClip("selmovie",_root.getNextHighestDepth());
  75. // creation of a movieclip where I will draw lines from units
  76. _root.createEmptyMovieClip("unitlines",_root.getNextHighestDepth());
  77. // function to be executed every time I press the mouse
  78. _root.onMouseDown = function() {
  79.     // saving mouse coordinates
  80.     click_x = _root._xmouse;
  81.     click_y = _root._ymouse;
  82.     // I clicked
  83.     clicked = true;
  84. };
  85. // function to be executed every time I move the mouse
  86. _root.onMouseMove = function() {
  87.     // if I clicked...
  88.     if (clicked) {
  89.         // I am dragging
  90.         dragging = true;
  91.         // saving acutal x and y mouse positions
  92.         posx = _root._xmouse;
  93.         posy = _root._ymouse;
  94.         // clearing the movieclip I am about to draw on
  95.         selmovie.clear();
  96.         // setting the line style of the movieclip
  97.         selmovie.lineStyle(2,0x000000,50);
  98.         // setting the filling of the movieclip
  99.         selmovie.beginFill(0xff0000,30);
  100.         // drawing a square connecting the point I first clicked on and the point I am currently on
  101.         selmovie.moveTo(click_x,click_y);
  102.         selmovie.lineTo(posx,click_y);
  103.         selmovie.lineTo(posx,posy);
  104.         selmovie.lineTo(click_x,posy);
  105.         selmovie.lineTo(click_x,click_y);
  106.         // at this time the square is filled, and you can omit next line
  107.         selmovie.endFill();
  108.  
  109.     }
  110. };
  111. // function to be executed every time I release the mouse
  112. _root.onMouseUp = function() {
  113.     // clearing the drag square
  114.     selmovie.clear();
  115.     // saving mouse position
  116.     xmouse = _root._xmouse;
  117.     ymouse = _root._ymouse;
  118.     // if I am not dragging...
  119.     if (!dragging) {
  120.         // scanning for all units
  121.         for (x=1; x<=10; x++) {
  122.             // determining distance between the units and the mouse
  123.             dist_x = _root["sphere_"+x]._x-xmouse;
  124.             dist_y = _root["sphere_"+x]._y-ymouse;
  125.             distance = Math.floor(Math.sqrt(dist_x*dist_x+dist_y*dist_y));
  126.             // if the distance is less than unit's radius...
  127.             if (distance<_root["sphere_"+x]._width/2) {
  128.                 // mark the unit as selected
  129.                 _root["sphere_"+x].gotoAndStop(2);
  130.             }
  131.             else {
  132.                 // else mark the unit as unselected
  133.                 _root["sphere_"+x].gotoAndStop(1);
  134.             }
  135.         }
  136.     }
  137.     // I am not clicking             
  138.     clicked = false;
  139.     // I am not dragging
  140.     dragging = false;
  141. };
  142. _root.onEnterFrame = function() {
  143.     // no units selected at the moment...
  144.     unit_selected = false;
  145.     // clearing the mc
  146.     unitlines.clear();
  147.     // if I am not dragging the mouse to select multiple units...
  148.     if (!dragging) {
  149.         // setting the line style
  150.         unitlines.lineStyle(1,0x888888,100);
  151.         // scanning through all units
  152.         for (x=1; x<=10; x++) {
  153.             // if the unit is selected
  154.             if (_root["sphere_"+x]._currentframe == 2) {
  155.                 // drawing a line from the centre of the unit to mouse position
  156.                 unitlines.moveTo(_root["sphere_"+x]._x,_root["sphere_"+x]._y);
  157.                 unitlines.lineTo(_root._xmouse,_root._ymouse);
  158.                 // at least a unit was selected
  159.                 unit_selected = true;
  160.             }
  161.         }
  162.     }
  163. };

And this is the result

Download the source code and think about enemy's AI... this will be the hardest part

Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
1 Star2 Stars3 Stars4 Stars5 Stars (7 votes, average: 4.86 out of 5)
Loading ... Loading ...

» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
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”

  1. Questo on August 9th, 2008 10:17 pm

    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)

Leave a Reply




Trackbacks

  1. 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 [...]