cocos2dx -lua 面向对象-转
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了cocos2dx -lua 面向对象-转,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3130字,纯文字阅读大概需要5分钟。
内容图文
![cocos2dx -lua 面向对象-转](/upload/InfoBanner/zyjiaocheng/1236/7347956cf3d440039b7916adf246b543.jpg)
转自: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
|
比较简单不赘述。
原文:http://www.cnblogs.com/sanjin/p/3578518.html
内容总结
以上是互联网集市为您收集整理的cocos2dx -lua 面向对象-转全部内容,希望文章能够帮你解决cocos2dx -lua 面向对象-转所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。