local fileUtils = cc.FileUtils:getInstance()
local searchPaths = {}
local resPrefix = "res/"
table.insert(searchPaths, 1, resPrefix .. "图片名")
...
table.insert(searchPaths, 1, resPrefix .. "abc...d/图片名")
fileUtils:setSearchPaths(searchPaths)
cc.Director:getInstance():getTextureCache():addImageAsync("图片名",handler(self, self.AsyncLoaded))
cc.Director:getInstance():getTextureCache():addImageAsync("图片名",function () self:AsyncLoaded() end)
local m_bUserInit = cc.UserDefault:getInstance():getBoolForKey("USER_INIT", false)
if not m_bUserInit then
cc.UserDefault:getInstance():setBoolForKey("USER_INIT", true)
cc.UserDefault:getInstance():setBoolForKey("BackMusic", true)
cc.UserDefault:getInstance():setBoolForKey("GameMusic", true)
end
local delay = cc.DelayTime:create(delay)
local sequence = cc.Sequence:create(delay, cc.CallFunc:create(callback)
local action = cc.RepeatForever:create(sequence)
node:runAction(action)
-- delay = 1 那么就表示:每1S执行一次callback回调函数
-- local action = cc.RepeatForever:create(sequence)出掉node:runAction(sequence) 就表示只执行一次
后来才发现原来版本里是有这个函数的:extern.lua里的schedule(node, callback, delay)可以自己试试(我没试)
local bezier = {cc.p(x1,y1), cc.p(x2,y2), cc.p(x3,y3),}
local pSeqBack = cc.Sequence:create(cc.BezierTo:create(time, bezier))
node:runAction(pSeqBack)
int tolua_cocos2d_BezierTo_create(lua_State* tolua_S)
{
if (NULL == tolua_S)
return 0;
int argc = 0;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
if (!tolua_isusertable(tolua_S,1,"cc.BezierTo",0,&tolua_err)) goto tolua_lerror;
#endif
argc = lua_gettop(tolua_S) - 1;
if (argc == 2)
{
double t = 0.0;
ok &= luaval_to_number(tolua_S, 2, &t);
if (!ok)
return 0;
int num = 0;
cocos2d::Vec2 *arr = NULL;
ok &= luaval_to_array_of_vec2(tolua_S, 3, &arr, &num);
if (!ok)
return 0;
if (num < 3)
{
CC_SAFE_DELETE_ARRAY(arr);
return 0;
}
ccBezierConfig config;
config.controlPoint_1 = arr[0];
config.controlPoint_2 = arr[1];
config.endPosition = arr[2];
CC_SAFE_DELETE_ARRAY(arr);
BezierTo* tolua_ret = BezierTo::create(t, config);
if (NULL != tolua_ret)
{
int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1;
int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL;
toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"cc.BezierTo");
return 1;
}
}
CCLOG("'create' has wrong number of arguments: %d, was expecting %d\n", argc, 2);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'create'.",&tolua_err);
return 0;
#endif
}
static void extendBezierTo(lua_State* tolua_S)
{
lua_pushstring(tolua_S,"cc.BezierTo");
lua_rawget(tolua_S,LUA_REGISTRYINDEX);
if (lua_istable(tolua_S,-1))
{
lua_pushstring(tolua_S,"create");
lua_pushcfunction(tolua_S,tolua_cocos2d_BezierTo_create);
lua_rawset(tolua_S,-3);
}
lua_pop(tolua_S, 1);
}
static int lua_cocos2dx_BezierTo_finalize(lua_State* tolua_S)
{
printf("luabindings: finalizing LUA object (BezierTo)");
return 0;
}
int lua_register_cocos2dx_BezierTo(lua_State* tolua_S)
{
tolua_usertype(tolua_S,"cc.BezierTo");
tolua_cclass(tolua_S,"BezierTo","cc.BezierTo","cc.BezierBy",nullptr);
tolua_beginmodule(tolua_S,"BezierTo");
tolua_endmodule(tolua_S);
std::string typeName = typeid(cocos2d::BezierTo).name();
g_luaType[typeName] = "cc.BezierTo";
g_typeCast["BezierTo"] = "cc.BezierTo";
return 1;
}
local animation =cc.Animation:create()
for i=0,5 do
local szName =string.format("图片%d.png",i)
animation:addSpriteFrameWithFile(szName)
end
local animation =cc.Animation:create()
for i=0,13 do
local szName =string.format("图片%d.png",i)
local spriteFrame = cc.SpriteFrameCache:getInstance():getSpriteFrame(szName)
animation:addSpriteFrame(spriteFrame)
end
animation:setDelayPerUnit(time)
animation:setRestoreOriginalFrame(true)
local action =cc.Animate:create(animation)
node:runAction(cc.RepeatForever:create(action))
local tableView = cc.TableView:create(cc.size(width, height))
tableView:setDirection(cc.SCROLLVIEW_DIRECTION_HORIZONTAL)
tableView:setPosition(0, 0)
tableView:setDelegate()
self:addChild(tableView)
--registerScriptHandler functions must be before the reloadData funtion
tableView:registerScriptHandler(handler(self, self.numberOfCellsInTableView),cc.NUMBER_OF_CELLS_IN_TABLEVIEW)
tableView:registerScriptHandler(handler(self, self.scrollViewDidScroll),cc.SCROLLVIEW_SCRIPT_SCROLL)
tableView:registerScriptHandler(handler(self, self.scrollViewDidZoom),cc.SCROLLVIEW_SCRIPT_ZOOM)
tableView:registerScriptHandler(handler(self, self.tableCellTouched),cc.TABLECELL_TOUCHED)
tableView:registerScriptHandler(handler(self, self.cellSizeForTable),cc.TABLECELL_SIZE_FOR_INDEX)
tableView:registerScriptHandler(handler(self, self.tableCellAtIndex),cc.TABLECELL_SIZE_AT_INDEX)
tableView:reloadData()
function ScrollView:scrollViewDidScroll(view)
print("scrollViewDidScroll")
end
function ScrollView:scrollViewDidZoom(view)
print("scrollViewDidZoom")
end
function ScrollView:tableCellTouched(table,cell)
print("cell touched at index: " .. cell:getIdx())
end
function ScrollView:cellSizeForTable(table,idx)
--error cocos2dx
return self.m_cellSize.width, self.m_cellSize.width
end
function ScrollView:tableCellAtIndex(table, idx)
local cell = table:dequeueCell()
if not cell then
cell = cc.TableViewCell:new()
item = cc.Sprite:create("图片名")
item:setPosition(self.m_cellSize.width/2, self.m_cellSize.height/2)
item:setTag(60)
cell:addChild(item)
else
item = cell:getChildByTag(60)
end
return cell
end
function ScrollView:numberOfCellsInTableView(table)
return ScrollView:GetCount()
end
注意:
table.sort(self.m_Data,
function(a,b)
return a.m_cbData
注意:当self.m_Data表里有2个相同的m_cbData字段值时,如果多次调用sort时,这2个值的位置会互换。
local pData = {}
pData.m_pSprite = cc.Sprite:create(a)
pData.m_cbData = 1
table.insert(self.m_Data, pData)
pData.m_pSprite = cc.Sprite:create(b)
pData.m_cbData = 2
table.insert(self.m_Data, pData)
pData.m_pSprite = cc.Sprite:create(c)
pData.m_cbData = 2
table.insert(self.m_Data, pData)
当调用时:
table.sort(self.m_Data,
function(a,b)
return a.m_cbData
假如是:a b c
local pData = {}
pData.m_pSprite = cc.Sprite:create(a)
pData.m_cbData = 1
pData.m_iIndex = self.m_iIndex
table.insert(self.m_Data, pData)
self.m_iIndex = self.m_iIndex + 1
pData.m_pSprite = cc.Sprite:create(b)
pData.m_cbData = 2
pData.m_iIndex = self.m_iIndex
table.insert(self.m_Data, pData)
self.m_iIndex = self.m_iIndex + 1
pData.m_pSprite = cc.Sprite:create(c)
pData.m_cbData = 2
pData.m_iIndex = self.m_iIndex
table.insert(self.m_Data, pData)
self.m_iIndex = self.m_iIndex + 1
-- 这样排序就不会有问题了,精灵就不会互换了。
table.sort(self.m_Data,
function(a,b)
if a.m_cbData==b.m_cbData then
return a.m_iIndex < b.m_iIndex
else
return a.m_cbData
function hanshu(Data, iCount) -- Data为表 iCount为数值
Data = {}
for i = #Data+1, #Data + 5 do
Data[i] = i
iCount = iCount + 1
end
end
----------------------
local Data = {1,2,3,4,5,6,7,8,9,10}
local iCount = 10
hanshu(Data, iCount)
for i = 1, #Data do
print(Data[i])
end
print(iCount)
function hanshu(Data, iCount) -- Data为表 iCount为数值
for i = #Data+1, #Data + 5 do
Data[i] = i
iCount = iCount + 1
end
return iCount
end
local A = class("A", function() return Layer:create() end)
--init()
function A:init()
print("A:init")
local m_visibleSize = cc.Director:getInstance():getVisibleSize()
self.m_pSprite = cc.Sprite:create(a)
self:addChild(self.m_pSprite, 0)
end
--function SetTexture
function A:SetTexture(strPngName)
print("A:SetTexture")
self.m_pSprite:setTexture(strPngName)
end
--ctor()
function A:ctor()
print("A:ctor")
self:ignoreAnchorPointForPosition(false)
self:init()
end
--create( ... )
function A_createLayer( ... )
print("A_createLayer")
local layer = A.new()
return layer
end
------------------------------------
local m_ALayer = A_createLayer()
self:addChild(m_ALayer, 0)
m_ALayer:SetTexture(b)
A = class("A", nil)
function A:new(o)
print("A:new")
o = o or {}
setmetatable(o,self)
self.__index = self
return o
end
function A:GetInstance()
if self.instance == nil then
self.instance = self:new()
self:Reset()
end
return self.instance
end
function A:ReleaseInstance()
print("A:ReleaseInstance")
if self.instance then
self.instance = nil
end
end
function A:GetA()
return self
end
function A:Reset()
-- clear()
self.dwUserID = 0
self.dwGameID = 0
self.dwGroupID = 0
self.szNickName = ""
self.szGroupName = ""
self.szUnderWrite = ""
self.wFaceID = 0
self.dwCustomID = 0
end
-- 成员方法
...
function A:SetUserID(dwUserID)
self.dwUserID = dwUserID
end
function A:GetUserID()
return self.dwUserID
end
-------------------------------
local m_AInstance = A:GetInstance():GetA()
m_AInstance.dwUserID = 1101
m_AInstance.dwGameID = 1120
m_AInstance.szNickName = "test001"
....
m_AInstance.dwCustomID = 100
A:GetInstance():SetUserID(1102)
print(A:GetInstance():GetUserID())