当前位置:懂科普 >

综合知识

> lua怎么写

lua怎么写

1. 怎么用Lua写出来 新手

list = {1,,41,341,1,3,5,1,}

lua怎么写

outlist= {};

recordlist={}

for k,v in pairs(list) do

recordlist[v] = outlist[v] or 0;

recordlist[v] = outlist[v] + 1;

end

for k,v in pairs(recordlist) do

table.insert(outlist,{data=k,num=v});

end

table.sort(outlist,function(a,b) if a.num > b.num then return true;else return false; end end);

for k,v in ipairs(outlist) do

print(v.data..":"..v.num.."n");

end

2. lua的接口的写法

扩展Lua的基本方法之一就是为应用程序注册新的C函数到Lua中去。

当我们提到Lua可以调用C函数,不是指Lua可以调用任何类型的C函数(有一些包可以让Lua调用任意的C函数,但缺乏便捷和健壮性)。正如我们前面所看到的,当C调用Lua函数的时候,必须遵循一些简单的协议来传递参数和获取返回结果。

相似的,从Lua中调用C函数,也必须遵循一些协议来传递参数和获得返回结果。另外,从Lua调用C函数我们必须注册函数,也就是说,我们必须把C函数的地址以一个适当的方式传递给Lua解释器。

当Lua调用C函数的时候,使用和C调用Lua相同类型的栈来交互。C函数从栈中获取她的参数,调用结束后将返回结果放到栈中。

为了区分返回结果和栈中的其他的值,每个C函数还会返回结果的个数(the function returns (in C) the number of results it is leaving on the stack.)。这儿有一个重要的概念:用来交互的栈不是全局变量,每一个函数都有他自己的私有栈。

当Lua调用C函数的时候,第一个参数总是在这个私有栈的index=1的位置。甚至当一个C函数调用Lua代码(Lua代码调用同一个C函数或者其他的C函数),每一个C函数都有自己的独立的私有栈,并且第一个参数在index=1的位置。

26.1 C 函数先看一个简单的例子,如何实现一个简单的函数返回给定数值的sin值(更专业的实现应该检查他的参数是否为一个数字):static int l_sin (lua_State *L) { double d = lua_tonumber(L, 1); /* get argument */ lua_pushnumber(L, sin(d)); /* push result */ return 1; /* number of results */}任何在Lua中注册的函数必须有同样的原型,这个原型声明定义就是lua.h中的lua_CFunction:typedef int (*lua_CFunction) (lua_State *L);从C的角度来看,一个C函数接受单一的参数Lua state,返回一个表示返回值个数的数字。所以,函数在将返回值入栈之前不需要清理栈,函数返回之后,Lua自动的清除栈中返回结果下面的所有内容。

我们要想在Lua使用这个函数,还必须首先注册这个函数。我们使用lua_pushcfunction来完成这个任务:他获取指向C函数的指针,并在Lua中创建一个function类型的值来表示这个函数。

一个quick-and-dirty的解决方案是将这段代码直接放到lua.c文件中,并在调用lua_open后面适当的位置加上下面两行:lua_pushcfunction(l, l_sin);lua_setglobal(l, "mysin");第一行将类型为function的值入栈,第二行将function赋值给全局变量mysin。这样修改之后,重新编译Lua,你就可以在你的Lua程序中使用新的mysin函数了。

在下面一节,我们将讨论以比较好的方法将新的C函数添加到Lua中去。对于稍微专业点的sin函数,我们必须检查sin的参数的类型。

有一个辅助库中的luaL_checknumber函数可以检查给定的参数是否为数字:当有错误发生的时候,将抛出一个错误信息;否则返回作为参数的那个数字。将上面我们的函数稍作修改:static int l_sin (lua_State *L) { double d = luaL_checknumber(L, 1); lua_pushnumber(L, sin(d)); return 1; /* number of results */}根据上面的定义,如果你调用mysin('a'),会得到如下信息:bad argument #1 to 'mysin' (number expected, got string)注意看看luaL_checknumber是如何自动使用:参数number(1),函数名("mysin"),期望的参数类型("number"),实际的参数类型("string")来拼接最终的错误信息的。

