在lua脚本中使用我们自定义的精灵类
首先创建cocos2dx-lua项目,然后在项目中添加我们的自定义精灵类:这里Himi类名为:HSprite
//
// HSprite.h
// cocos2dx_lua_tests_by_Himi
//
// Created by Himi on 12-8-30.
//
// #ifndef cocos2dx_lua_tests_by_Himi_HSprite_h
#define cocos2dx_lua_tests_by_Himi_HSprite_h #include "cocos2d.h"
using namespace cocos2d; class HSprite : public cocos2d::CCSprite{ public:
static HSprite* createHSprite(const char* _name);
void hspriteInit();
};
#endif
HSprite.cpp
//
// HSprite.cpp
// cocos2dx_lua_tests_by_Himi
//
// Created by Himi on 12-8-30.
//
// #import "HSprite.h" HSprite* HSprite::createHSprite(const char* _name){
HSprite* sp = new HSprite();
if(sp && sp->initWithFile(_name)){
sp->hspriteInit();
sp->autorelease();
return sp;
}
CC_SAFE_DELETE(sp);
return NULL;
} void HSprite::hspriteInit(){
CCMessageBox("create HSprite success", "Himi_Lua");
}
以上代码很简单,继承CCSprite,添加一个自动释放的创建函数(createHSprite)以及一个自定义初始化函数(hspriteInit)
下面我们打开LuaCocos2d.cpp 类,这个类在项目的 libs/lua/cocos2dx_support目录下,如下图:
然后开始添加我们自定义精灵类,让Lua脚本能认识它;
步骤分为3步:
1. 注册我们的自定义类:
在LuaCocos2d.cpp类中搜索“tolua_reg_types”这个函数,然后在其中进行注册:
tolua_usertype(tolua_S,
"HSprite"
);
如下图所示:
第二步:声明我们自定义类的函数:
搜索“tolua_Cocos2d_open”这个函数,然后在其中添加如下代码:
tolua_cclass(tolua_S, "HSprite", "HSprite", "CCSprite", NULL);
tolua_beginmodule(tolua_S,"HSprite");
tolua_function(tolua_S,"createHSprite",tolua_Himi_HSprite_createHSrpite00);
tolua_endmodule(tolua_S);
如下图:
这里开始解释:
首先定义能让脚本认识的类函数,遵循如下:
a) tolua_cclass(tolua_S, “HSprite”, “HSprite”, “CCSprite”, NULL);
tolua_cclass声明哪个类函数,第一个状态值默认:tolua_S
后两个参数:是自定义类类名
再往后是继承的父类类名
b)添加参数开始声明:
tolua_beginmodule(tolua_S,”HSprite”);
c) 添加自定类函数:
tolua_function(tolua_S,”createHSprite”,tolua_Himi_HSprite_createHSrpite00);
第一个参数默认,第二个参数自定义类名,第三个:实现脚本与自定义类之间的转换实现函数
注意,这里有多个函数,可以继续写;
d) 结束自定义函数:
tolua_endmodule(tolua_S);
第三步:实现我们的脚本之间转换函数 tolua_Himi_HSprite_createHSrpite00
实现如下:
/* method: create of class HSprite */
#ifndef TOLUA_DISABLE_tolua_Himi_HSprite_createHSrpite00
static int tolua_Himi_HSprite_createHSrpite00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertable(tolua_S,,"HSprite",,&tolua_err) ||
!tolua_isstring(tolua_S,,,&tolua_err) ||
!tolua_isnoobj(tolua_S,,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
const char* pszFileName = ((const char*) tolua_tostring(tolua_S,,));
{
HSprite* tolua_ret = (HSprite*) HSprite::createHSprite(pszFileName);
int nID = (tolua_ret) ? tolua_ret->m_uID : -;
int* pLuaID = (tolua_ret) ? &tolua_ret->m_nLuaID : NULL;
tolua_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"HSprite");
}
}
return ;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'create'.",&tolua_err);
return ;
#endif
}
#endif //#ifndef TOLUA_DISABLE
这里Himi解释下:
童鞋们可以从第 384行的 #endif 把这个实现函数分为两部分来看,
首先是375~384之间的代码:
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertable(tolua_S,,"HSprite",,&tolua_err) ||
!tolua_isstring(tolua_S,,,&tolua_err) ||
!tolua_isnoobj(tolua_S,,&tolua_err)
)
goto tolua_lerror;
else
#endif
这里是对参数类型的判断:
tolua_isusertable 是否为”第三个参数”自定义类型
tolua_isstring 是否为字符串类型
tolua_isnoobj 结束(没有参数的判断)
然后是386~392之间的代码段:
const char* pszFileName = ((const char*) tolua_tostring(tolua_S,,));
{
HSprite* tolua_ret = (HSprite*) HSprite::createHSprite(pszFileName);
int nID = (tolua_ret) ? tolua_ret->m_uID : -;
int* pLuaID = (tolua_ret) ? &tolua_ret->m_nLuaID : NULL;
tolua_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"HSprite");
}
这里是对脚本代码的解析从而调用的自定义创建的函数
最后我们修改 hello2.lua 脚本中的代码:(创建cocos2dx-lua项目默认Resources下自带的文件)
在脚本最后”– run”下代码修改如下:
-- run
local sceneGame = CCScene:create()
-- sceneGame:addChild(createLayerFram())
-- sceneGame:addChild(createLayerMenu())
sceneGame:addChild(createHimiLayer())
CCDirector:sharedDirector():runWithScene(sceneGame)
这里Himi注视了另个layer的添加,添加了自己的Layer
sceneGame:addChild(createHimiLayer())
然后将Himi自定义方法添加在脚本中,代码如下:
local function createHimiLayer()
local layerH = CCLayer:create() local _font = CCLabelTTF:create("Himi_(cocos2dx-Lua)教程","Arial",)
_font:setPosition(,)
layerH:addChild(_font) --创建自定义类型精灵
local hsprite = HSprite:createHSprite("himi.png")
hsprite:setPosition(,)
hsprite:setScale(1.5)
hsprite:setRotation()
layerH:addChild(hsprite)
return layerH
end
创建自己定义的精灵,然后进行调用缩放,旋转,设置坐标函数。
ok,运行后的接图如下:
在lua脚本中使用我们自定义的精灵类的更多相关文章
- Win32下 Qt与Lua交互使用(四):在Lua脚本中自由执行Qt类中的函数
话接上篇.通过前几篇博客,我们实现在Lua脚本中执行Qt类中函数的方法,以及在Lua脚本中连接Qt对象的信号与槽. 但是,我们也能发现,如果希望在Lua脚本中执行Qt类的函数,就必须绑定一个真正实现功 ...
- Win32下 Qt与Lua交互使用(三):在Lua脚本中connect Qt 对象
话接上文.笔者为了方便使用Lua,自己编写了一个Lua的类.主要代码如下: QLua.h #ifndef QLUA_H #define QLUA_H // own #include "inc ...
- Win32下 Qt与Lua交互使用(二):在Lua脚本中使用Qt类
话接上篇.成功配置好Qt+Lua+toLua后,我们可以实现在Lua脚本中使用各个Qt的类.直接看代码吧. #include "include/lua.hpp" #include ...
- lua脚本中字符串分割split
function split( s, c ) for item in string.gmatch( s, "(.-)"..c) do print(item); end end s ...
- LUA脚本中O(2)级素数查询
--================================================================================================== ...
- 【COCOS2DX-LUA 脚本开发之一】在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途!
[COCOS2DX-LUA 脚本开发之一]在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途! 分类: [Cocos2dx Lua 脚本开发 ] 2012-04-1 ...
- Unity3D热更新之LuaFramework篇[05]--Lua脚本调用c#以及如何在Lua中使用Dotween
在上一篇文章 Unity3D热更新之LuaFramework篇[04]--自定义UI监听方法 中,我对LuaBehaviour脚本进行了扩展,添加了两个新的UI监听方法,也提到最好能单写一个脚本处理此 ...
- Redis中的原子操作(2)-redis中使用Lua脚本保证命令原子性
Redis 如何应对并发访问 使用 Lua 脚本 Redis 中如何使用 Lua 脚本 EVAL EVALSHA SCRIPT 命令 SCRIPT LOAD SCRIPT EXISTS SCRIPT ...
- .net core中加载lua脚本的类库: MoonSharp
前言 MoonSharp是一个支持C#调用lua脚本的类库,支持.net, .net core, mono, unity,因此在.net core中也能够使用,而且加载和调用lua也很方便简单: 官网 ...
随机推荐
- A.Kaw矩阵代数初步学习笔记 3. Binary Matrix Operations
“矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...
- linux下git的简单运用
linux下git的简单运用 windows下也有git,是git公司出的bash,基本上模拟了linux下命令行.许多常用的命令和linux下操作一样.也就是说,windows下的git命令操作和l ...
- mongo&node
///// node install $ sudo apt-get install python-software-properties $ curl -sL https://deb.nodesou ...
- JavaWeb---总结(八)HttpServletResponse对象(二)
一.HttpServletResponse常见应用--生成验证码 1.1.生成随机图片用作验证码 生成图片主要用到了一个BufferedImage类, 生成随机图片范例: 1 package gacl ...
- JS-百钱买百鸡案例-for循环制作
<html> <head> <meta charset="utf-8"/> <title></title> <sc ...
- asp.net下调用Matlab生成动态链接库
对于这次论文项目,最后在写一篇关于工程的博客,那就是在asp.net下调用matlab生成的dll动态链接库.至今关于matlab,c/c++(opencv),c#(asp.net)我总共写了4篇配置 ...
- 10月17日下午MySQl数据库CRUD高级查询
高级查询:1.连接查询 #适用于有外键关系的 没有任何关系没法用select * from Info,Nation #同时查询这俩表并把两表每个数据相互组合,形成笛卡尔积 select * from ...
- vs2013安装闪退及vs2010 vs2013打开时提示 未能完成的操作 及vs2013安装时出现图片后闪退
vs2013打开时提示如上图,vs2010只有 未能完成的操作 这样的提示. 这时.net 4.0开发的程序打开也毫无反应,应该是.net framework出了问题.查看控制面板-卸载程序,发现 ...
- YOURPHP的分页完整版
html代码 <?php print_r($ser['searchtype']);?> <select name="searchtype"> <opt ...
- Shader_2[杂]
三个shader,平滑滤波.锐化滤波和高斯模糊 http://tieba.baidu.com/p/3791791688 Unity3D研究院之自制批量修改Shader插件(五十七) http://ww ...