Cocos2d-x-lua游戏两个场景互相切换MainScene01切换到MainScene02

/*

   场景一lua代码

*/


require "MainScene02"


local  dic_size = CCDirector:sharedDirector():getWinSize()
local  win_w = dic_size.width
local  win_h = dic_size.height

local   cclog =  function (...)
     print ( string.format (...))
end

function  MainScene_01()
    
    
local  self = CCLayer:create()
    
    
-- click btn
     local   function  clickMenu_scene()
        
-- 切换场景 < 特效 >
         local  fadeClipX = CCTransitionFlipX:create( 1 ,_main02())
        CCDirector:sharedDirector():replaceScene(fadeClipX)
        
    
end
    
-- 初始化相关场景元素
     local   function  init()
        
        cclog(
"....this is init thing......" )
        
-- put a bg pic
         local  pSrite = CCSprite:create( "bg_store.png" )
        pSrite:setPosition(ccp(win_w/
2 , win_h/ 2 ))
        pSrite:setScaleX(win_w/pSrite:getContentSize().width)
        self:addChild(pSrite)
        
        
        
-- create a Button Menu
         local  btn  = CCMenuItemImage:create( "btn_store_normal.png" "btn_store_selected.png" )
        btn:setPosition(ccp(win_w/
2 , win_h/ 2 ))
        btn:registerScriptTapHandler(clickMenu_scene)
-- 添加按钮点击触发事件
         local  btn_menu = CCMenu:createWithItem(btn)
        btn_menu:setPosition(ccp(
0 , 0 ))
        self:addChild(btn_menu)
        
    
end
    
    init()
--get init method
    
    
return  self
end

function  _main01()
    
local  scene = CCScene:create()
    scene:addChild(MainScene_01())
    
local  dic = CCDirector:sharedDirector()
    dic:setDisplayStats(false)
    
-- CCEGLView:sharedOpenGLView():setDesignResolutionSize(960,480, kResolutionShowAll)
     -- CCEGLView:sharedOpenGLView():setDesignResolutionSize(480, 800, kResolutionShowAll)
    dic:runWithScene(scene)
end

_main01()--调用方法


/*

   场景二lua代码

*/


local dic_size = CCDirector:sharedDirector():getWinSize()
local win_w = dic_size.width
local win_h = dic_size.height

--初始化第二个场景的层元素
local function MainScene_02()

local layer = CCLayer:create()
local cclog = function (...)
    
print(string.format(...))
end
--点击按钮触发的方法
local function clickMainScene02_pop()
    cclog(
"this is clicked ".."btn 02")
    
local fins = layer:getChildByTag(100)
    fins:setVisible(true)
    
end

cclog(
"这是在切换新的场景。。。。。。。")
--创建层
--local layer = CCLayer:create()
--
初始化场景的各个元素
local function inits()
    
--初始化第二个场景的背景
    local bgSprite = CCSprite:create("bg_mainmenu.png")
    bgSprite:setPosition(ccp(win_w/
2, win_h/2))
    bgSprite:setScaleX(win_w/bgSprite:getContentSize().width)
    layer:addChild(bgSprite)
    
--添加按钮
    local popMenu = CCMenuItemImage:create("menu1.png","menu1.png")
    popMenu:registerScriptTapHandler(clickMainScene02_pop)
    popMenu:setScale(
2)
    popMenu:setPosition(ccp(win_w/
2, win_h/2))
    
local okMenu = CCMenu:createWithItem(popMenu)
    okMenu:setPosition(ccp(
0,0))
    layer:addChild(okMenu)
    
local showSp = CCSprite:create("menu2.png")
    showSp:setTag(
100)
    showSp:setAnchorPoint(ccp(
0,0))
    showSp:setPosition(ccp(win_w/
2, win_h/2))
    showSp:setVisible(false)
    layer:addChild(showSp)
    
end

inits()
--[[
 
处理触碰事件
 onTouchBegan
 onTouchMoved
 onTouchEnded
 ]]

local function onTouchBegan()
    
    cclog(
"xiaoshiba -------------")
    layer:getChildByTag(
100):setVisible(false)

    
return true
end

local function onTouchMoved()
    cclog(
"onTouchMoved")
end
local function onTouchEnded()
    cclog(
"onTouchEnded")
    
--任意触碰时 将菜单隐藏
    layer:getChildByTag(100):setVisible(false)
end

local function onTouch(eventTye,x,y)
    
if eventType == "began" then
        
return onTouchBegan(x,y)
        
elseif eventType == "moved" then
        
return onTouchMoved(x,y)
        
else
        
return onTouchEnded(x,y)
    
end
    
end
--注册触碰事件
layer:setTouchEnabled(true)
layer:registerScriptTouchHandler(onTouch)



return  layer
end

function _main02()
local sc02 = CCScene:create()
sc02:addChild(MainScene_02())
return sc02
end








你可能感兴趣的:(Cocos2d-x)