Our main task in this chapter is to create an archer (the protagonist of the game) who can shoot arrows and let the protagonist rotate the bow and arrow in his hand continuously as the touch point changes. analyze: For this archery character, it can shoot arrows continuously. When we press a certain point on the screen, a red line (looking like a parabola) will be "drawn" from the position of the character's hand holding the arrow to mark the movement trajectory of the arrow; when sliding the finger or mouse on the screen, the red line will change its trajectory continuously with the position of the touch point; when releasing the finger or mouse on the screen, an arrow will be shot, and this arrow will move along the final red line path. In addition, the bow and arrow in the player's hand will rotate with the finger or mouse on the screen. Player Class Let's create the Player class. Its initial definition is as follows:
The above methods are what we need to implement in these two chapters. We will expand other methods when needed later. The CC_SYNTHESIZE macro is used to define a protected variable and declare a getfunName function and a setfunName function. You can use the getfunName function to get the value of the variable and use the setfunName function to set the value of the variable. For example: CC_SYNTHESIZE(int, playerHp, PlayerHp); defines an integer playerHp variable and also declares two methods, getPlayerHp() and setPlayerHp(). ccQuadBezierConfig is a newly defined structure, which we will explain in detail later. Let’s take a look at each of the above methods from top to bottom. Create a character The first step is to initialize (init) and create (create) the Player. Here we create the character by giving the Player's position, and the incoming coordinate position should be the position we read from the object layer of TiledMap (described in the previous chapter). The specific code is as follows:
Next, let's take a look at the createPlayer method, which will initialize our Player character. The code is as follows:
In the createPlayer method, we will create a game character as shown below. Because we couldn't find suitable game resources (the resources obtained in the original game were all parts, and they had to be reassembled frame by frame to be used), our game was kept simple and we didn't include complicated ones. Here we simply divide the character into two parts. The first part is of course the player's body, and the second part is the hand and the arrow that rotates with the touch point/mouse. (PS: Of course, due to resource limitations, the quality of our game may be slightly reduced, but you can't blame me! O(∩_∩)O~) When setting the playerbody position, you may have noticed that we did not set the character's body at the passed in playerPos, but slightly adjusted it. This is because the position we passed in is close to the bottom of the tile (we need to do this when making a tmx file. I didn't explain it clearly in the last chapter, so I'll make it up in this chapter. Remember it!). As shown in the following figure: The Y value coordinate should not be too close to the bottom of the tile, that is, do not set it to 9.990, 9.998, which is too close to 10, because the coordinate values stored in the tmx file are integers. If it is set to 9.990, 9.998, then the stored value will be 9.990 X 32 = 319.68 = 320, and similarly 9.998 X 32 is also 320. 320 is a special number for a map with a tile size of 32 X 32, because 320 / 32 = 10. In this way, the program will mistakenly think that points such as 9.990 and 9.998 are the 10th point on the coordinate. And as we said in the previous chapter, due to resolution adaptation, there is a certain deviation between the position of objects in the object group and the actual position, so we need to correct these deviations when setting the character's body position. The schematic diagram for setting the position in the above code is as follows: The offset value of the object group on the X axis is stored in GameManager, which is a singleton class. We will explain it in detail in the following chapters. Of course, if you want to run the code now, remove the GameManager::getInstance()->getObjectPosOffX() part first. After creating the character, we need to create the character's health bar. The health bar can be created through the ProgressTimer class encapsulated in Cocos2d-x. The code snippet is as follows:
#p# Rotate character bow Next, we will let the bow and arrow of the Player rotate according to the touch point/mouse. So we define the following function:
The parameter of the rotateArrow method is the location of the touch point. 1) Get the position of the character's bow and arrow in the game scene; 2) Calculate the rotation angle of the bow and arrow. The trigonometric tangent function is used here for calculation, and the principle is shown in the following figure: vector(offX, offY) is the vector between the touch point and the bow. Through the getAngle method, we can get the arc between the vector and the X axis. Furthermore, we need to convert radians rotateRadians to degrees. CC_RADIANS_TO_DEGREES is a macro that can convert radians to degrees. The reason for multiplying by -1 during conversion is that Cocos2d-x stipulates that the clockwise direction is positive, which is opposite to the direction of the angle we calculated, so the angle a needs to be converted to -a during conversion. 3) Control the range of the rotation angle, that is, only let it rotate within the right half of the character. 4) Calculate the arrow rotation time. speed indicates the speed at which the turret rotates. 0.5 / M_PI is actually 1 / 2PI, which means it rotates one circle in 1 second. rotateDuration represents the time required to rotate a specific angle, which is calculated by multiplying radians by speed. 5) Make the bow and arrow perform a rotational motion. Touch Response OK, now the Player is preliminarily defined. Next, we return to the game scene, add the Player to it, and test whether the arrow rotates with the touch point. In the Cocos2d-x 3.x engine, the process of implementing touch response is basically the same. So in 3.6, the process is still:
So we want to test whether the arrow rotates with the touch point. The first step is to rewrite the following touch callback function in GameScene and declare the variable:
Next, we need to create and bind touch events in the init function of GameScene, and create a Player object for testing. As follows:
The position of the Player is fixed, so we can't set it randomly. This is just for testing. In the following chapters, we will create a class to manage objects obtained from TiledMap, including Player, enemies, props, bricks, etc. The above plist and pvr.ccz files are our packaged resources, which are packaged using the Texturepacker editor. Click here for more details. After binding the touch events, we finally need to implement them. The code is as follows:
In the onTouchBegan and onTouchMoved functions, the processing method is the same, that is, when the current touch point is inconsistent with the previous touch point, the Player's bow and arrow are rotated. The getLocation method converts the screen coordinates saved in the touch object into the Cocos2d coordinates we need. For those who cannot distinguish between screen coordinates and Cocos2d coordinates, please refer to the article Detailed Explanation of the Cocos2d-x3.0 Coordinate System. When the touch ends, the Player object needs to shoot the arrow, but we won't write this for now. Run the game and you can see the desired effect. For resources in this chapter, please click here to download. |
<<: A brief analysis of the conspiracy theory behind the App Store's 1 Yuan selection
>>: Learn more about IOS9 every day 2: UI testing
The exploration of the deep night sky has been wi...
What does it feel like to be in love? Most of the...
For those who do bidding promotion, they are all ...
Although humans have been using saws for thousand...
[[123481]] Security has been enhanced, the overal...
Introduction to Yoga Beginner Video Resources: Co...
How much does it cost to develop the Alxa League ...
Industry 4.0 is a term that describes initiatives...
How much does it cost to join the Qiqihar decorat...
The Internet circle is familiar with the concept ...
Have you ever been curious? What does the subway ...
Apple released the EFI 2.9 update for the MacBook ...
From the ZTE incident to Huawei and Sugon being b...
This bird can kick through a human's stomach,...
There is still a month to go until Mother's D...