Real-time strategic game development-rectangle application practices + Methods ignored by you in array

Source: Internet
Author: User

Last time I posted a rectangle object in the game
Development
Application in
Article
Next, we will introduce the practical application of the rectangle object in the instant strategy game. At the same time, a few methods are usually used in array objects.



Friends who have played "Red Police" or "times of Empire" should know that such games have a box selection function. Mouse
Pull a box on the screen and all the items in the box are selected.


The prototype of this example is as follows:



1. There are two options for tank selection: Box selection and click to select.
2. After a tank is selected, it will show that it is selected
3. If a tank is selected when you click the mouse on the screen, the tank moves to the vicinity of the mouse clicking position (not the mouse clicking position nearby), and the selection status is automatically canceled.
4. If a new option is selected, the selection status of the selected tank will be canceled.




Let's take a look at the effect.
, Click here



[Flash
Http://source.iflashigame.com/c&c/c&c.swf
[/Flash]
Explanation:


1. Implementation of tank selection and non-selection status

Set an attribute in our tank class
Select, implemented by the set method. When select = true, let the frame of the tank jump to 2nd frames. When select = false, let the frame of the tank jump to 1st frames.



Set a private Variable _ select in tank, and then set getter/setter

  1. Public Function get select (): Boolean
  2. {
  3. Return _ select;
  4. }
  5. Public Function set select (S: Boolean)
  6. {
  7. _ Select = s;
  8. This. gotoandstop (_ select = true? 2: 1)
  9. }

Copy code

2. Implementation of tank click selection

We do not need to register mouse events for every tank.
You only need to register a mouse event for the container where the tank is placed to know which tank the user clicks.
Mouseevent has two attributes: Target and currenttarget. The two attributes are different. The target returns the interactive object that receives the click first, and the currenttarget returns the object registered by your addeventlistener. See:

3. Implementation of tank box Selection

Basically, we need to do the following steps:
A. Use the mouse's mouse_move event to pull the box.
B. Compare the drawn box with the boundary box of the object to determine whether the object has been selected.
C. Change these objects to the selected state.
D. When the mouse is clicked again, if a tank is in the selected status and is in the open space position, all tanks move to the vicinity of the target point, and cancel all selection statuses.

The implementation idea is to prepare two arrays, one to record the reference tanklist of all tanks, and one to record the reference selectlist of the selected tanks at any time.

Here, we will use the methods foreach () and filter () in the two arrays that are not frequently used. These two methods will play an important role here. There are five new methods for array objects that you may not be concerned about. For example:

Let's take a look at how we use this example:

  1. Selectlist = tanklist. Filter (selecttank); // find the tank selected
  2. Selectlist. foreach (movetank); // move all selected tanks

  3. Function selecttank (element: *, index: int, arr: array)
  4. {
  5. // If the boundary of the tank overlaps the selected box, the tank is displayed as the selected state.
  6. If (element. getbounds (this). intersects (REC) = true)
  7. {
  8. Element. Select = true;
  9. Return true;
  10. }
  11. Return false;
  12. }

  13. Function movetank (element: *, index: int, arr: array)
  14. {
  15. Element. Select = false; // cancel the tank selection status when moving
  16. VaR randomx = targetpoint. x + math. Random () * 150-75;
  17. VaR randomy = targetpoint. Y + math. Random () * 150-75;
  18. Element. moveTo (randomx, randomy); // move the tank
  19. }

Copy code

The complete code is as follows:
========================================================== ========================================================== =
Main Program
:

  1. Import tank;

  2. VaR map: SPRITE = new sprite (); // defines a map
    Container
  3. VaR tanklist: array = new array (); // defines a list Of all tanks recorded
  4. VaR selectlist: array = new array (); // defines a list of currently selected tanks
  5. VaR targetpoint: point; // target point
  6. VaR REC: rectangle; // select the rectangular object in the box.
  7. VaR shape: Shape = new shape (); // used to display the shape object of the Selection box
  8. VaR ismove: Boolean; // select a tank or a mobile tank.
  9. Addchild (MAP); // put the map into the display list
  10. Addchild (SHAPE); // selection box

  11. // Generate 20 tanks
  12. For (VAR I = 0; I <20; I ++)
  13. {
  14. VaR tank = new tank ();
  15. Map. addchild (tank );
  16. Tanklist. Push (tank); // record the reference of the tank
  17. }

  18. Addeventlistener (mouseevent. mouse_down, mousedownhandler)
  19. Addeventlistener (mouseevent. mouse_up, mouseuphandler)

  20. Function mousedownhandler (EVT: mouseevent)
  21. {
  22. Targetpoint = new point (EVT. currenttarget. mousex, EVT. currenttarget. Mousey );
  23. // If you click on a tank, the tank is selected. Otherwise, register a mouse movement event.
  24. If((evt.tar get is tank) = true)
  25. {
  26. Selectlist. foreach (unselect); // changes all tanks in the selected linked list to unselected
  27. Cleararray (selectlist); // clear the selectlist Array
  28. Evt.tar get. Select = true
  29. Selectlist.push(evt.tar get)
  30. Ismove = false;
  31. }
  32. Else
  33. {
  34. Addeventlistener (mouseevent. mouse_move, mousemovehandler)
  35. Ismove = true;
  36. }
  37. }
  38. Function mousemovehandler (EVT: mouseevent)
  39. {
  40. VaR recwidth = EVT. currenttarget. mouseX-targetPoint.x;
  41. VaR recheight = EVT. currenttarget. The mouseY-targetPoint.y;
  42. Shape. Graphics. Clear ();
  43. Shape. Graphics. beginfill (0x99ccff, 0.2 );
  44. Shape. Graphics. linestyle (2, 0x000000, 0.8 );
  45. Shape. Graphics. drawrect (targetpoint. X, targetpoint. Y, recwidth, recheight)
  46. Shape. Graphics. endfill ();
  47. }
  48. Function mouseuphandler (EVT: mouseevent)
  49. {
  50. Removeeventlistener (mouseevent. mouse_move, mousemovehandler)
  51. Rec = shape. getbounds (this );
  52. Shape. Graphics. Clear ();
  53. If (Rec. width> 0 & Rec. Height> 0) // reselect
  54. {
  55. Selectlist. foreach (unselect );
  56. Cleararray (selectlist );
  57. Selectlist = tanklist. Filter (selecttank );
  58. Rec. width = Rec. Height = 0;
  59. }
  60. Else if (selectlist. length> 0 & ismove = true)
  61. {
  62. Selectlist. foreach (movetank)
  63. Cleararray (selectlist );
  64. }
  65. }
  66. // Used to filter which tanks are selected
  67. Function selecttank (element: *, index: int, arr: array)
  68. {
  69. If (element. getbounds (this). intersects (REC) = true)
  70. {
  71. Element. Select = true;
  72. Return true;
  73. }
  74. Return false;
  75. }
  76. // Method for canceling the tank selection status
  77. Function unselect (element: *, index: int, arr: array)
  78. {
  79. Element. Select = false;
  80. }
  81. // Mobile tank Method
  82. Function movetank (element: *, index: int, arr: array)
  83. {
  84. Element. Select = false;
  85. VaR randomx = targetpoint. x + math. Random () * 150-75;
  86. VaR randomy = targetpoint. Y + math. Random () * 150-75;
  87. Element. moveTo (randomx, randomy );
  88. }
  89. Function cleararray (ARR: array)
  90. {
  91. While (ARR. length> 0)
  92. {
  93. Arr. Shift ();
  94. }
  95. }

Copy code

========================================================== ========================================================== =
Tanks

  1. Package
  2. {
  3. /**
  4. * A simple tank model
  5. * Author: prodigal son with a flash knife
  6. * Blog: http://hi.baidu.com/mr_ziqiang
  7. */
  8. Import flash. display. movieclip
  9. Import flash. Geom. Point
  10. Import flash. Events. event;

  11. Public class tank extends movieclip
  12. {
  13. Private var speed: int; // tank speed
  14. Private var targetpoint: point; // target point
  15. Private VaR _ select: Boolean; // whether the tank is selected. If this tank is selected, the selected tank status is displayed.
  16. /**
  17. * Tank Constructor
  18. * A coordinate is randomly generated during initialization.
  19. */
  20. Public Function tank ()
  21. {
  22. This. mousechildren = false; // remember to disable the mouse clicking of a sub-object.
  23. This. x = 300 * Math. Random () + 10; // set the random size range of the class in the upper left corner when the tank appears.
  24. This. Y = 300 * Math. Random () + 10;
  25. This. scalex = This. scaley = 1 + math. Random (); // The largest tank in the Red Police
  26. Speed = (3-scalex) * 4; // The greater the volume, the slower the speed.
  27. }
  28. Public Function moveTo (TX: Number, Ty: Number)
  29. {
  30. This. removeeventlistener (event. enter_frame, onmove );
  31. Targetpoint = new point (TX, Ty );
  32. This. addeventlistener (event. enter_frame, onmove );
  33. }
  34. Private function onmove (EVT: Event)
  35. {
  36. VaR dx = targetpoint. X-This. X;
  37. VaR DY = targetpoint. Y-This. Y;
  38. VaR radian = math. atan2 (dy, dx); // New Angle
  39. Body. Rotation = radian * 180/Math. Pi; // in my tank class, there is a body MC, so that the green box does not follow the turn
  40. If (dx * dx + dy * dy <= speed * speed)
  41. {
  42. This. x = targetpoint. X;
  43. This. Y = targetpoint. Y;
  44. This. removeeventlistener (event. enter_frame, onmove );
  45. }
  46. Else
  47. {
  48. VaR vx = math. Cos (radian) * speed; // calculates the increment.
  49. VaR Vy = math. Sin (radian) * speed;

  50. This. x + = VX;
  51. This. Y + = Vy;
  52. }
  53. }
  54. Public Function get select (): Boolean
  55. {
  56. Return _ select;
  57. }
  58. Public Function set select (isselect: Boolean)
  59. {
  60. _ Select = isselect;
  61. If (_ select = true)
  62. {
  63. This. gotoandstop (2 );
  64. }
  65. Else
  66. {
  67. This. gotoandstop (1 );
  68. }
  69. }
  70. }
  71. }

Copy code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.