cocos2dx -lua 面向对象-转
转自: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 面向对象-转的更多相关文章
- cocos2dx lua 绑定之二:手动绑定自定义类中的函数
cococs2dx 3.13.1 + vs2013 + win10 1.首先按照<cocos2dx lua 绑定之一:自动绑定自定义类>绑定Student类 2.在Student类中增加一 ...
- 【cocos2d-x + Lua(1) 绑定Lua并使用tolua++】
为什么要使用Lua进行游戏开发?转载请注明出处http://www.cnblogs.com/zisou/p/cocos2dx-lua1.html 上面一个问题我觉得在我们使用Lua之前需要深入思考的, ...
- Cocos2dx+lua合适还是Cocos2dx+js合适?
问题: 开发cocos2dx手游Cocos2dx+lua合适还是Cocos2dx+js合适 百牛信息技术bainiu.ltd整理发布于博客园 回答: 作者:廖宇雷链接:https://www.zhih ...
- Lua面向对象----类、继承、多继承、单例的实现
(本文转载)学习之用,侵权立删! 原文地址 http://blog.csdn.net/y_23k_bug/article/details/19965877?utm_source=tuicool&a ...
- cocos2d-x lua绑定解析
花了几天时间看了下cocos2d-x lua绑定那块,总算是基本搞明白了,下面分三部分解析lua绑定: 一.lua绑定主要用到的底层函数 lua绑定其本质就是有一个公用的lua_Stack来进行C和L ...
- cocos2d-x + Lua接入iOS原生SDK的实现方案[转]
相信很多朋友在使用cocos2d-x+lua开发游戏时都遇到过接入iOS原生SDK的问题,比如常见的接应用内支付SDK,广告SDK或是一些社交平台SDK等等,我也没少接过这类SDK.这篇文章主要是对我 ...
- Cocos2dx lua 3D实例代码
用cocoside 创建一个项目 cocos2dx lua 项目即可 ,然后替换掉gamescene 就可以,具体效果还有函数的参数,相信大家一看就明白.简单说下ide 创建的 cocos lua 项 ...
- RichLabel基于Cocos2dx+Lua v3.x
RichLabel 简介 RichLabel基于Cocos2dx+Lua v3.x解析字符串方面使用了labelparser,它可以将一定格式的字符串,转换为lua中的表结构扩展标签极其简单,只需添加 ...
- cocos2d-x lua table数据存储
cocos2d-x lua table数据存储 version: cocos2d-x 3.6 1. 将table转为json http://blog.csdn.net/songcf_faith/art ...
随机推荐
- 【SPOJ-QTREE3】树链剖分
http://www.spoj.com/problems/QTREE3/ 时间限制:2s 代码长度限制:50000B 内存限制:1536MB [题目描述] 给出N个点的一棵树(N-1条边 ...
- nginx重启失败
参考: http://www.bubuko.com/infodetail-1742262.html Starting nginx: nginx: [emerg] bind() to 0.0.0.0:8 ...
- (转)自动安装VIM插件
转自: http://xwz.me/wiki/doku.php?id=vim:plugins 我的插件列表 把下面GetLatestVimScripts.dat放进~/.vim/GetLatest/目 ...
- 教你从头到尾利用DQN自动玩flappy bird(全程命令提示,GPU+CPU版)【转】
转自:http://blog.csdn.net/v_JULY_v/article/details/52810219?locationNum=3&fps=1 目录(?)[-] 教你从头到尾利用D ...
- kernel_read【转】
转自:http://blog.csdn.net/echoisland/article/details/7101097http://lxr.oss.org.cn/source/fs/exec.c 798 ...
- StringUtils.isEmpty()和StringUtils.isBlank() 区别
isBlank()判断空的情况包括了isEmpty()的情况,isBlank()不仅判断了 无对象.空对象的情况,而且也判断了无意义的空白字符,比如空格等.
- 使用Bind服务配置DNS服务器
bind是什么 bind是DNS服务器软件 ,他的服务名称是named 功能区分: 正向解析:根据主机名查找对应的IP地址 反向解析:根据IP地址查找对应的主机名(域名) 工作形式上区分: 主服务器: ...
- CSDN博客用Windows Live Writer方法,终于可以离线编辑了!
以下是安装WLW 和相关设置: 1. 下载安装WLW (步骤略,可自行下载,官方下载地址:http://www.microsoft.com/zh-cn/download/details.aspx?id ...
- Android动画之仿美团加载数据等待时,小人奔跑进度动画对话框(附顺丰快递员奔跑效果)
Android动画之仿美团加载数据等待时,小人奔跑进度动画对话框(附顺丰快递员奔跑效果) 首句依然是那句老话,你懂得! finddreams :(http://blog.csdn.net/finddr ...
- AC日记——pigs poj 1149
POJ - 1149 思路: 最大流: 代码: #include <cstdio> #include <cstring> #include <iostream> # ...