魔獸軍團前端項目的一些總結

1.當打通本關開啓下一關卡時增加一個解鎖特效,特效播完之後再播放水晶指示特效,其他時候接入地圖不播放關卡解鎖特效。

if adjustFlag == true or (newChapter == nowChapter and nil ~= lastBtn) then
local x,y = lastBtn:getPosition()
local parent = lastBtn:getParent() –先把父節點保存起來再加特效

    if isBattleEnd then --戰鬥結束播放下一關卡解鎖特效
        local function addEndEffect()
            local jsonPath = "res/effect/300016/skeleton.json"
            local atlasPath = "res/effect/300016/skeleton.atlas"
    --SkeletonAnimation.new 是前端自己封裝的一個接口
            local effect = SkeletonAnimation.new(
                                        "new_section_effect",
                                        jsonPath,
                                        atlasPath,
                                        "default",
                                        "animation",
                                        true
                                    )

            effect:setPosition(x+0, y-80 + tmpFix)
            effect:setScale(1)
            parent:addChild(effect._ccnode,100)
            SECTION_EFFECT = effect._ccnode
        end

        local scale = specialEffectScale
        local effectId
        local effectY = 0 
        if scale == 1.2 then
            effectId = 301005
        elseif scale == 1.3 then
            effectId = 301006
        elseif scale == 1.5 then
            effectId = 301007
            effectY = 5
        end
        local resId = SpecialEffectConfig[effectId].resId

        local jsonPath = "res/effect/"..resId.."/skeleton.json"
        local atlasPath = "res/effect/"..resId.."/skeleton.atlas"

        local effect = SkeletonAnimation.new(
                                    "jie_suo_effect",
                                    jsonPath,
                                    atlasPath,
                                    "default",
                                    "animation",
                                    false
                                )

        effect:setPosition(x, y - effectY)
        effect:setScale(SpecialEffectConfig[effectId].scaleNum)
        effect:setTimeScale(SpecialEffectConfig[effectId].scaleTime)

        parent:addChild(effect._ccnode,10)

        local function actionEnd(event)
            if "complete" == event.type then
                effect:unregisterSpineEventHandler(sp.EventType.ANIMATION_COMPLETE) --播完要把這個事件的監聽取消掉
                addEndEffect()
            end
        end
        effect:registerSpineEventHandler(actionEnd, sp.EventType.ANIMATION_COMPLETE)
    else
        local jsonPath = "res/effect/300016/skeleton.json"
        local atlasPath = "res/effect/300016/skeleton.atlas"

        local effect = SkeletonAnimation.new(
                                    "new_section_effect",
                                    jsonPath,
                                    atlasPath,
                                    "default",
                                    "animation",
                                    true
                                )

        effect:setPosition(x+0, y-80 + tmpFix)
        effect:setScale(1)

        parent:addChild(effect._ccnode,100)--因爲x,y是相對於地圖的座標,而特效的座標也是相對於地圖而言的,所以是將特效添加到地圖上而不是添加到關卡的按鈕上
        SECTION_EFFECT = effect._ccnode
    end
end

2.給主界面的閃亮大禮包圖標添加閃亮的特效
function updateActivityButton(self)
local layer = self._ccnode:getChildByName(“activityPanel”)
local buttonFlex = layer:getChildByName(“shensuo”)

local playerLevel = PlayerInfo.getLv()
local playerVip = PlayerInfo.getVip()
self.activity.activities = {}
for i, config in ipairs(MainpageConfig.ActivityConfig) do
    if config.posIndex == ActivityLogic.posIndex.homepage then
        local isShowFuncName = isShowActivityFuncNames[config.activity]
        local isShow = true
        repeat
            if playerLevel < config.levelLimit or playerVip < config.vipLimit then
                isShow = false
                break
            end
            if isShowFuncName and self[isShowFuncName] then
                isShow = isShow and self[isShowFuncName](self)
            end
        until true
        if isShow then
            table.insert(self.activity.activities, config.activity)
            table.insert(self.activity.special,config.activity)
        end
    end
end

local buttonNum = #self.activity.buttons
local activityNum = #self.activity.activities
if buttonNum > activityNum then
    for i = 1, buttonNum -activityNum do
        local button = table.remove(self.activity.buttons)
        button:removeFromParent()
    end
elseif buttonNum < activityNum then
    for i = 1, activityNum - buttonNum do
        local button = self.activity.moduleActivity:clone()
        layer:addChild(button)
        table.insert(self.activity.buttons, button)
    end
end

