Cocos2d-x game instances-simple action games (6)

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

This article continues with the simple action game instance, this article adds a simple blood size UI, the effect is as follows:

We can see that a UI is added to the upper left corner to display the blood bar, which is the result of adding two lines with different line widths. First, we declare a uilayer class, which inherits from the cclayer, use the draw function to draw the shape. The declaration of this class is as follows:

In addition to the initialization function and destructor, the draw function is rewritten to draw a graph in it, and then the variable life of the main character's life is added to its set and get functions for easy access, the function implementation is as follows:

It mainly refers to the implementation of the draw function. It inherits from the draw function of the cclayer. First, it draws a black line at the bottom, and the line width is set to 10. Then it draws the blood strip above, and the blood strip is red, the line width is set to 5, and the red length is set to 100 at the beginning of life, which is determined by the life value;

In our scenario initialization init, add this UI Layer and add it to the scenario:

Ui = new uilayer ();

Addchild (ui, 0 );

Then, when a collision with the monster is detected in the update function, the corresponding blood volume is subtracted:

If (! Isreduce & iscollision (gameplayer, enemy )){

Ccactioninterval * Action = ccblink: actionwithduration (5, 10 );

Gameplayer-> runaction (action );

Schedule (schedule_selector (mapscene: resetreduce), 5.0f );

Isreduce = true;

Hmove = 0;

UI-> setlife (ui-> getlife ()-5 );

}

This simple action game instance is basically complete. It just adds some simple functions, such as map collision, jumping, enemies, and UI. You can add other functions. Upload the code here, the Code address is:

Http://download.csdn.net/detail/bill_man/4246534

In addition, I would like to thank you for your support. The coding time is too short and there is no time for restructuring and optimization. If you have any errors or deficiencies, please help me to point out. Thank you!

If there are any errors, I hope you can correct them more.

Next, continue with other game instances

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.