MVC Framework for COCOS2D-X projects

Source: Internet
Author: User

MVC Framework for COCOS2D-X projects

The cocos2d-x version used in this article is: Cocos2d-x 3.2

When we have started to build a project and start writing code, I think the students will certainly encounter such a problem:

After some UI classes are loaded onto the parent, there is often no cause for crashes. Or the code is written in thousands of lines, difficult to maintain and others help to deal with. What is this for?

In fact, it is because of 2 points:

    • You still don't know enough about cocos2d-x.

    • You don't have a framework concept.

In fact, this is often the case because your child element starts calling the parent element before the parent addchild. Or you just keep on writing and don't even think about what to write.

I used to be in the communication group to meet this problem of students often say: "Your child is still in the mother's belly is not born, you let it go to soy sauce possible?" ”

"Your child should have an infinite future, why don't you give it a good plan for the future, and just start to put a spell on it and let it shackled?" ”

I used to be a beginner cocos2d-x often encounter a misunderstanding. That is, add the display UI or the data that needs the parent in the INIT function.

In fact, there is no problem in adding code that shows the UI in Init, but it's not good for my programming experience, and I personally think that you should initialize it, and your UI should be loaded in the UI's function, logic should be inside the logic function, not everywhere.

I want to understand the MVC framework of the students, all know what MVC is, then I cite the mother of MVC definition :

Thefull name of M-VC is the Model View controller, which is the abbreviation of models-View-controller, a software design paradigm that organizes code with a method of business logic, data, interface display separation, and business logic * * To a component, there is no need to rewrite business logic while improving and customizing the interface and user interaction. MVC is uniquely developed to map the traditional input, processing, and output functions in a logical graphical user interface structure.

When I first learned about the MVC framework, I also struggled for a long time, for small projects such a framework is very expensive, often in a class can be done in something, not to be divided into 4 parts, manager, Business, UI, logic. Here, I can only say that the benevolent see of the beholder.

Let me first talk about the benefits of this framework:

1, you can easily get the previous business to any new projects.

2, you can plan change logic, art changes the UI, do not have to re-logic and business to do too much modification.

3, you can easily find the location of the problem, the code is clear, neat, normative.

Disadvantages: is you will feel very annoying, clearly a very simple business, not to write so complicated.


So what does it look like to apply the MVC framework to cocos2d-x?

First students look at the base class I wrote Baselayer

Based on my personal experience, I have given baselayer these simple functions:

1. Initialization and data reading

2. Display and Load UI

3. Load logic and return logic

......

As mentioned above, Inti () is only responsible for initialization, and ShowUI () is responsible for displaying and loading the UI, and loading and returning logic is responsible for logical processing.

So we look at the MVC architecture and Baselayer:

    • Model ———— initialization of Baselayer data and its own business

    • View ———— initialization and loading of Baselayerui

    • Logic processing and return of controller ———— Baselayer

Then the implementation of Baselayer is generally the step:

1, Inti ();

2, Getreaddata ();

3, Loaduilogic ();

4, ShowUI ();

5, Returnlogic ();

Take the actual function business as an example: Equipment upgrade function

I now have a need to implement upgrade capabilities for the equipment.

We take Baselayer as the base class and derive the business class Equipment_lvup (please forgive me for my primary school English graduation).

12345 Equipment_LvUp* m_pEquip = Equipment_LvUp::create();this->addchild(m_pEquip);m_pEquip->GetReadData(…);m_pEquip->LoadUILogic (…);m_pEquip->showUI ();

From a sequential point of view, it is not difficult to see that first initializing the data, then acquiring the business necessary data, loading the business logic, displaying the business views UI after we do this, it is easy to judge the problem if something happens because of the data, the display, or the logic. Then, when our business logic changes, we can adjust the code according to the requirements, both vivid and intuitive. And you can throw the business under different manager to call at any time, it is quite convenient.

And we can also be based on different needs, the progress of its repeated reconstruction and functional improvement. Such functions as asynchronous loading of resources, optimization of business algorithms, etc. can be gradually perfected. You can also write 4 classes, like the manager, the business, the UI, and the logical classification that you mentioned earlier. These methods can be used in any way, which just gives you a thought, a means.

MVC framework My understanding, popular point, like you play League of Legends, your event is through the mouse and keyboard to respond to, it can be said that your intuitive logic processing, qwer each click through the keyboard mapping to the corresponding logic to handle, after the logic processing to notify the relevant UI animation or rendering, etc. The UI then gives you a new interface for you to make new choices.

It is not difficult to see this framework pattern from the Cocos2d-x demo

Logical separation

Different classes are used to handle the increase and decrease of logical reference count

Each class is only responsible for its own display and animation effects.

      • likexx2015-01-08 15:10:20 halo, MVC is not a universal tool, Model->view->controller this structure for event-driven or pure data-driven things, Web site, app app can, the game itself is main Loop structure Ah pro, and MVC is two design ideas, you have to go to apply the MVC thinking to forcibly add this, in addition to superfluous, will lead to a lot of game design implementation becomes extremely cumbersome--in the application development, some cumbersome is necessary, Because an application needs to be improved from small to large for many years, but the game is different, the game life cycle is a year or two, then is maintenance, so the game architecture is efficiency first, convenient to modify the first. Support (9) Reply (2) Demonvv2015-01-13 12:35:18 replies. Likexx: This is true, I have been tangled for a long time, but I think it is still necessary, because the coder must have an architectural idea, in order to help the future of programming, so I think, It's good to have some knowledge of MVC at first. Also thank you for your suggestion, will be a great help to my future programming way! Support (1) Reply wind chimes heard winds 2015-01-15 17:53:57 reply. Likexx: Please for example not the main loop application, except Hello World, no Thanks support (6) Reply
      • sgxiaolong2015-01-08 12:13:56mvc One drawback is that m,v is not separate, V can access to M, but m cannot access to V, so it weakens the role of C, so I tend to MVP this mode, data transmission model M->P->V User input model V->p->m so that I can decouple M, V, p serves as the middle layer of the bridge.

MVC Framework for COCOS2D-X projects

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.