cococs2dx 3.13.1 + vs2013 + win10

1.首先按照《cocos2dx lua 绑定之一:自动绑定自定义类》绑定Student类

2.在Student类中增加一个用于测试手动绑定的函数manual_call

①Student.h中增加函数

    //手动绑定调用函数
void manual_call();

②Student.cpp中增加函数实现

//和自动绑定相比,只增加了这个函数
void Student::manual_call()
{
std::cout << " manual call function " << std::endl;
}

3.在libluacocos2d项目中新增两个手动绑定的处理文件lua_cocos2dx_student_manual.h和lua_cocos2dx_student_manual.cpp

①lua_cocos2dx_student_manual.h

#pragma once

#ifdef __cplusplus
extern "C" {
#endif
#include "tolua++.h"
#ifdef __cplusplus
}
#endif TOLUA_API int register_student_moudle(lua_State* L);

②lua_cocos2dx_student_manual.cpp

#include "scripting/lua-bindings/manual/user_define/lua_cocos2dx_student_manual.hpp"
#include "scripting/lua-bindings/auto/lua_userdefine_student_auto.hpp"
#include "user_define/Student.h" #include "scripting/lua-bindings/manual/tolua_fix.h"
#include "scripting/lua-bindings/manual/LuaBasicConversions.h"
#include "scripting/lua-bindings/manual/cocos2d/LuaScriptHandlerMgr.h"
#include "scripting/lua-bindings/manual/CCLuaValue.h"
#include "scripting/lua-bindings/manual/CCLuaEngine.h"
#include "base/CCEventListenerFocus.h" //调用函数
static int tolua_student_test_function(lua_State* L)
{
if (nullptr == L)
return ; Student** s = (Student**)luaL_checkudata(L, , "Student");
luaL_argcheck(L, s != NULL, , "invalid user data");
(*s)->manual_call();
return ;
} //注册函数
static void regist_student_manual_functions(lua_State* L)
{
//找到对应自动注册的类
lua_pushstring(L, "Student");
lua_rawget(L, LUA_REGISTRYINDEX);
if (lua_istable(L,-))
{
tolua_function(L, "manual_call", tolua_student_test_function); //将函数绑定到Student类中
}
lua_pop(L, );
} int register_student_moudle(lua_State* L)
{
lua_getglobal(L, "_G");
if (lua_istable(L,-))//stack:...,_G,
{
register_all_userdefine_student(L);
regist_student_manual_functions(L);
}
lua_pop(L, ); return ;
}

4.将函数注册到lua中,找到libluacocos2d项目中

①在CCLuaStack.cpp文件增加头文件引用

#include "scripting/lua-bindings/manual/user_define/lua_cocos2dx_student_manual.hpp"

②在init函数里增加函数注册到Lua

先屏蔽注册自动函数的相关代码,使用register_student_moudle同时注册自动绑定和手动绑定的函数

//register_all_userdefine_student(_state);
register_student_moudle(_state);

5.重新编译项目,在Lua里使用

local student = Student:new()
student:manual_call()

6.如果不使用自动绑定,全部使用手动绑定,可参考下面两篇文章直接进行绑定

Lua和C++交互 学习记录之八:C++类注册为Lua模块

Lua和C++交互 学习记录之九:在Lua中以面向对象的方式使用C++注册的类

