Cocos2d-x tutorial (27)-Cocos2d-x 2.2.x tolua ++ usage

Source: Internet
Author: User

 

 

 

Sometimes we need to call methods in the Custom C ++ class in Lua. This process can be implemented using the tolua ++ tool.

The process is as follows:

1. handwritten a c ++ file in your project, xx. h and xx. cpp

2. Open cocos2d-x-2.2.2/tools/tolua ++ and write a. pkg file.

3. register the. pkg file you just compiled in the Cocos2d. pkg file.

4. modify the content of the build. sh File

5. Execute build. sh to generate a new luaco cos2d. cpp file, and we need to introduce the C ++ Class header file defined in luaco cos2d. cpp.

6. You can directly call the method in xx. h In Lua.

 

Specific process:

1. Create a new project and create a new C ++ file in the project, such:

 

2. open the cocos2d-x-2.2.2/tools/tolua ++ and write a corresponding C ++ file. pkg file, the method used in the file is called in Lua, and then save the file to the current directory (use a text editor and save the suffix. pkg) for example:


 

 

3. Locate Cocos2d. pkg in the current directory and open it in the text editor. register the. pkg file you just compiled in the Cocos2d. pkg file.

Add code in the last line:

4.find tolua00000000.mac.zip in the current directory, decompress tolua ++ for mac, and modify build. sh file content, the blue background part needs to be modified, you need to change this to your own path, here is the author path:

5. Open the terminal and execute the build. sh file to generate a new luaco cos2d. cpp file (automatically generated). In addition, we need to introduce the C ++ Class header file defined in luaco cos2d. cpp.

6. Call it in The Lua script. Here I will add the Code directly in hello. lua. Observe the console output:

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.