local positonFlexX, positonFlexY = buttonFlex:getPosition()
local effecName = "effect"
for i, button in ipairs(self.activity.buttons) do
    local activity = self.activity.activities[i]
    local activityConfig = HomepageActivityConfig[activity]
    --因爲不知道之前需要去掉特效的圖標位置變化成了多少,所以每個圖標都要檢查一下,把所有圖標的特效都去掉再根據條件加特效。
    local effect = button:getChildByName(effecName)
    if effect then
        effect:removeFromParent(true)
    end

    if self.activity.isFlex then
        local imageMark = button:getChildByName("hongdian")

        local row = math.ceil((i + 1) / self.activity.maxCol)
        local col = i + 1 - (row - 1) * self.activity.maxCol
        button:setPosition(positonFlexX - self.activity.intervalWidth * (col - 1), positonFlexY - self.activity.intervalHeight * (row - 1))
        button:setVisible(true)
        button:loadTextureNormal(string.format(MainpageConfig.pathActivityButton, activityConfig.resource))
        local function onButton(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                local funcName = activityButtonFuncNames[activity]
                if funcName and self[funcName] then
                    self[funcName](self)
                end
            end
        end
        button:addTouchEventListener(onButton)

        local markFunc = markActivityFuncNames[activity]
        if markFunc and self[markFunc](self) then
            imageMark:setVisible(true)
        else
            imageMark:setVisible(false)
        end
        button:setVisible(true)

        --添加圖標的特效
        local specialFunc = isShowActivitiesSpecialFuncNames[activity]
        local effectId = activityConfig.effectId
        if specialFunc and self[specialFunc](self) and effectId ~= "" then
            local scale = button:getScale()

            local jsonPath = "res/effect/"..effectId.."/skeleton.json"
            local atlasPath = "res/effect/"..effectId.."/skeleton.atlas"

            local effect = SkeletonAnimation.new(
                                        effecName,
                                        jsonPath,
                                        atlasPath,
                                        "default",
                                        "animation",
                                        true
                                    )

            effect:setPosition(60, 0)--因爲effect是button的子節點,所以這裏設置是相對父節點的位置。如果這個特效的位置取的button的x,y設進去,x,y是個比較大的值的話,會拋出屏幕的外面看不到特效!
            effect:setScale(scale)
            effect._ccnode:setName(effecName)
            --前面有說過SkeletonAnimation.new是自己封裝的接口,雖然effect._name==effecName,但還是要用原生態的接口setName()設置下節點的名字,要不然前面local effect = button:getChildByName(effecName) 移除特效的時候會找不到剛纔設置的特效。
            button:addChild(effect._ccnode,1)
        end
    else
        button:setVisible(false)
    end
end

end

3.上下滑動副本地圖可以跳轉章節,即在地圖處於最底部或最頂部時,向上滑動地圖或向下滑動地圖可進入上一章節或下一章節
local function scrollViewEvent(sender, evenType)
if evenType == ccui.ScrollviewEventType.scrollToBottom then
self._toBottomCount = self._toBottomCount + 1
if self._toBottomCount >= 6 then
local chapterId = self._charpterId - 1
if chapterId >= 1 then
local chapters = CampaignInfo.getChapters()
local sections = chapters[chapterId].sections
if next(sections) ~= nil then
local function doOpenUi()
Campaign.showChapter(chapterId, sections)
end

                    performWithDelay(Stage.currentScene._ccnode, doOpenUi, 0.02)
                end
            end
        end
    elseif evenType ==  ccui.ScrollviewEventType.scrollToTop then
        self._toTopCount = self._toTopCount + 1 
        if self._toTopCount >= 8 then --因爲已經通關的章節一進去就自動滾到最頂端,所以這裏要加個計數,要不然會一直跳到沒有通關的章節
            local chapters = CampaignInfo.getChapters()
            local chapterId = self._charpterId + 1
            if chapterId < #chapters then
                local condition = chapters[chapterId].condition
                local isReach = CampaignInfo.checkCondition(condition)
                if condition and CampaignInfo.checkCondition(condition) then
                    local sections = chapters[chapterId].sections
                        if next(sections) ~= nil then
                             local function doOpenUi()
                            Campaign.showChapter(chapterId, sections)
                        end

                        performWithDelay(Stage.currentScene._ccnode, doOpenUi, 0.02)
                    end
                end
            end
        end
    end               
end

4.如何傳遞self
function initUI(self)
local downBaseBg = self._ccnode:getChildByName(“downBaseBg”)
local mainBtn = downBaseBg:getChildByName(“mainBtn”)
local roleBtn = downBaseBg:getChildByName(“roleBtn”)
local heroBtn = downBaseBg:getChildByName(“heroBtn”)
local bagBtn = downBaseBg:getChildByName(“bagBtn”)
local friendBtn = downBaseBg:getChildByName(“friendBtn”)
local shopBtn = downBaseBg:getChildByName(“shopBtn”)

heroBtn._self = self
roleBtn._self = self  --把self給存起來

mainBtn:addTouchEventListener(onMainBtn)
roleBtn:addTouchEventListene
end

function onRoleBtn(sender, eventType)
if ccui.TouchEventType.ended == eventType then
PartnerLogic.setIsRoleMark(false)
updateMarkRole(sender._self) –使用剛纔的self
CharacterPort.goToCharacterUI()
end
end
function updateMarkRole(self)
……
end

發佈了31 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章