cocos2dx lua 绑定之二:手动绑定自定义类中的函数的更多相关文章

  1. cocos2dx lua 绑定之一:自动绑定自定义类中的函数

    cococs2dx 3.13.1 + vs2013 + win10 1.首先定义C++类Student 在cocos2d-x\cocos文件夹下新建一个user_define的文件夹放置两个文件. 注 ...

  2. cocos2dx——lua自己主动和手动绑定

    [自己主动绑定] 參考:http://my.oschina.net/skyhacker2/blog/298397 主要是通过引擎自带的tools/tolua,主要过程例如以下: 1.编写好要导出的c+ ...

  3. Cocos2d-x——CocosBuilder官方帮助文档翻译1 使用自定义类

    原创:请注明转载! 在Cocos2d-x中使用CocosBuilder 使用自定义类 CocosBuilder的使用方法是通过自定义类.在CocosBuilder中选中一个对象并在属性栏中输入自定义类 ...

  4. 关于在App_Code文件夹自定义类中Session无法使用

    由于前台页面需要调用App_Code中自定义类的函数,但在自定义类中找不到Session,解决方法如下: 新建一个类session,并自己定义函数GetSession(),引用命名空间 System. ...

  5. Win32下 Qt与Lua交互使用(四):在Lua脚本中自由执行Qt类中的函数

    话接上篇.通过前几篇博客,我们实现在Lua脚本中执行Qt类中函数的方法,以及在Lua脚本中连接Qt对象的信号与槽. 但是,我们也能发现,如果希望在Lua脚本中执行Qt类的函数,就必须绑定一个真正实现功 ...

  6. spring自定义类中@AutoWired标识的元素注入为null

    最近在做项目的时候,发现程序运行的时候有一个nullpointer exception,一脸懵逼因为感觉程序没什么逻辑.后来发现是因为new出来的component不会自动注入它的元素. 现象:@Co ...

  7. 学习CodeIgniter框架之旅(二)继承自定义类

    在很多情况下,框架类并不能满足项目的需求,这时候需要程序要自定义一些类,比如说基类等等,对比了TP框架,CI框架目前好像还没加入命名空间,这点TP做得比较好,不用特殊的处理就可以随便继承自定义的类,只 ...

  8. unity, 自定义类中使用print

    在unity脚本中自定义c#类,而且不继承MonoBehaviour的话,若还想在其中使用print函数,可以用MonoBehaviour.print(...).

  9. 自定义的类型放入STL的set中,需要重载自定义类中的“<”符号(转)

    在以前学习STL的时候,曾经学到过,如果要将自定义的类型放入到set中的话,就需要重载“<”符号,原因是set是一个有序的集合,集合会按照“<”比较的大小,默认按照从小到大的顺序排列.假设 ...

随机推荐

  1. Neutron Networking QoS

    目前,Neutron有一个QoS的proposal(https://wiki.openstack.org/wiki/Neutron/QoS#Documents),但是只有Ciscso和NVP插件实现了 ...

  2. windows证书地址

    C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys 用certutil -sto ...

  3. HackerRank "Playing with numbers"

    This is 'Difficult' - I worked out it within 45mins, and unlocked HackerRank Algorithm Level 80 yeah ...

  4. java定时器和多线程实践记录

    这几天因为需要测试mongodb读写分离的问题,因此写了个定时查询程序,并且用到了多线程,以达到定时启动多个线程查询数据库的效果,下边代码记录备忘: package timmer; import ja ...

  5. org.hibernate.id.IdentifierGenerationException: Unknown integral data type for ids : java.lang.String

    org.hibernate.id.IdentifierGenerationException: Unknown integral data type for ids : java.lang.Strin ...

  6. UVA 133 The Dole Queue

    The Dole Queue 题解: 这里写一个走多少步,返回位置的函数真的很重要,并且,把顺时针和逆时针写到了一起,也真的很厉害,需要学习 代码: #include<stdio.h> # ...

  7. openmp并行计算

    #include <omp.h>#include <stdio.h>#include <stdlib.h> void test(int n){ for (int i ...

  8. 转_IOS开源

    http://www.csdn.net/article/2013-06-05/2815530-GitHub-iOS-open-source-projects-one 苹果App Store本身所具有的 ...

  9. Input gameobject vector3 c#

    Input类中的常用方法 bool w=Input.GetKey(KeyCode.W);//检测是否按下键盘W Input.GetKeyDown(KeyCode.W);//表示检测按下时 Input. ...

  10. 关于this指向思考

    事情起因为<js函数式编程指南>里面看到这段话: 上例DB对象中的save方法作为参数传入到fs的readFile,按道理this指向DB,为啥作者的意思是存在this指针可能会指向其他地 ...