您的位置:首页 > 移动开发 > Cocos引擎

cocos场景管理器-自己用

2016-06-22 13:15 337 查看
SceneManager = SceneManager or BaseClass()

SceneManager.SCENE_TRANSITIONS = {

    CROSSFADE       = {cc.TransitionCrossFade, 2},

    FADE            = {cc.TransitionFade, 3, cc.c3b(0, 0, 0)},

    FADEBL          = {cc.TransitionFadeBL, 2},

    FADEDOWN        = {cc.TransitionFadeDown, 2},

    FADETR          = {cc.TransitionFadeTR, 2},

    FADEUP          = {cc.TransitionFadeUp, 2},

    FLIPANGULAR     = {cc.TransitionFlipAngular, 3, cc.TRANSITION_ORIENTATION_LEFT_OVER},

    FLIPX           = {cc.TransitionFlipX, 3, cc.TRANSITION_ORIENTATION_LEFT_OVER},

    FLIPY           = {cc.TransitionFlipY, 3, cc.TRANSITION_ORIENTATION_UP_OVER},

    JUMPZOOM        = {cc.TransitionJumpZoom, 2},

    MOVEINB         = {cc.TransitionMoveInB, 2},

    MOVEINL         = {cc.TransitionMoveInL, 2},

    MOVEINR         = {cc.TransitionMoveInR, 2},

    MOVEINT         = {cc.TransitionMoveInT, 2},

    PAGETURN        = {cc.TransitionPageTurn, 3, false},

    ROTOZOOM        = {cc.TransitionRotoZoom, 2},

    SHRINKGROW      = {cc.TransitionShrinkGrow, 2},

    SLIDEINB        = {cc.TransitionSlideInB, 2},

    SLIDEINL        = {cc.TransitionSlideInL, 2},

    SLIDEINR        = {cc.TransitionSlideInR, 2},

    SLIDEINT        = {cc.TransitionSlideInT, 2},

    SPLITCOLS       = {cc.TransitionSplitCols, 2},

    SPLITROWS       = {cc.TransitionSplitRows, 2},

    TURNOFFTILES    = {cc.TransitionTurnOffTiles, 2},

  ZOOMFLIPANGULAR = {cc.TransitionZoomFlipAngular, 2},

    ZOOMFLIPX       = {cc.TransitionZoomFlipX, 3, cc.TRANSITION_ORIENTATION_LEFT_OVER},

    ZOOMFLIPY       = {cc.TransitionZoomFlipY, 3, cc.TRANSITION_ORIENTATION_UP_OVER},

}

function SceneManager:__init()
if SceneManager.Instance ~= nil then
return
end
print("................")
SceneManager.Instance = self
self.scene_list = {}
self.scene_list.scene_nums= 0

end

function SceneManager:delMe()
for i = #self.scene_list ,2 ,-1 do 
local v = self.scene_list[i]
if v then 
v:delMe()
self.scene_list[i] = nil
end
end
print("Clear All Scene ")

end

--[[

场景入栈

-------------------------------

    @sceneName 场景名

    @transitonScene 过渡动画

    @... 参数。table

]]

function SceneManager:pushSceneWithTransition(sceneName,transitionType, ... )
local nexscene_class = require(sceneName)

local run_scene = self.scene_list[ #self.scene_list ]
ChestError(nexscene_class)
--空指针检测
local nextscene = nexscene_class.new(...)
nextscene.__cname = sceneName
self.scene_list.scene_nums = self.scene_list.scene_nums + 1
self.scene_list[ #self.scene_list + 1] = nexScene
print("SceneManager:pushScene ->>>>>>>>>>>>self.scene_list size ", self.scene_list.scene_nums)
if self.scene_list.scene_nums >= 2 then 
local scene = self:converTransitionByType(transitionType,...)
cc.Director:getInstance():pushScene(scene)
end

end

function SceneManager:runScene(sceneName,transitionType, ... )
local nexscene_class = require(sceneName)
local run_scene = self.scene_list[ #self.scene_list ]
ChestError(nexscene_class)
--空指针检测
local nextscene = nexscene_class.new(...)
nextscene.__cname = sceneName
self.scene_list.scene_nums = self.scene_list.scene_nums + 1
self.scene_list[#self.scene_list + 1] = nextscene
print("SceneManager:runScene ->>>>>>>>>>>>self.scene_list size ", self.scene_list.scene_nums)
if self.scene_list.scene_nums >= 1 then 
local newScene =  self:getNextRunScene() 
if transitionType then 
newScene = self:converTransitionByType(transitionType,...)
end

ChestError(newScene)
--空指针检测

if display.getRunningScene() then 
cc.Director:getInstance():replaceScene(newScene)
return
else
cc.Director:getInstance():runWithScene(newScene)
end
end

end

-- 从栈顶取出下一个场景

function SceneManager:getNextRunScene()
return self.scene_list[ #self.scene_list ] or nil 

end

-- 动作转换

function SceneManager:converTransitionByType(transitionType, ...)

-- 转换相应的大写,注意只传 过度名,去掉cc例如 MOVEINB
local  key = string.upper(tostring(transitionType))
-- 如果是随机的话
if key == "RANDOM" then
local transiton_nums = table.nums(SceneManager.SCENE_TRANSITIONS)
print("SceneManager.....transiton_nums的值",transiton_nums)
key = math.random(1,#transiton_nums)
end

if nil == key then 
return nil
end
local scene_action_cls , count, default = unpack(SceneManager.SCENE_TRANSITIONS[key])
local nextscene = self:getNextRunScene() 
ChestError(nextscene)
local cfg = ...
local scene

if count == 3 then
scene  = scene_action_cls:create(cfg.time,nextscene ,cfg.other or default)
else
scene  = scene_action_cls:create(cfg.time,nextscene)
end
return scene

end

-- 回到主场景--即开始游戏场景

function SceneManager:goToMain()
-- 首先连续出栈

end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: