Cocos2d-x Analysis of the game of the landlord and the card actionCategory: C/C + + cocos2d-x Learning notes 2013-05-17 13:54 461 People reading reviews (0) Collection Report Cocos2d-x landlord action
Recently in the development of a cocos2d-x-based game, in the game development process of the Cocos2d-x this engine has a more profound understanding. Now for the implementation process of the sub-card action to a brief summary and summary of Cocos2d-x engine action processing.
First of all, here first recommend a blog address: http://codingnow.cn/cocos2d-x/775.html, inside on the ccaction system structure did a good summary, And the blogger of a cocos2d-x about the touch of the event introduction of the article is also more detailed, suggest you go to see.
When it comes to action, the first thing we think about is the movement of elves. Ccmoveto and Ccmoveby are used here, and they are all ccaction subclasses, the difference being that the Ccmoveto is moved to the specified location according to the parameters, while the Ccmoveby parameter indicates the specific length of the move, such as Ccmoveby * Moveby = ccmoveby::create (0, CCP (100, 100)); This sentence means that the call wizard moves the x-axis (right) at the original location Point 100, moving 100 along the y-axis (UP). And if it is ccmoveto, it will move to the CCP (100, 100) this point.
However, in the actual game development process, may use more complex action effect, when we need to combine some objects, we need to use ccsequence and Ccspawn. The two are created in a similar way, all created by their static create function, with parameters that you need to combine with the action object, and note that the last argument is null. The difference between the two is that Ccsequence will execute the action object that was passed in at the time of the build, while Ccspawn executes the action object that was passed in. However, the above two methods are only for the operation of the system action provided by the same sprite. Sometimes we need to make an elf zone respond to the action of another sprite, which means that sprite A is required to perform its actions and then notify Sprite B to perform the corresponding action. The so-called action execution, in fact, through the Ccactionmanager at each frame refresh, each action calls the step function to achieve the play of the action. The main explanation for this is that the implementation of the action is based on the refresh of the frame, regardless of the order in which the code is added. For example, if you have three sprites in the For loop performing actions in turn, the engine does not perform the action because of the order in which your code executes, but instead performs the actions of the three sprites each time the for loop executes. This goes back to the question of how to get an elf to tell another genie that I've done the action and that you're doing the action.
In Cocos2d-x, four callback function wrappers are provided to help us solve this problem.
Callfunc_selector, corresponding to the Cccallfunc class, is used to wrap the callback function without parameters;
Callfuncn_selector, corresponding to the CCCALLFUNCN class, is used to wrap a callback function that contains a pointer to the ccnode* type that performs the action (in short, who run this action will pass whose address as a parameter);
Callfuncnd_selector, corresponding to the CCCALLFUNCND class, in addition to the 2nd ccnode* can be passed, we can also pass a void* pointer;
Callfunco_selector, corresponding to the Cccallfunco class, is used to wrap a callback function that contains a ccobject* parameter, and describes the four callback function wrappers, mainly because it can be added as a parameter to Ccsequence. It is also possible to add to Ccspawn, except that the action object and function address in the Ccspawn as a parameter are executed at the same time, so that some unexpected effects can occur. Therefore, to ensure that an action is completed before executing our callback function, we need to add it to the ccsequence. So how do you implement an elf to perform the action and then let another sprite perform the action. It's very simple, as long as the second-to-last argument in an action sequence that an elf needs to execute (the final argument is null, of course) is set to the address of the function you want to execute, and then the wizard enters the callback function after all the actions that have been done. You can add another sprite's execution action code in the callback function.
The following is a concrete introduction to the implementation of the Landlord licensing action effects.
Friends who have played mobile phone owners know that the card will be issued to the upper left and the top right of the two computer players can not see the content of the card, and then sent to the user to see the specific content of the card, which actually contains at least two sprites. A Back Card wizard moves to the top left, then back to the origin, then to the top right (which can be set to not be visible), and then the wizard that displays the specific card (there will actually be many sheets, which can be maintained in a ccarray) to the user's selection area below. The process is simply to send a card to the user, continue to the card can use recursive call, until the number of cards before jumping out of the function. The implementation of the specific code is as follows:
[CPP] View plain copy print? Void gamescene::d ispatchcardscallback (Ccnode *psender, void* args) { switch ((int) args) { case 1: { //dispath left movebackcard->setposition (CCP (m_winSize.width / &NBSP;2,&NBSP;M_WINSIZE.HEIGHT&NBSP;/&NBSP;2)); Ccmoveby *leftmoveby = ccmoveby::create (0.1F,&NBSP;CCP (100 - m_winsize.width / 2, m_winsize.height / 2 - 127)); ccsequence *leftsequence = ccsequence::create (leftMoveBy, Cccallfuncnd::create (This, callfuncnd_selector (gamescene::d ispatchcardscallback), (void*) 2), null); movebackcard->runaction ( leftsequence); break; } case 2: { dispatch right movebackcard->setposition ( CCP (M_WINSIZE.WIDTH&NBSP;/&NBSP;2,&NBSP;M_WINSIZE.HEIGHT&NBSP;/&NBSP;2)); ccmoveby *rightmoveby = ccmoveby::create (0.1F,CCP (m_winSize.width / 2 - 100, m_winsize.height / 2 - 127)); ccsequence *rightsequence = ccsequence::create (rightMoveBy, Cccallfuncnd::create (This, callfuncnd_selector (gamescene::d ispatchcardscallback), (void*) 3), NULL); &nbSp movebackcard->runaction (rightsequence); break; } case 3: { //dispatch for user ccmoveto *moveaction = ccmoveto:: Create (0.1F,&NBSP;CCP (100 + (m_mutex + 1) * 30, 70)); ccsequence *sequence =ccsequence::create (moveAction, Cccallfuncnd::create (This, callfuncnd_selector (gamescene::d ispatchcardscallback), (void*) 1), NULL); if (m_mutex < 17) { (dynamic_cast< Cardsprite *> (M_array->objectatindex (m_mutex++))->runaction (sequence); } else { this->removechild (movebackcard); this->removechild (backcard); adjustcard (M_array); //sorting the card elves showcallscore (); //Display called Sub menu } break; } default: break; }