Cocos2d-x game instances (27)-simple action games (5)

Source: Internet
Author: User

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

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.