转自:http://www.himigame.com/lua-game/1282.html

上一篇中,向童鞋们介绍了如何自定义类binding到Lua中供给使用的教程,那么本篇将介绍利用OOP思想在在Lua中进行创建一个自定义类。

首先Himi来向大家讲解如何在Lua中不binding来自定义lua类,其实这种方式在Cocos2dx的Lua Samples已经为我们做好了例子,就看童鞋们是否认真阅读了。此示例路径在你解压cocos2dx引擎包下的cocos2d-2.1rc0-x-2.1.2/samples/Lua/TestLua 中的 TouchesTest ,如下图:

在这个示例中Ball.lua 与 Paddle.lua 分别作为对象进行的Lua编写,还没有看到过的童鞋请自行看下吧。

闲言少叙,下面详细介绍使用Lua来自定义lua类的步骤:

 第一步:

     我们到Cocos2dx引擎目录下的 samples/Lua/TestLua/Resources/luaScript  下找到“extern.lua” 文件,其内容如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
--Create an class.
function class(classname, super)
    local superType = type(super)
    local cls
 
    if superType ~= "function" and superType ~= "table" then
        superType = nil
        super = nil
    end
 
    if superType == "function" or (super and super.__ctype == 1) then
        -- inherited from native C++ Object
        cls = {}
 
        if superType == "table" then
            -- copy fields from super
            for k,v in pairs(super) do cls[k] = v end
            cls.__create = super.__create
            cls.super    = super
        else
            cls.__create = super
        end
 
        cls.ctor    = function() end
        cls.__cname = classname
        cls.__ctype = 1
 
        function cls.new(...)
            local instance = cls.__create(...)
            -- copy fields from class to native object
            for k,v in pairs(cls) do instance[k] = v end
            instance.class = cls
            instance:ctor(...)
            return instance
        end
 
    else
        -- inherited from Lua Object
        if super then
            cls = clone(super)
            cls.super = super
        else
            cls = {ctor = function() end}
        end
 
        cls.__cname = classname
        cls.__ctype = 2 -- lua
        cls.__index = cls
 
        function cls.new(...)
            local instance = setmetatable({}, cls)
            instance.class = cls
            instance:ctor(...)
            return instance
        end
    end
 
    return cls
end
 
function schedule(node, callback, delay)
    local delay = CCDelayTime:create(delay)
    local callfunc = CCCallFunc:create(callback)
    local sequence = CCSequence:createWithTwoActions(delay, callfunc)
    local action = CCRepeatForever:create(sequence)
    node:runAction(action)
    return action
end
 
function performWithDelay(node, callback, delay)
    local delay = CCDelayTime:create(delay)
    local callfunc = CCCallFunc:create(callback)
    local sequence = CCSequence:createWithTwoActions(delay, callfunc)
    node:runAction(sequence)
    return sequence
end

这个Lua中提供了3个方法: 第二个函数与第三个函数分别是更新函数与序列动作函数,很easy 不多说。

我们主要关注的是 第一个函数:

class(classname,super)   ,  此函数可以用于创建我们自定义lua类

第一个参数:自定义类名

第二个参数: 自定义类所继承的父类

至于其中的实现,大家需要掌握Lua的语言与程序设计,比较容易理解的。

第二步:我们自定义一个精灵类,且继承CCSprite,其文件名Himi这里随便起的是“MySprite.lua” ,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
require "extern"   --导入模板,作用调用其class函数
 
MySprite = class("MySprite",
    function(fileName)
        return CCSprite:create(fileName)
    end
)
 
MySprite.__index = MySprite   -- 用于访问
 
MySprite.type = 0    -- 自定义属性
 
function MySprite:createMS(fileName,_type)      --自定义构造函数
    local mySprite = MySprite.new(fileName)
    mySprite:myInit(_type)
    return mySprite
end
 
function MySprite:myInit(_type)    --自定义函数
    self.type =_type
end

比较简单不赘述。

