由于公司使用的quick-cocos2d-x引擎的版本是V2.1.4,比較老而且沒(méi)有任何說(shuō)明文檔,所以很多函數(shù)都要看引擎源碼才知道。有些函數(shù)要試一試才能找到正確的寫法,真是挺蛋疼的。不過(guò)認(rèn)真去學(xué)還是會(huì)有收獲滴。
今天“捉蝦”許久,學(xué)的東西不多,不說(shuō)廢話,直接把今天所學(xué)呈上來(lái)。
1、此版本下創(chuàng)建精靈的兩種方式
方式一:傳統(tǒng)方式
sprite1 = CCSprite:create("image/sanli.jpeg")
sprite1:setPosition(display.cx + 400, display.cy - 200)
self:addChild(sprite1, 5)
方式二:display方式
local background = display.newSprite("image/background.png", display.cx, display.cy) self:addChild(background)
顯然display方式比較簡(jiǎn)潔
2、創(chuàng)建label的兩種寫法,跟創(chuàng)建精靈類似:
方式一:
local label1 = CCLabelTTF:create("Hello", "Arial", 32)
label1:setPosition(display.cx + 400, display.cy - 80)
self:addChild(label1, 6)
方式二:
local label = ui.newTTFLabel({
text = "Hello, World",
size = 32,
x = display.cx,
y = display.cy,
align = ui.TEXT_ALIGN_CENTER --整齊豎排
})
self:addChild(label, 7)
3、游戲調(diào)度的三種方式:
--游戲調(diào)度
local function update(dt)
local x, y = label1:getPosition()
label1:setPosition(x - 2, y + 1)
end
--游戲開(kāi)始調(diào)度
--這是一種調(diào)度方法-------------------------------------------------------------
sch = CCDirector:sharedDirector():getScheduler()
sch:scheduleScriptFunc(update, 1/60.0, false) --update是要執(zhí)行的函數(shù),中間是執(zhí)行間隔,最后為是否暫停
--這是第二種調(diào)度方法------------------------------------------------------------
self:scheduleUpdateWithPriorityLua(update, 0)--第二個(gè)參數(shù)是一個(gè)優(yōu)先級(jí),值越小越先執(zhí)行
--這種調(diào)度方式對(duì)應(yīng)的停止調(diào)度為:self:unscheduleUpdate()
--這是第三種調(diào)度方式-------------------------------------------------------------
local? scheduler = require("framework.scheduler")
scheduler.scheduleGlobal(update, 1.0 / 60) --第二個(gè)參數(shù)是執(zhí)行間隔
--第一種和第三種本質(zhì)上是一樣的,用于游戲中計(jì)時(shí)很好用。但是我不知道如果停止調(diào)度 =。=
4、等待一段設(shè)定時(shí)間之后調(diào)用某一函數(shù):
self:performWithDelay(function()
?????? print("-----------等待3秒之后打印了這句話---------------")
end, 3.0)
5、創(chuàng)建文本菜單
local startItem = ui.newTTFLabelMenuItem({
text = "Start",
size = 30,
aligh = ui.TEXT_ALIGN_CENTER,
listener = function ()
print("The start item is touched")
self.player:attack()
end,
x = display.cx,
y = display.cy + 50,
})
local endItem = ui.newTTFLabelMenuItem({
text = "End",
size = 30,
aligh = ui.TEXT_ALIGN_CENTER,
listener = function ()
print("The end item is touched")
self.player:dead()
end,
x = display.cx,
y = display.cy - 50,
})
local menu = ui.newMenu({startItem, endItem})
self:addChild(menu)
6、創(chuàng)建觸摸層:
function MainScene:addTouchLayer()
local function onTouch(eventName, x, y)
if eventName == "began" then
self.player:walkTo({x=x,y=y})
self:unscheduleUpdate()? ? ? --------------------------停止調(diào)度
end
end
--創(chuàng)建觸摸層
self.layerTouch = display.newLayer()
--添加事件監(jiān)聽(tīng)器
self.layerTouch:addTouchEventListener(function(event, x, y)
return onTouch(event, x, y)
end, false)
self.layerTouch:setTouchEnabled(true)
self.layerTouch:setPosition(ccp(0,0))
self.layerTouch:setContentSize(CCSizeMake(display.width, display.height))
self:addChild(self.layerTouch)
end
6、創(chuàng)建和播放動(dòng)畫
--創(chuàng)建并播放角色攻擊動(dòng)畫
function Player:attack()
--創(chuàng)建動(dòng)畫
local frames = display.newFrames("player1-2" .. "-%d.png", 1, 4)
local animation = display.newAnimation(frames, 0.2)
--local frame = display.newSpriteFrame("player1-1-1.png")
transition.playAnimationOnce(self, animation)
end
今天學(xué)的大致就是以上這些,寫得比較亂,沒(méi)有邏輯。有空再系統(tǒng)寫。從標(biāo)簽、菜單、精靈、層、場(chǎng)景、動(dòng)畫和特效到用戶事件、音樂(lè)音效、粒子系統(tǒng)、瓦片地圖和物理引擎,再到數(shù)據(jù)持久化、網(wǎng)絡(luò)通信和性能優(yōu)化等知識(shí),將會(huì)大致地系統(tǒng)地梳理一遍。今天就先到這吧。