cocos2dx 3d開源項目 fantasyWarrior3D 從零走起 1 [LoadingScene & MainMenuScene]



[AppDelegate.cpp] 


還是從applicationDidFinishLaunching()開始好了
發現這裏多了一個對腳本進行加密的方法
(1) stack->setXXTEAKeyAndSign("2dxLua", strlen("2dxLua"), "XXTEA", strlen("XXTEA"));  
(2)通過config.json配置來找到lua的入口main.lua並執行,再跳轉到第一個界面LoadingScene

engine->executeScriptFile(ConfigParser::getInstance()->getEntryFile().c_str());


[LoadingScene]


1. LoadingScene的構造順序:

(1) LoadingScene:create() 被外部調用
(2)在create中首先執行LoadingScene:new() ,如下定義纔會生效
把自己初始化爲一個 scene:
local MainMenuScene = class("MainMenuScene",function()
    return cc.Scene:create()
end
)

(3)ctor()
由於使用了 cocos2dx-lua function.lua 定義的class方法,所以會默認調用子類的構造函數 ctor()
(4)在create最後調用 LoadingScene:init()  完成其他子對象的初始化

2. init() 中啓用了一個調度器來執行update以更新子對象的狀態

cc.Director:getInstance():getScheduler():scheduleScriptFunc(update,0.1,false)
update 做了哪些事:
(1)每0.1秒 緩存一個粒子和素材
LoadingScene:cachedParticleRes()
LoadingScene:cachedTextureRes()


(2)調整進度條的位置 loadingbar:setPercent((self._totalResource-self._num)/self._totalResource*100)


(3) 如果發現已經加載完畢了就跳轉
local scene = require("MainMenuScene")
cc.Director:getInstance():replaceScene(scene:create())

[MainMenuScene]


//靜態函數,用於設置默認帶ALPHA通道的貼圖像素格式。則圖片創建爲貼圖時,如果有ALPHA通道,則生成此默認貼圖像素格式。  
static void setDefaultAlphaPixelFormat(CCTexture2DPixelFormat format);  

1. addCloud 添加雲朵

(1) 開啓一個移動雲朵的調度器,頻率是每秒60次
self.scheduleCloudMove = cc.Director:getInstance():getScheduler():scheduleScriptFunc(cloud_move,1/60,false)

2. addlogo  來回搖擺的實現

    local function logoShake()
        --rand_n = range * math.sin(math.rad(time*speed+offset))
        local rand_x = 0.1*math.sin(math.rad(time*0.5+4356))
        local rand_y = 0.1*math.sin(math.rad(time*0.37+5436)) 
        local rand_z = 0.1*math.sin(math.rad(time*0.2+54325))
        logo:setRotation3D({x=math.deg(rand_x),y=math.deg(rand_y),z=math.deg(rand_z)})
        time = time+1
    end
    self.logoSchedule = cc.Director:getInstance():getScheduler():scheduleScriptFunc(logoShake,0,false)

其實這段代碼可以看作是
        local rand_x = 0.1*math.sin(math.rad(time))
        local rand_y = 0.1*math.sin(math.rad(time)) 
        local rand_z = 0.1*math.sin(math.rad(time))
通過math.rad 把time限制爲360以內得一個弧度,再通過 math.sin 轉換爲一個區間數值[-x,x],
最後 *0.1 將擺動幅度降低,這樣就實現了一個 x,y,z軸上的來回搖擺

這個裏在cocosIDE裏面設置數值發現邊框特效突然沒了,“即視”功能還是有點bug呀

3. addPointLight  長翅膀的那個傢伙

(1) getLightSprite()來構造身體用到了alpha混色,以達到羽翼等部分的半透明效果
self._lightSprite:setBlendFunc(gl.ONE,gl.ONE_MINUS_SRC_ALPHA)
GL_ONE:1.0
GL_ONE_MINUS_SRC_ALPHA:1.0減去源的Alpha值作爲因子

參考 http://cn.cocos2d-x.org/tutorial/show?id=1739

(2)法線貼圖 以實現凹凸效果

local effectNormalMapped = cc.EffectNormalMapped:create("mainmenuscene/logo_normal.png");
effectNormalMapped:setPointLight(self._pointLight)
effectNormalMapped:setKBump(50)   這個是設置凹凸值?

(3) “圓周運動” 與 拖拽
onTouchBegin 記錄當前位置,並且打開移動處理函數 movePoint
onTouchEnded 關掉移動處理函數,並且調用 getBezierAction() 重新開始“圓周運動”

movePoint :
值得注意的是 使用lerp 函數來獲取插值,達到一個減速和加速的移動效果。
movePoint函數: local point = cc.pLerp(lightSpritePos,self._prePosition,dt*2)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章