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
- Public Function get select (): Boolean
- {
- Return _ select;
- }
- Public Function set select (S: Boolean)
- {
- _ Select = s;
- This. gotoandstop (_ select = true? 2: 1)
- }
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:
- Selectlist = tanklist. Filter (selecttank); // find the tank selected
- Selectlist. foreach (movetank); // move all selected tanks
- Function selecttank (element: *, index: int, arr: array)
- {
- // If the boundary of the tank overlaps the selected box, the tank is displayed as the selected state.
- If (element. getbounds (this). intersects (REC) = true)
- {
- Element. Select = true;
- Return true;
- }
- Return false;
- }
- Function movetank (element: *, index: int, arr: array)
- {
- Element. Select = false; // cancel the tank selection status when moving
- VaR randomx = targetpoint. x + math. Random () * 150-75;
- VaR randomy = targetpoint. Y + math. Random () * 150-75;
- Element. moveTo (randomx, randomy); // move the tank
- }
Copy code
The complete code is as follows:
========================================================== ========================================================== =
Main Program
:
- Import tank;
- VaR map: SPRITE = new sprite (); // defines a map
Container
- VaR tanklist: array = new array (); // defines a list Of all tanks recorded
- VaR selectlist: array = new array (); // defines a list of currently selected tanks
- VaR targetpoint: point; // target point
- VaR REC: rectangle; // select the rectangular object in the box.
- VaR shape: Shape = new shape (); // used to display the shape object of the Selection box
- VaR ismove: Boolean; // select a tank or a mobile tank.
- Addchild (MAP); // put the map into the display list
- Addchild (SHAPE); // selection box
- // Generate 20 tanks
- For (VAR I = 0; I <20; I ++)
- {
- VaR tank = new tank ();
- Map. addchild (tank );
- Tanklist. Push (tank); // record the reference of the tank
- }
- Addeventlistener (mouseevent. mouse_down, mousedownhandler)
- Addeventlistener (mouseevent. mouse_up, mouseuphandler)
- Function mousedownhandler (EVT: mouseevent)
- {
- Targetpoint = new point (EVT. currenttarget. mousex, EVT. currenttarget. Mousey );
- // If you click on a tank, the tank is selected. Otherwise, register a mouse movement event.
- If((evt.tar get is tank) = true)
- {
- Selectlist. foreach (unselect); // changes all tanks in the selected linked list to unselected
- Cleararray (selectlist); // clear the selectlist Array
- Evt.tar get. Select = true
- Selectlist.push(evt.tar get)
- Ismove = false;
- }
- Else
- {
- Addeventlistener (mouseevent. mouse_move, mousemovehandler)
- Ismove = true;
- }
- }
- Function mousemovehandler (EVT: mouseevent)
- {
- VaR recwidth = EVT. currenttarget. mouseX-targetPoint.x;
- VaR recheight = EVT. currenttarget. The mouseY-targetPoint.y;
- Shape. Graphics. Clear ();
- Shape. Graphics. beginfill (0x99ccff, 0.2 );
- Shape. Graphics. linestyle (2, 0x000000, 0.8 );
- Shape. Graphics. drawrect (targetpoint. X, targetpoint. Y, recwidth, recheight)
- Shape. Graphics. endfill ();
- }
- Function mouseuphandler (EVT: mouseevent)
- {
- Removeeventlistener (mouseevent. mouse_move, mousemovehandler)
- Rec = shape. getbounds (this );
- Shape. Graphics. Clear ();
- If (Rec. width> 0 & Rec. Height> 0) // reselect
- {
- Selectlist. foreach (unselect );
- Cleararray (selectlist );
- Selectlist = tanklist. Filter (selecttank );
- Rec. width = Rec. Height = 0;
- }
- Else if (selectlist. length> 0 & ismove = true)
- {
- Selectlist. foreach (movetank)
- Cleararray (selectlist );
- }
- }
- // Used to filter which tanks are selected
- Function selecttank (element: *, index: int, arr: array)
- {
- If (element. getbounds (this). intersects (REC) = true)
- {
- Element. Select = true;
- Return true;
- }
- Return false;
- }
- // Method for canceling the tank selection status
- Function unselect (element: *, index: int, arr: array)
- {
- Element. Select = false;
- }
- // Mobile tank Method
- Function movetank (element: *, index: int, arr: array)
- {
- Element. Select = false;
- VaR randomx = targetpoint. x + math. Random () * 150-75;
- VaR randomy = targetpoint. Y + math. Random () * 150-75;
- Element. moveTo (randomx, randomy );
- }
- Function cleararray (ARR: array)
- {
- While (ARR. length> 0)
- {
- Arr. Shift ();
- }
- }
Copy code
========================================================== ========================================================== =
Tanks
- Package
- {
- /**
- * A simple tank model
- * Author: prodigal son with a flash knife
- * Blog: http://hi.baidu.com/mr_ziqiang
- */
- Import flash. display. movieclip
- Import flash. Geom. Point
- Import flash. Events. event;
- Public class tank extends movieclip
- {
- Private var speed: int; // tank speed
- Private var targetpoint: point; // target point
- Private VaR _ select: Boolean; // whether the tank is selected. If this tank is selected, the selected tank status is displayed.
- /**
- * Tank Constructor
- * A coordinate is randomly generated during initialization.
- */
- Public Function tank ()
- {
- This. mousechildren = false; // remember to disable the mouse clicking of a sub-object.
- This. x = 300 * Math. Random () + 10; // set the random size range of the class in the upper left corner when the tank appears.
- This. Y = 300 * Math. Random () + 10;
- This. scalex = This. scaley = 1 + math. Random (); // The largest tank in the Red Police
- Speed = (3-scalex) * 4; // The greater the volume, the slower the speed.
- }
- Public Function moveTo (TX: Number, Ty: Number)
- {
- This. removeeventlistener (event. enter_frame, onmove );
- Targetpoint = new point (TX, Ty );
- This. addeventlistener (event. enter_frame, onmove );
- }
- Private function onmove (EVT: Event)
- {
- VaR dx = targetpoint. X-This. X;
- VaR DY = targetpoint. Y-This. Y;
- VaR radian = math. atan2 (dy, dx); // New Angle
- 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
- If (dx * dx + dy * dy <= speed * speed)
- {
- This. x = targetpoint. X;
- This. Y = targetpoint. Y;
- This. removeeventlistener (event. enter_frame, onmove );
- }
- Else
- {
- VaR vx = math. Cos (radian) * speed; // calculates the increment.
- VaR Vy = math. Sin (radian) * speed;
- This. x + = VX;
- This. Y + = Vy;
- }
- }
- Public Function get select (): Boolean
- {
- Return _ select;
- }
- Public Function set select (isselect: Boolean)
- {
- _ Select = isselect;
- If (_ select = true)
- {
- This. gotoandstop (2 );
- }
- Else
- {
- This. gotoandstop (1 );
- }
- }
- }
- }
Copy code