Quick cocos2dx学习笔记

http://www.swfdiy.com/?p=1663

http://blog.csdn.net/ecaifu800/article/details/23917943

 quick 在cdx+lua的上层,可以把quick当作是入门教程,cdx+lua当成是高阶教程 

1 import和require

local Component = import("..Component")
local EventProtocol = class("EventProtocol", Component)

两个点..是指上个目录

local UILayout = import(".UILayout")
local UIBoxLayout = class("UIBoxLayout", UILayout)

一个点.是指当前目录

2 框架观察

framework里的init就是调用framework.cc.init,cc.ui = import(".ui.init");这样cc就能引用到相关的类

3 :号和.的区别

:是个语法糖,调用的函数会自动传递参数self
local a = {x = 0}

function a.foo(self, a)
    self.x = a
end

function a:foo2(a)
    self.x = a
end

--调用时:
a.foo(a, 2)
a.foo2(2)


上述两个操作是等价的,用:时就省去了定义和调用时需要额外添加self用来指代自身的麻烦

4 ipairs 和paris的区别

http://blog.csdn.net/witch_soya/article/details/7556595

local components = {
    "components.behavior.StateMachine",
    "components.behavior.EventProtocol",
    "components.ui.BasicLayoutProtocol",
    "components.ui.LayoutProtocol",
    "components.ui.DraggableProtocol",
}

for _, packageName in ipairs(components) do
    print(_,packageName);
end

print("------");
local tabFiles = {
	[1] = "test1",
	[3] = "test3",
	[6] = "test6",
	[4] = "test4"
}
for k, v in ipairs(tabFiles) do
    print(k,v);
end

>lua -e "io.stdout:setvbuf 'no'" "for.lua"
1 components.behavior.StateMachine
2 components.behavior.EventProtocol
3 components.ui.BasicLayoutProtocol
4 components.ui.LayoutProtocol
5 components.ui.DraggableProtocol
------
1 test1

ipairs是从1,2,3...顺序开始遍历 碰到nil就退出 遍历是有序的

pairs不是 pairs遍历是无序的 键值对遍历

5 随机数生成

lua 随机数math.random()

Lua 生成随机数需要用到两个函数:
math.randomseed(xx), math.random([n [, m]])

1. math.randomseed(n) 接收一个整数 n 作为随机序列种子。
2. math.random([n [, m]]) 有三种用法: 无参调用, 产生 (0,1) 之间的浮点随机数; 只有参数 n, 产生 [1,n] 1-n 之间,包括1,n的整数; 有两个参数 n, m, 产生 [n,m]n-m 之间包括n,m的随机整数

对于相同的随机种子, 生成的随即序列一定是相同的。所以程序每次运行, 赋予不同的种子很重要。很自然想到使用系统时间作为随机种子,即:

math.randomseed(os.time())  

----然后不断产生随机数  

for i=1, 5 do  

 print(math.random())  

end  

http://blog.csdn.net/zhangxaochen/article/details/8095007

6 c++ lua交互

http://www.cnblogs.com/hmxp8/archive/2011/11/23/2259777.html

http://blog.csdn.net/liaowenfeng/article/details/10607915

tolua.isnull是判断一个userdata是否存在或者已经被释放的。如果传入的参数不是userdata,当然会返回true。 

tolua 一些可以用的函数(测试过)

7 lua table api

http://www.cnblogs.com/whiteyun/archive/2009/08/10/1543139.html

操作数组元素:

local test = {1,2,3,4,5}
for i=1,#test do
    if test[i] == 3 then
     table.remove(test, i)
    end
end
--[[table.remove()函数删除并返回table数组部分位于pos位置的元素. 其后的元素会被前移. pos参数可选,
默认为table长度, 即从最后一个元素删起.]]
table.remove(test)
for k, v in ipairs(test) do
    print(k,v);
end
--[[
  

1 1
2 2
3 4



]]

 8 内存管理

老师,在横版游戏里面,很多在线玩家都在同一个场景,切换进入下一个地图的时候,使用的是另一个Scene,请问下场景里面的玩家对象是不是不用清除,切换另一个Scene的时候,自动销毁上一个Scene的显示对象的吗?

这个问题涉及到两方面的问题,一个是CCDirector里的场景切换,你可以看下CCDirector.h里面有关scene的几个方法:runWithScene()、pushScene()、popScene()、replaceScene()几个方法;第二个问题是内存管理机制的问题,现在的内存管理是通过parent和child来进行,如果parent node被释放的话,child node也是会被释放的

 8 物理引擎

使用Physicals代替Box2D和chipmunk

quick-cocos2d-x物理引擎之chipmunk(一)http://my.oschina.net/lonewolf/blog/173427

posted on 2014-11-02 16:20  防空洞123  阅读(569)  评论(0编辑  收藏  举报

导航