Cocos custom classes bound to Lua

Source: Internet
Author: User

1: Follow the instructions of \frameworks\cocos2d-x\tools\tolua under Readme.mdown step by step. Be sure to do as described in the document, I have been the pit of the father's things very miserable. (check if this is a good way to do this: Run the next genbindings.py to see if you can generate Liblua files in the Auto directory.) If you can, then your environment configuration will succeed. )

2: Write a custom C + + class

Example:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:14PX;" > #pragma once#include "cocos2d.h" Using_ns_cc;class myclass:public ref{public:myclass (void); ~myclass (void); bool Init () {return true;}; Create_func (MyClass); int foo (int i);}; </span>

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:14PX;" > #include "MyClass.h" myclass::myclass (void) {}myclass::~myclass (void) {}int myclass::foo (int i) {return i + 100;} </span>

3: In the \frameworks\cocos2d-x\tools\tolua directory you can see a lot of. ini files and a Genbindings.python file.

To copy an. ini file, I rename: Myclass.ini. Copy a python file and rename it as: Mygenbindings.python.

Modify two configuration files:

The python file needs to be modified:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:14PX;" > Cmd_args = {' Cocos2dx.ini ': (' cocos2d-x ', ' Lua_cocos2dx_auto '), ' Cocos2dx_extension.ini ': (' coc Os2dx_extension ', ' Lua_cocos2dx_extension_auto '), ' Cocos2dx_ui.ini ': (' cocos2dx_ui ', ' lua_cocos2dx_ui                     _auto '), ' Cocos2dx_studio.ini ': (' Cocos2dx_studio ', ' Lua_cocos2dx_studio_auto '), ' Cocos2dx_spine.ini ': (' cocos2dx_spine ', ' Lua_cocos2dx_spine_auto '), ' Cocos2dx_physics.ini ': (' Cocos 2dx_physics ', ' Lua_cocos2dx_physics_auto '), ' Cocos2dx_experimental_video.ini ': (' Cocos2dx_experimenta L_video ', ' Lua_cocos2dx_experimental_video_auto '), ' Cocos2dx_experimental.ini ': (' cocos2dx_experiment Al ', ' Lua_cocos2dx_experimental_auto '), ' Cocos2dx_controller.ini ': (' Cocos2dx_controller ', ' Lua_cocos 2dx_controller_auto '),}</span>

Into:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:14PX;" >cmd_args = {' Myclass.ini ': (' MyClass ', ' Lua_myclass_auto '),                     }</span>

To modify the Myclass.ini file:

[MyClass] ( It's important to be the same as the file name.) I've been in this hole for a long time ) This is the first line

prefix = MyClass

Target_namespace = (my no namespace)

headers =% (Cocosdir) s/: /runtime-src/classes/myclass.h (This is my classpath, and appdelegate together, according to your own path configuration bar)

Classes = MyClass

The following skip =, Rename_functions = And so on are all turned empty.

This makes it possible to generate Lua and Cocos intermediate files. Just like Lua_cocos2dx_auto.cpp and HPP. Since we did not change the build path of the intermediate file in the Python file, it should be in the same directory as the Lua_cocosdx_auto.

Register our custom classes and add code to the AppDelegate.cpp file:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:14PX;" >auto engine = Luaengine::getinstance (); Register_all_myclass (Engine->getluastack ()->getluastate ());// Add this line of code here </span>
This allows us to register our custom classes.

Compile the project and find an error:

<pre name= "code" class= "HTML" ><span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:14PX;" > "int __cdecl register_all_myclass (struct lua_state *)" ([email Protected]@[email protected]@@z), the symbol in the function "public: virtual bool __thiscall appdelegate::applicationdidfinishlaunching (void) "([email protected]@ @UAE_NXZ) is referenced </span >

Workaround: Add the two files you just generated to the Liblua project and regenerate the Liblua project. It's done!!

4: Call our class in Lua code.

In Hello2.lua's main (), add:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:14PX;" >local test = Myclass:create () print ("The first Cocos bind Lua result".) Test:foo (2)) </span>



Found out the answer we asked for. This completes the Cocos custom class binding to Lua.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Cocos custom classes bound to Lua

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.