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








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GFanStudio-LeeSir

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值