Small man (Bill man) Personal Original, welcome to reprint, reprint please indicate the address, small man (Bill man) column address http://blog.csdn.net/bill_man
Continue to the simple action game instance. In this article, add the mobs and the collision detection between the monsters and the protagonist. The effect is as follows:
First, add the monster genie to the initialization function of the scenario, and set the location and anchor:
Enemy = ccsprite: spritewithfile ("spinningpeas.png ");
Map-> addchild (enemy, map-> getchildren ()-> count ());
Enemy-> setpositioninpixels (CCP (winsize. width/2 + 80, 32 ));
Enemy-> setanchorpoint (CCP (0.5f, 0 ));
The following is the logic of the monster, that is, moving left and right. The logic code is as follows:
Enemymovetick is a simple step variable, and the moving step of each frame is given according to this value.
To make collision detection accurate, I have customized the width and height of the main character and blame, and added them to the init function:
Ownsize = ccsizemake (40,100 );
Othersize = ccsizemake (32, 32 );
The following describes the collision detection function:
The function detects a collision based on the location of the two genie and the previously set width. The Anchor's anchor is located at the soles of the feet, and the absolute value of the anchor's abscissa difference is less than half the width difference, the vertical coordinate difference (only the difference between the protagonist's vertical coordinate and the monster's vertical coordinate) is smaller than the height of the monster, greater than the height of the protagonist multiplied by-1, that is, the relative position of the monster and the protagonist is as follows:
The mob is higher than this one (the small rectangle is the mob ):
The mob is lower than this position:
That is, it should be within this range:
Call the detection function in the update function and handle the collision:
First, add the injury action to the main character, and then Mark isreduce as true, that is, the collision will not be detected within five seconds after the injury, and then use the schedule function to set isreduce back to false in five seconds. Set the horizontal speed to 0,
If there are any errors, I hope you can correct them more.
Next, continue simple action game instance