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