Recently I used cocos2d-iphone to do the magic tower game with cocos2d-x re-compiled a bit, here I at the end of the article to provide code source for everyone to learn should be cocos2d-x contact is not very deep, if you have any suggestions or questions, you can only make progress when you learn from each other,
Here are some of the issues I encountered when writing code:
1. It should be C ++ without NSString (string class) Like oc. Most of the processing is done with char, So-x encapsulates a CCString,
However, if many parameters are passed (const char * string), we need to get the characters through string-> getCString. The following tag is created as an example:
CCLabelTTF: create (CCString: create ("Hello warrior, you can use experience to improve your capabilities")-> getCString (), "Verdana-Bold ", 32 );
2. create is used for class creation in-x, which is not bad, but here I will talk about how to create in-x like [[class name alloc] init] In oc, new class name = [class name alloc], (new class)-> init () = [... init]. As follows:
BuyKey * buykey = newBuyKey ();
Buykey-> init ();
Cannot be written as BuyKey * buykey = (new BuyKey ()-> init (); should be returned as bool type for the init () method in-x.
3. When creating a class, you must add CREATE_FUNC (Class Name). In this way, your init () method will be called only when you use create. Of course, if you want to manually call it, you can also not add it. For example, when we need to use Singleton mode for this class, we cannot create it using create, at this time, we do not need CREATE_FUNC (Class Name );
4, there is a little c ++ in the class attributes, We need to write the setter and getter method, but the cocos2d-x has been written for us to implement these two methods of the macro
// Define the m_energy variable of an int class. The access permission for this variable is protected.
// The following method name is Energy, which declares an int getEnergy () and void setEnergy (int value) method. The specific implementation needs to be defined in cpp.
CC_PROPERTY (int, m_energy, Energy );
// Basically the same as above, but the get method is passed for reference, that is, an int & getEnergy () is declared ();
CC_PROPERTY_PASS_BY_REF (int, m_energy, Energy );
// Define variables, but only declare the get method. The specific implementation must be defined in cpp.
CC_PROPERTY_READONLY (int, m_energy, Energy );
CC_PROPERTY_READONLY_PASS_BY_REF (int, m_energy, Energy );
// Define variables, and directly define the default get/set method. The first four categories are similar.
CC_SYNTHESIZE (cocos2d: CCObject *, m_weapon, Weapon );
CC_SYNTHESIZE_PASS_BY_REF (cocos2d: CCObject *, m_weapon, Weapon );
CC_SYNTHESIZE_READONLY (cocos2d: CCObject *, m_weapon, Weapon );
CC_SYNTHESIZE_READONLY_PASS_BY_REF (cocos2d: CCObject *, m_weapon, Weapon );
// When setWeapon, call the release of the original m_weapon and call the retain of the new value. Of course, exceptions (equal or NULL) have been ruled out ).
CC_SYNTHESIZE_RETAIN (cocos2d: CCObject *, m_weapon, Weapon );
CC_PROPERTY is more suitable for quickly declaring a value attribute, while CC_SYNTHESIZE is more suitable for declaring an object.
If you are not satisfied with these macros, You can implement the setter and getter methods by yourself.
The corresponding macro CCARRAY_FOREACH (mosterArray, object) is also available for Array traversal)
5. Collision Detection
Rect. intersectsRect (Rect1), a similar method in Collision Detection-x, checks whether Rect (CCRect) matches Rect1 in the target region.
Now let's talk about these points. Next, I will say that the unsolved problem is that after I play the monster walking animation during map initialization, the monster blocks will collapse after I delete them, so I commented out this part in the code source. If you want to solve this problem, please give me some advice. My QQ: 864635084
I commented out the code that is too troublesome to modify the npc conversation content. If you are interested, you can modify it.
Here, I will continue to write about the guardian tutorial because there should be too much content. However, I will explain and analyze it in the following book.