Quick-Cocos2d-X Butler Framework process

Source: Internet
Author: User

 

I have been paying more attention to Quick Lua, but I have never been able to use Quick Lua in practice because of the company's Lua framework that I have been using in the project. I can see that many people in the group are using this. Here I will sort out the process to start using. I mainly discuss the actual use process problems.



For example, many learners do not even know why enterScene (MainScene) is a string? Of course, if you are familiar with the framework, you can skip this article.


Start now!


I. Preparations
1. Install, download, and so on. The official forum is clearly written, so I won't talk about it. Http://wiki.quick-x.com/doku.php? Id = zh_cn: get_started_create_new_project
2. for IDE, I used IEDA and configured the export api code prompt, which is quite convenient. Http://wiki.quick-x.com/doku.php? Id = zh_cn: get_started_install_intellij_idea


2. Create a project

After the creation, the main. lua you first see is started to MyApp. lua.

 

require(app.MyApp).new():run()


 

View the MyApp. lua file:
1. require (app. MyApp)
The MyApp. lua code executed here is:

 

Local MyApp = class (MyApp, cc. mvc. AppBase) -- inherits cc. mvc. AppBasereturn MyApp


 

At this time, you get the MyApp class (lua has a lot about class implementation on the Internet ).


2. require (app. MyApp). new ()

After MyApp. new () is executed, the Code executed is:

 

function MyApp:ctor()    MyApp.super.ctor(self)end


 

Why does new () execute MyApp: ctor ()? See the function class (classname, super) method under function. lua:
function cls.new(...)            local instance = cls.__create(...)            -- copy fields from class to native object            for k,v in pairs(cls) do instance[k] = v end            instance.class = cls            instance:ctor(...)            return instanceend



We can see that in the implementation method of the class, a new () method is declared for each created class. The method calls the ctor () constructor method (the ctor is just a name, so after some people think of new, they will certainly call the constructor. lua does not have a class, but we just mimic the class)


3. require (app. MyApp). new (): run ()

At this time

 

function MyApp:run()    CCFileUtils:sharedFileUtils():addSearchPath(res/)    self:enterScene(MainScene)end


 


So we entered MainScene. lua.


For the MyApp. lua file, if I change it to the following format, do you understand the above:
-- Declaration class MyApp = class (MyApp, cc. mvc. appBase) --- class constructor -- function MyApp: ctor () MyApp. super. ctor (self) end --- corresponds to the static create () method of cpp -- function MyApp: create () local myApp = MyApp. new () myApp: init () end --- your method -- @ param self -- local function launchMainScene (self) CCFileUtils: sharedFileUtils (): addSearchPath (res/) self: enterScene (MainScene) end --- init Method -- function MyApp: init () -- add code here launchMainScene (self) end




The corresponding main. lua converts the original require (app. MyApp). new (): run ()

To:

 

require(app.MyApp)MyApp:create()


 


Is it easier for you to understand, haha.


3. MainScene. lua
Why can I switch the scenario of enterScene (MainScene?
Let's take a look at the AppBase of the parent class of MyApp:

 

function AppBase:enterScene(sceneName, args, transitionType, time, more)    local scenePackageName = self. packageRoot .. .scenes. .. sceneName    local sceneClass = require(scenePackageName)    local scene = sceneClass.new(unpack(totable(args)))    display.replaceScene(scene, transitionType, time, more)end

 

In this way, you can understand why MainScene can be called even if no require file exists. Of course, you should pay attention to the file path of require, which is the app/scenes folder written by scene by default.

All right, the rest should be able to see why based on the above ideas. I will not list them one by one.

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.