下面看一个稍微复杂的例子:写一个返回给定目录内容的函数。Lua的标准库并没有提供这个函数,因为ANSI C没有可以实现这个功能的函数。

在这儿,我们假定我们的系统符合POSIX标准。我们的dir函数接受一个代表目录路径的字符串作为参数,以数组的形式返回目录的内容。

比如,调用dir("/home/lua")可能返回{".", "..", "src", "bin", "lib"}。当有错误发生的时候,函数返回nil加上一个描述错误信息的字符串。

#include #include static int l_dir (lua_State *L) { DIR *dir; struct dirent *entry; int i; const char *path = luaL_checkstring(L, 1); /* open directory */ dir = opendir(path); if (dir == NULL) { /* error opening the directory? */ lua_pushnil(L); /* return nil and 。 */ lua_pushstring(L, strerror(errno)); /* error message */ return 2; /* number of results */ } /* create result table */ lua_newtable(L); i = 1; while ((entry = readdir(dir)) != NULL) { lua_pushnumber(L, i++); /* push key */ lua_pushstring(L, entry->d_name); /* push value */ lua_settable(L, -3); } closedir(dir); return 1; /* table is already on top */}辅助库的luaL_checkstring函数用来检测参数是否为字符串,与luaL_checknumber类似。

(在极端情况下,上面的l_dir的实现可能会导致小的内存泄漏。调用的三个Lua函数lua_newtable、lua_pushstring和lua_settable可能由于没有足够的内存而失败。

其中任何一个调用失败都会抛出错误并且终止l_dir,这种情况下,不会调用closedir。正如前面我们所讨论过的,对于大多数程序来说这不算个问题:如果程序导致内存不足,最好的处理方式是立即终止程序。

另外,在29章我们将看到另外一种解决方案可以避免这个问题的发生)26.2 C 函数库一个Lua库实际上是一个定义了一系列。

3. 怎么用LUA语言写一个定时器

原生lua是不提供定时的

因为定时的话 其实就等于引入多线程 而原生lua只支持单线程的

所以可以考虑引入第3方库

比较现成的 socket的timeout 或自己写一个

如果 一定要在lua里实现 可以考虑写个死循环 但是这非常消耗cpu

local t = os.time()

while true do

local time = os.time()

if time - t >= 10 then

t = time

print(t)

end

end

4. Lua 怎么编写输入框

相关的接口有

CCTextFieldTTF:attachWithIME() return type: bool

CCTextFieldTTF:detachWithIME() return type: bool

CCTextFieldTTF:getCharCount() return type: int

CCTextFieldTTF:setColorSpaceHolder(ccColor3B val) return type: void

CCTextFieldTTF:getColorSpaceHolder() return type: ccColor3B

CCTextFieldTTF:setString(const char* text) return type: void

CCTextFieldTTF:getString() return type: const char*

CCTextFieldTTF:setPlaceHolder(const char* text) return type: void

CCTextFieldTTF:getPlaceHolder() return type: const char*

CCTextFieldTTF:textFieldWithPlaceHolder(const char* placeholder, CCSize dimensions, CCTextAlignment alignment, const char* fontName, float fontSize) return type: CCTextFieldTTF

CCTextFieldTTF:textFieldWithPlaceHolder(const char* placeholder, const char* fontName, float fontSize) return type: CCTextFieldTTF制作一个edit控件,首先要用textFieldWithPlaceHolder 创建一个textField

然后要设置setPosition

最后要关联上输入法attachWithIME()

我前一阵子也在用cocos2d做这些东西 但发现cc提供的脚本的接口还是太少

有些功能还是得在CPP下写

比如这个edit也就只能提供输入,连光标都没有的

标签: lua
  • 文章版权属于文章作者所有,转载请注明 https://dongkepu.com/zonghezhishi/knzker.html