标签: lua

引用Lua中的父类(Corona)

在我的game.lua文件中我有这个:

function new()

    local obj = display.newGroup();

    currentLevel = Level.new(1);
    currentLevel.game = obj; //also tried currentLevel.game = self;

    function obj:replay()
            print("game - replay")
    end

    return obj;

end
Run Code Online (Sandbox Code Playgroud)

在Level lua文件中,我尝试在game.lua中调用重播函数:

game = {};
...
game:replay();
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:尝试调用方法'重放'(一个零值)

如何在level.lua中保留对游戏文件的引用?

lua coronasdk

0
推荐指数
1
解决办法
662
查看次数

将变量传递给Lua中的函数

我是Lua的新手,所以(很自然地)我遇到了我试图编程的第一件事.我正在使用Corona Developer软件包提供的示例脚本.这是我试图调用的函数的简化版本(删除了无关材料):

function new( imageSet, slideBackground, top, bottom )

    function g:jumpToImage(num)

            print(num)
            local i = 0
            print("jumpToImage")
            print("#images", #images)
            for i = 1, #images do
                if i < num then
                    images[i].x = -screenW*.5;
                elseif i > num then
                    images[i].x = screenW*1.5 + pad
                else
                    images[i].x = screenW*.5 - pad
                end
            end
            imgNum = num
            initImage(imgNum)
        end

end
Run Code Online (Sandbox Code Playgroud)

如果我尝试像这样调用该函数:

local test = slideView.new( myImages )
test.jumpToImage(2)
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

尝试将数字与零比较

在第225行.似乎"num"没有被传递到函数中.为什么是这样?

lua function coronasdk

0
推荐指数
1
解决办法
2340
查看次数

指定lua参数应该是副本还是引用

我想知道是否有办法指定是否应复制或仅引用lua函数的参数.颜色是表示颜色的对象.

例如,使用此代码

function editColor(col)
    col.r = 0
    print(tostring(col.r))
end

color = Color(255, 0, 0)
print(tostring(color.r))
editColor(color)
print(tostring(color.r))
Run Code Online (Sandbox Code Playgroud)

使输出

255
0
0
Run Code Online (Sandbox Code Playgroud)

所以col是颜色的"参考",但是这段代码:

function editColor(col)
    col = Color(0, 0, 0)
    print(tostring(col.r))
end

color = Color(255, 0, 0)
print(tostring(color.r))
editColor(color)
print(tostring(color.r))
Run Code Online (Sandbox Code Playgroud)

使此输出

255
0
255
Run Code Online (Sandbox Code Playgroud)

所以这里的颜色被复制了.

有没有办法强制复制或引用参数?就像&C++中的运算符一样?

lua reference function

0
推荐指数
1
解决办法
6000
查看次数

lighttpd运行lua脚本时遇到问题

我一直在尝试在lighttpd中运行lua脚本.我正在运行gentoo,我使用lua use标志安装了lighttpd.mod_magnet.so应该是它应该的位置.当我尝试启动服务器时,我收到此错误:

2011-06-03 15:55:32: (configfile.c.912) source: /etc/lighttpd/lighttpd.conf line: 43 pos: 68 parser failed somehow near here: (
Run Code Online (Sandbox Code Playgroud)

这是我的配置文件:

###############################################################################
# Default lighttpd.conf for Gentoo.
# $Header: /var/cvsroot/gentoo-x86/www-servers/lighttpd/files/conf/lighttpd.conf,v 1.5     2010/11/18 15:13:47 hwoarang Exp $
###############################################################################

# {{{ variables
var.basedir  = "/var/www/localhost"
var.logdir   = "/var/log/lighttpd"
var.statedir = "/var/lib/lighttpd"
# }}}

# {{{ modules
# At the very least, mod_access and mod_accesslog should be enabled.
# All other modules should only be loaded if necessary.
# NOTE: the order of modules is important. …
Run Code Online (Sandbox Code Playgroud)

lua lighttpd gentoo

0
推荐指数
1
解决办法
2260
查看次数

Lua:加载文件后全局函数失败

我正在尝试调用名为test2.lua的lua文件中的函数.这是test2.lua的内容:

function abc(path)
 t = {}
 table.insert(t, "a")
 return t
end
Run Code Online (Sandbox Code Playgroud)

如您所见,它需要一个输入并返回一个字符串.

这是我的C代码.这很简单.然而我调用getglobal以调用该函数不起作用... lua_getglobal说我测试时它不是一个函数...这是什么原因?不应该abc是源文件中可返回的全局函数吗?为什么它只为这个全球性找到零呢?

L = lua_open();
luaL_openlibs(L);
luaL_loadfile(L, "src/test2.lua");

lua_getglobal(L, "abc");

lua_pushstring(L, "coollll");

int error = 0;
if ((error = lua_pcall(L, 1, 1, 0)) == 0)
{
    std::cout << "cool";
}
Run Code Online (Sandbox Code Playgroud)

编辑:

调用lua_getglobal导致我的程序无论使用loadfile还是dofile都会破坏控制...任何想法为什么?

lua_getglobal崩溃程序

c++ lua

0
推荐指数
1
解决办法
2452
查看次数

2个向量之间的度数?

这个学位计算有什么问题?

我选择了2个矢量,它们的度数应该是90°.

我用cos,cos ^ -1尝试了它,没有cos计算.但是我的代码的输出是:
0(没有cos计算)
1(cosinus)
1.5707963267949(cosinus ^ -1(acos))

local square = math.sqrt;
local weaponVector, hitVector = , {x = 0, y = 0, z = 1,}, {x = 0, y = 1, z = 0,};
local xW, yW, zW = weaponVector.x, weaponVector.y, weaponVector.z;
local xH, yH, zH = hitVector.x, hitVector.y, hitVector.z;
local angleBetweenWeaponAndHitDirection = math.acos(math.abs(xW*xH+yW*yH+zW*zH)
            / (square(xW*xW+yW*yW+zW*zW) * square(xH*xH+yH*yH+zH*zH)));
if (angleBetweenWeaponAndHitDirection>180) then
    angleBetweenWeaponAndHitDirection = 360-angleBetweenWeaponAndHitDirection;
end
print(angleBetweenWeaponAndHitDirection)
print(math.cos(angleBetweenWeaponAndHitDirection))
print(math.acos(angleBetweenWeaponAndHitDirection))
Run Code Online (Sandbox Code Playgroud)

math lua vector

0
推荐指数
1
解决办法
185
查看次数

如何覆盖已编译的(?)脚本?

如果我创建一个简单的lua脚本文件:
test.lua

#!/usr/bin/env lua

local var = math.random(100)
print(var)
Run Code Online (Sandbox Code Playgroud)

它在调用时将始终打印相同的值,即使它在每次调用时确实应该是一个新的随机值.
我的猜测是脚本编译(?)为例如:

#!/usr/bin/env lua

local var = 82
print(var)
Run Code Online (Sandbox Code Playgroud)

(如果82是第一次调用时的随机值)

我可以做什么来确保每次调用脚本时都会编译一个新的(?)字节码(?),这样我就可以在每次执行时得到一个新的随机值剧本?

我添加(?)的原因是我甚至不知道脚本是否完全被编译或者它们被调用时会发生什么,但是这样的事情显然正在发生,因为我总是得到相同的值.

lua

0
推荐指数
1
解决办法
95
查看次数

Lua和假Typecast

我正在使用Lua和C++(使用LuaWrapper),我遇到了麻烦.假设我有这些课程:

class Bar{...};
class Foo
{
    ...
    std::map<std::string,Bar*> _barlist;
    ...
    Bar* getBar(std::string key)
    {
        return _barlist[key];
    }
}

class BarDerived1 : public Bar{...}
class BarDerived2 : public Bar{...}
Run Code Online (Sandbox Code Playgroud)

我可以访问Foo和Foo::getBar()从Lua 访问,事实是返回的对象的类型是Bar,我无法访问其BarDerivedX方法.

据我所知,Lua中的Objects/Classes只是MetaTables,其功能分配给键,所以我想知道是否有办法将这些额外的函数从BarDerivedX类复制到Bar对象(甚至可以工作吗?),也是这是最好的方法,或者我应该在让Lua访问之前用C++编译它?

c++ lua casting

0
推荐指数
1
解决办法
526
查看次数

Lua/C绑定,从lua绑定

我正在构建一个lua事件系统(在lua中),但是我希望能够从C中触发事件,我想将C函数绑定到lua函数,这样C函数可以在lua中触发事件,我打算用:

lua_register
Run Code Online (Sandbox Code Playgroud)

功能; 但是我似乎找不到像这样绑定我的lua函数的方法,看起来我需要一个lua函数来做同样的事情,但是从lua方面来说,我正在考虑做一些hack,将C函数绑定到lua,简单地调用'lua_register',但这对我来说似乎有些不安全.

那我该怎么做呢?

c lua binding

0
推荐指数
1
解决办法
1371
查看次数

我怎么知道我的lua脚本在Wireshark中运行?

我准备在Lua为Wireshark编写一个解剖器,但是想先测试一个简单的hello world.但是怎么知道它在运行?我可以在某处看到调试打印吗?这是脚本:

-- hello.lua
print("Hello World!")
Run Code Online (Sandbox Code Playgroud)

我在Windows上运行Wireshark 0.99.7.

lua wireshark

0
推荐指数
1
解决办法
4620
查看次数

标签 统计

lua ×10

c++ ×2

coronasdk ×2

function ×2

binding ×1

c ×1

casting ×1

gentoo ×1

lighttpd ×1

math ×1

reference ×1

vector ×1

wireshark ×1