cocos2dx -lua 面向对象-转的更多相关文章

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

    cococs2dx 3.13.1 + vs2013 + win10 1.首先按照<cocos2dx lua 绑定之一:自动绑定自定义类>绑定Student类 2.在Student类中增加一 ...

  2. 【cocos2d-x + Lua(1) 绑定Lua并使用tolua++】

    为什么要使用Lua进行游戏开发?转载请注明出处http://www.cnblogs.com/zisou/p/cocos2dx-lua1.html 上面一个问题我觉得在我们使用Lua之前需要深入思考的, ...

  3. Cocos2dx+lua合适还是Cocos2dx+js合适?

    问题: 开发cocos2dx手游Cocos2dx+lua合适还是Cocos2dx+js合适 百牛信息技术bainiu.ltd整理发布于博客园 回答: 作者:廖宇雷链接:https://www.zhih ...

  4. Lua面向对象----类、继承、多继承、单例的实现

    (本文转载)学习之用,侵权立删! 原文地址   http://blog.csdn.net/y_23k_bug/article/details/19965877?utm_source=tuicool&a ...

  5. cocos2d-x lua绑定解析

    花了几天时间看了下cocos2d-x lua绑定那块,总算是基本搞明白了,下面分三部分解析lua绑定: 一.lua绑定主要用到的底层函数 lua绑定其本质就是有一个公用的lua_Stack来进行C和L ...

  6. cocos2d-x + Lua接入iOS原生SDK的实现方案[转]

    相信很多朋友在使用cocos2d-x+lua开发游戏时都遇到过接入iOS原生SDK的问题,比如常见的接应用内支付SDK,广告SDK或是一些社交平台SDK等等,我也没少接过这类SDK.这篇文章主要是对我 ...

  7. Cocos2dx lua 3D实例代码

    用cocoside 创建一个项目 cocos2dx lua 项目即可 ,然后替换掉gamescene 就可以,具体效果还有函数的参数,相信大家一看就明白.简单说下ide 创建的 cocos lua 项 ...

  8. RichLabel基于Cocos2dx+Lua v3.x

    RichLabel 简介 RichLabel基于Cocos2dx+Lua v3.x解析字符串方面使用了labelparser,它可以将一定格式的字符串,转换为lua中的表结构扩展标签极其简单,只需添加 ...

  9. cocos2d-x lua table数据存储

    cocos2d-x lua table数据存储 version: cocos2d-x 3.6 1. 将table转为json http://blog.csdn.net/songcf_faith/art ...

随机推荐

  1. loj6087 毒瘤题

    传送门:https://loj.ac/problem/6087 [题解] 这垃圾题目卡空间啊... k=1相信大家都会,把所有数异或起来就是答案了. 考虑k=2,把所有数异或起来得到两个答案数的异或值 ...

  2. [bzoj1251]序列终结者——splay

    题目大意 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技 ...

  3. bzoj 1041 数学推理

    原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1041 我们只需要求第一象限内(不包括坐标轴)的点数然后ans=ans*4+4就好了 首先我 ...

  4. cygwin与vim配置

    参考 http://www.jeepshoe.org/810958442.htm cygwin安装包管理器 通过终端安装apt-cyg之前选要安装以下软件包wget tar gawk bzip2 ap ...

  5. C#实例:Unity依赖注入使用

    http://jingyan.baidu.com/article/c74d6000840b260f6b595d78.html

  6. 各种编码UNICODE、UTF-8、ANSI、ASCII、GB2312、GBK详解

    来自:http://blog.csdn.net/lvxiangan/article/details/8151670 ------------------------------------------ ...

  7. selenium+python自动化79-文件下载(SendKeys)【转载】

    前言 文件下载时候会弹出一个下载选项框,这个弹框是定位不到的,有些元素注定定位不到也没关系,就当没有鼠标,我们可以通过键盘的快捷键完成操作. SendKeys库是专业的处理键盘事件的,所以这里需要用S ...

  8. Android局域网访问webservice以及其中的一些问题

    应老师的要求,要做个安卓app,实现备份app上的数据到服务器上的mongodb上,网上搜了下相关的实现方式.利用webservice技术,具体来说就是客户端直接调用服务器端的接口.之前从来没接触这玩 ...

  9. asp.net数据类型--泛型

    asp.net有很多的数据类型,同时c#等均是强数据类型,在使用的过程,存在因数据类型不一致,在编译时通过,在使用过程中出错的情况,因此从2.0起,增加泛型这种类型.这种类型,在定义时不指定类型,而在 ...

  10. Wannafly挑战赛17 A 走格子【矩阵行走/模拟】

    [链接]:A [分析]:可以设置方向数组和标记数组.当不合法(越界/访问过)就转向,转向可以用now=(now+1)%4 [代码]: #include <bits/stdc++.h> #d ...