Create a Flash Game like Nano War

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:

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

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.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (16 votes, average: 3.31 out of 5)
Loading ... Loading ...
If you found this post useful, please consider a small donation.
» 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.

11 Responses to “Create a Flash Game like Nano War”

  1. Xodus on August 6th, 2008 4:59 am

    Hey, im really excited about this game concept, i cant wait for the next tut. Good post.

    Btw, i suddenly noticed that you dont have the line “if you liked this post, you can buy me a beer (or two)” Why did you take it off?

  2. Questo on August 6th, 2008 5:18 am

    maybe you can fix a small bug that doesn’t matter thats in the actual game:

    If you have a unit selected while it is taken over it still has a line coming out of it (it can’t shoot or do anything for you anything but still)

  3. FrozenHaddock on August 6th, 2008 6:52 am

    Oh, nice.

    I played this, a lot. I was -hoooooked-

    I look forward to the rest of this.

  4. Mr Sun on August 6th, 2008 2:00 pm

    Thanks for the tutorial :)
    I always wondered how I could do a selection sort of thing in actionscript

  5. Gabriel Bianconi on August 6th, 2008 5:44 pm

    Great Tut!

  6. JDog on August 6th, 2008 7:22 pm

    Looks good !

  7. samedi on August 11th, 2008 6:12 am

    this game is a similar one with a twist
    http://www.newgrounds.com/portal/view/449961

    i like both ones… like tic tac toe on steroids
    My problem with these games are that after you have found the right strategy its impossibles to loose.

    I think explaining how to create an AI for a game like this would be a great tutorial

  8. badben on August 11th, 2008 7:23 pm

    Hi,
    I’m surprised to see my game on this website :).

    Regards.

  9. cssdesigner on August 29th, 2008 2:00 pm

    Wow I make this step-by-step and I played with it!

    Nice tutorial!

Leave a Reply




Trackbacks

  1. Flash Tutorials | AS3, AS2 Flash game tutorials roundup part 2 | Lemlinh.com on August 23rd, 2008 5:09 am

    [...] Read more [...]

  2. Flash obstacle avoidance prototype : Emanuele Feronato on August 25th, 2008 4:29 pm

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

Posts