用Quick Cocos2dx做乙個連連看(二)

2022-02-02 00:56:36 字數 2338 閱讀 1396

今天完成了以下內容:

1 成對生成spriteitem

2 重排介面製作完成

3 spriteitem的選擇邏輯

主要**如下:

1

function

mainscene:onenter()

2local

item

3local

item0

4local temptype = 1

5for i=1,32

do6 temptype = math.floor(math.random(0,10

))7 item =spriteitem.new()

8item:setdata(temptype)

9item:addto(self.layer)

10 item:setname(string.format("

item%d

", i))

11 self.items[i] =item

12 item:setpos(math.floor((i-1)%row),math.floor((i-1)/row))

1314 item0 =spriteitem.new()

15item0:setdata(temptype)

16item0:addto(self.layer)

17 self.items[i+32] =item0

18 item0:setname(string.format("

item%d

", i+32

))19 item0:setpos(math.floor((i+31)%row),math.floor((i+31)/row))

20end

2122

self:shuffle(self.items)

2324

self.layer:addnodeeventlistener(cc.node_touch_event, handler(self, self.ontouched))

25 self.layer:settouchenabled(true)26

end

順序重排的方法:

1

function

mainscene:shuffle(t)

2local len = #t

3for i=1,len*2do4

local a = math.floor(math.random

(len))

5local b = math.floor(math.random

(len))67

if a ~= b then

8 t[a],t[b] =t[b],t[a]

9 t[a]:setpos(math.floor((a-1)%row),math.floor((a-1)/row))

10 t[b]:setpos(math.floor((b-1)%row),math.floor((b-1)/row))

11--

printinfo("swap item %d : %d", a,b)

12end

13end

14--[[

i = 1

15for i=1,len do

16print(t[i]:getname())

17end

]]18

end

選擇邏輯:根據螢幕上的座標點取得對應的spriteitem

1

function

mainscene:getitem( posx, posy )2--

printinfo("getitem %d : %d", posx, posy)34

local px = math.round((posx - startx)/50)5

local py = math.round((posy-starty)/50)6

if px > 8

or py > 8

then

7return

8end

9local index = row * py + px + 1

10local item =self.items[index]

1112

return

item

13end

當前效果圖如下:

QUICK COCOS2DX建立新專案

建立新專案之前,確保已經正確設定了quick cocos2dx root環境變數 啟動 終端 應用程式,然後進入需要放置新專案的目錄 cd desktop執行create project.sh命令,並指定專案的 package name quick cocos2dx root bin create ...

quick cocos2d x的Image使用方法

image的使用方法相對簡單。quick cocos2d x中可以指定該image是否使用9宮圖來進行渲染。不使用9宮格的image建立 local img cc.ui.uiimage.new test.png 使用9宮格的image建立 local img cc.ui.uiimage.new te...

Quick Cocos2d x 觸控機制詳解

cocos2d x 原本的觸控機制存在一些限制,在使用中需要開發者做不少額外的處理。而且 cocos2d x 的觸控事件是按照目標的優先順序來分派的,而不是按照目標的顯示層級來分派。針對這個問題,quick cocos2d x 提出了自己的一套觸控機制。本文詳細介紹了這套新機制的實現原理和用法。以下...