cocos2dx戰爭迷霧實現 lua

2021-07-06 05:04:34 字數 3213 閱讀 2751

tilemap的美術資源(戰爭迷霧)

戰爭迷霧的原理在網上已經有相關資料,就不補充了。

--戰爭迷霧用

--t1中索引與tilemap的gid對應,右邊值(表)與tilemap的資源對應。

p.t1 = ,

[2] = ,

[3] = ,

[5] = ,

[6] = ,

[8] = ,

[13] = ,

[15] = ,

[7] = ,

[12] = ,

[4] = ,

[14] = ,

[9] = ,

[11] = ,

[10] = ,

[1] = ,

}-- t2索引與t1的右邊的表的數字的和對應,值與t1的索引對應

p.t2 =

--先利用t1獲得對應座標的資源資訊(t1中的值),再利用t2中的索引返回新的gid(t2中的值)。

--戰爭迷霧

--已探索地圖

--61即tilemap中戰爭迷霧資源的第乙個圖素的gid

function p.newmap(x,y,num) 

p.cloudlayer:settilegid(61+num,ccp(x,y))

endfunction p.oldmap(x,y)

if x > -1 and y > -1 and x < p.size.width and y < p.size.height then

local id = p.cloudlayer:tilegidat(ccp(x,y))

return id - 61

else

return false

endend

function p.tabletotal(t)    

if t ~= nil and type(t) == "table" then

local total = 0

for k,v in pairs(t) do

total = total + v

endreturn total

endend

--對周圍戰爭迷霧的處理,按照x座標及y座標的差值的絕對值的和(即按照距離)進行分類處理

function p.setcloudinfo(x,y,dx,dy)

local newx,newy,num,total

local t = p.t1

local tab =

local idx,idx1,idx2

local oldnum,oldnum1,oldnum2

newx,newy = x+dx,y+dy

num = p.oldmap(newx,newy)

if(num) then    

local distance = math.abs(dx) + math.abs(dy)

if distance == 3 then

if dx > 0 and dy > 0 then

idx = 1

elseif dx > 0 and dy < 0 then

idx = 3

elseif dx < 0 and dy > 0 then

idx = 2

elseif dx < 0 and dy < 0 then

idx = 4

endelseif distance == 2 then

if dx == 0 or dy == 0 then

if dx == 0 then

if dy > 0 then

idx1,idx2 = 1,2

else

idx1,idx2 = 3,4

endelse

if dx > 0 then

idx1,idx2 = 1,3

else

idx1,idx2 = 2,4

endend

else

if dx > 0 and dy > 0 then

idx = 4

elseif dx > 0 and dy < 0 then

idx = 2

elseif dx < 0 and dy > 0 then

idx = 3

elseif dx < 0 and dy < 0 then

idx = 1

end 

endelseif distance <= 1 then

total = 1

p.newmap(newx,newy,total)

return

endif(idx) then

local nnum = 0

local value = tab[idx]

if distance == 2 then

nnum = t[num][idx] 

num = 1 

value = 0              

end  

oldnum = t[num][idx]

t[num][idx] = value

total = p.tabletotal(t[num])

total = total + nnum         

total = p.t2[total]            

p.newmap(newx,newy,total)

t[num][idx] = oldnum

else

oldnum1 = t[num][idx1]

oldnum2 = t[num][idx2]

t[num][idx1] = tab[idx1]

t[num][idx2] = tab[idx2]

total = p.tabletotal(t[num])

total = p.t2[total]

p.newmap(newx,newy,total)

t[num][idx1],t[num][idx2] = oldnum1,oldnum2

endend

endfunction p.setcloud(x,y)

for i = -2,2 do

for j = -2,2 do

local total = math.abs(i) + math.abs(j)

if total ~= 4 then

p.setcloudinfo(x,y,j,i)

endend

endend

--end

cocos2d x實現打字特效

原文請猛戳 這次分享乙個在cocos2d x中實現打字特效的小功能。首先,cocos2d x中label預設是utf8編碼,quickx提供了乙個string.utf8len介面,這裡再加乙個擷取子字串的函式 function utf8str str,start,num local function...

cocos2d x 實現中文輸出

下面我們來說說如何實現中文輸出吧!首先,個人覺得可以新建乙個tools.cpp 和 tools.h 檔案 首先在tools.h ifndef tools h 預定義塊 define tools h include cocos2d.h if cc target platform cc platform...

cocos2d x實現中文顯示 筆記

cocos2d x在win32開發中,不能直接顯示中文,需要轉字元。cocos2d x已經自帶了乙個對應的庫iconv。如果要使用它,我們要在做以下配置 1.右鍵專案 屬性 附加包含目錄 編輯。然後新增乙個路徑,我的如下 d cocos2d x cocos2d 2.0 x 2.0.4 cocos2d...