【已解决】quick中自定义导出lua类如何在项目中使用

今天根据官方教程弄了个自定义的C++类,导出lua成功了。但是教程中没有如何在qucik中使用的自定义类部分。我导出的lua已经在win下编译成功了。只是在quick中调用会报错。请官方人员指教。
我的环境是win7系统 + cocos2dx3.2 + quickcocos2dx 3.3

最后附上custom_api.ini文件内容,由于不能上传只能直接粘了。。。

the prefix to be added to the generated functions. You might or might not use this in your own

templates

prefix = custom_api

create a target namespace (in javascript, this would create some code like the equiv. to ns = ns || {})

all classes will be embedded in that namespace

target_namespace = my

android_headers = -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/include
android_flags = -D_SIZE_T_DEFINED_

clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include
clang_flags = -nostdinc -x c++ -std=c++11 -U SSE

cocos_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/platform/android -I%(cocosdir)s/cocos/editor-support -I%(cocosdir)s/external
cocos_flags = -DANDROID

cxxgenerator_headers =

extra arguments for clang

extra_arguments = (android_headers)s (clang_headers)s (cxxgenerator_headers)s (cocos_headers)s (android_flags)s (clang_flags)s (cocos_flags)s (extra_flags)s

what headers to parse

headers = %(cocosdir)s/…/runtime-src/Classes/MySprite.h

what classes to produce code for. You can use regular expressions here. When testing the regular

expression, it will be enclosed in “^", like this: "^Menu*”.

classes = MySprite.*

what should we skip? in the format ClassName::

ClassName is a regular expression, but will be used like this: “^ClassName$” functions are also

regular expressions, they will not be surrounded by “^$”. If you want to skip a whole class, just

add a single “" as functions. See bellow for several examples. A special class name is "”, which

will apply to all class names. This is a convenience wildcard to be able to skip similar named

functions from all classes.

skip =

rename_functions =

rename_classes =

for all class names, should we remove something when registering in the target VM?

remove_prefix =

classes for which there will be no “parent” lookup

classes_have_no_parents =

base classes which will be skipped when their sub-classes found them.

base_classes_to_skip =

classes that create no constructor

Set is special and we will use a hand-written constructor

abstract_classes =

Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are ‘yes’ or ‘no’.

script_control_cpp = no

有啊,在Quick的安装目录中就有说明文档
具体是在…\quick\framework\functions.lua

我摘抄部分说明如下:


高级用法

class() 除了定义纯 Lua 类之外,还可以从 C++ 对象继承类。

比如需要创建一个工具栏,并在添加按钮时自动排列已有的按钮,那么我们可以使用如下的代码:


-- 从 cc.Node 对象派生 Toolbar 类,该类具有 cc.Node 的所有属性和行为
local Toolbar = class("Toolbar", function()
    return display.newNode() -- 返回一个 cc.Node 对象
end)

-- 构造函数
function Toolbar:ctor()
    self.buttons = {} -- 用一个 table 来记录所有的按钮
end

-- 添加一个按钮,并且自动设置按钮位置
function Toolbar:addButton(button)
    -- 将按钮对象加入 table
    self.buttons#self.buttons + 1] = button

    -- 添加按钮对象到 cc.Node 中,以便显示该按钮
    -- 因为 Toolbar 是从 cc.Node 继承的,所以可以使用 addChild() 方法
    self:addChild(button)

    -- 按照按钮数量,调整所有按钮的位置
    local x = 0
    for _, button in ipairs(self.buttons) do
        button:setPosition(x, 0)
        -- 依次排列按钮,每个按钮之间间隔 10 点
        x = x + button:getContentSize().width + 10
    end
end

class() 的这种用法让我们可以在 C++ 对象基础上任意扩展行为。

既然是继承,自然就可以覆盖 C++ 对象的方法:


function Toolbar:setPosition(x, y)
    -- 由于在 Toolbar 继承类中覆盖了 cc.Node 对象的 setPosition() 方法
    -- 所以我们要用以下形式才能调用到 cc.Node 原本的 setPosition() 方法
    getmetatable(self).setPosition(self, x, y)

    printf("x = %0.2f, y = %0.2f", x, y)
end

注意: Lua 继承类覆盖的方法并不能从 C++ 调用到。也就是说通过 C++ 代码调用这个 cc.Node 对象的 setPosition() 方法时,并不会执行我们在 Lua 中定义的 Toolbar:setPosition() 方法。

@param string classname 类名
@param 父类或者创建对象实例的函数

@return table

]]

感谢您对本贴的回复,但我要的是自定义C++类导出的lua在quick中的应用,而不是在quick中自己新建的lua类的使用。Thank you all the same.

直接使用貌似没办法
只有你新建一个lua类继承你的自己定义C++类的方法
大不了不做任何实际扩展啊
没什么太大区别

在C++导出后,在lua里面就可以直接使用了啊

我的quick3.3直接用不行,会报错 :12:

:14:不是官方人员路过

在appdeleagte里面include和注册了方法没

有的,都已经注册好了:14:

还是没能实现。。。

求官方教程地址。

请见另一篇http://www.cocoachina.com/bbs/read.php?tid=287598&page=e&#a

我也遇到了这个问题, 如何解决呢, 调用 时出现
:13: attempt to index global ‘MyClass’ (a nil value)

一定是哪里没做对。至少你要简单说说绑定过程、调用代码、出错情况和信息这些吧

自己项目里的编译, 让你能发布到真机上运行。

想quick-x的player里使用,按照一样的方式,把quick-x 的player 也编译一次,和编译自己项目时候步骤基本一致;

如果是想再 cocos code IDE 里使用, 选择项目–右键 – 运行方式 – 运行配置 – 目标平台的runtime路径,选择成自己编译成功的app;

这问题我试验了20多次, 按照上面的方法一定能解决。

— Begin quote from ____

引用第13楼guaguahome于2015-03-11 01:20发表的 :
自己项目里的编译, 让你能发布到真机上运行。

想quick-x的player里使用,按照一样的方式,把quick-x 的player 也编译一次,和编译自己项目时候步骤基本一致;

http://www.cocoachina.com/bbs/job.php?action=topost&tid=287182&pid=1255410

— End quote

Mark 一下 以后在ide里面试试

看看有没有帮助: http://blog.csdn.net/marpools/article/category/2910337

谢谢你的链接,那边讲的内容和官网教程大同小异。不过看完后我把问题找到了。最后发现是引用类的路径问题。已经解决。还是要感谢您!