Cocos2d-x-lua game two scenarios switch between MainScene01 switch to MainScene02

Source: Internet
Author: User


Scenario 1 lua code


Require "MainScene02"

Local dic_size = CCDirector: shareddire(): getWinSize ()
Local win_w = dic_size.width
Local win_h = dic_size.height

Local cclog = function (...)
Print (string. format (...))

Function MainScene_01 ()

Local self = CCLayer: create ()

-- Click btn
Local function clickMenu_scene ()
-- Switch scenario <special effect>
Local fadatex = CCTransitionFlipX: create (1, _ main02 ())
CCDirector: shareddire(): replaceScene (fad%x)

-- Initialize related scenario Elements
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) -- Add button and click trigger event
Local btn_menu = CCMenu: createWithItem (btn)
Btn_menu: setPosition (ccp (0, 0 ))
Self: addChild (btn_menu)


Init () -- get init method

Return self

Function _ main01 ()
Local scene = CCScene: create ()
Scene: addChild (MainScene_01 ())
Local dic = CCDirector: shareddire ()
Dic: setDisplayStats (false)
-- CCEGLView: Export dopenglview (): setDesignResolutionSize (960,480, kResolutionShowAll)
-- CCEGLView: Export dopenglview (): setDesignResolutionSize (480,800, kResolutionShowAll)
Dic: runWithScene (scene)

_ Main01 () -- call Method


Scenario 2 lua code


Local dic_size = CCDirector: shareddire(): getWinSize ()
Local win_w = dic_size.width
Local win_h = dic_size.height

-- Initialize the layer elements of the second scenario
Local function MainScene_02 ()

Local layer = CCLayer: create ()
Local cclog = function (...)
Print (string. format (...))
-- Trigger by clicking the button
Local function clickMainScene02_pop ()
Cclog ("this is clicked" .. "btn 02 ")
Local fins = layer: getChildByTag (100)
Fins: setVisible (true)


Cclog ("this is a new scenario to switch ....... ")
-- Create Layer
-- Local layer = CCLayer: create ()
-- Initialize each element of the scenario
Local function inits ()
-- Initialize the background of the second scenario
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)
-- Add button
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)


Inits ()
Handle touch events
Local function onTouchBegan ()

Cclog ("xiaoshba -------------")
Layer: getChildByTag (100): setVisible (false)

Return true

Local function onTouchMoved ()
Cclog ("onTouchMoved ")
Local function onTouchEnded ()
Cclog ("onTouchEnded ")
-- Hide the menu when any touch occurs.
Layer: getChildByTag (100): setVisible (false)

Local function onTouch (eventTye, x, y)
If eventType = "began" then
Return onTouchBegan (x, y)
Elseif eventType = "moved" then
Return onTouchMoved (x, y)
Return onTouchEnded (x, y)

-- Register a touch event
Layer: setTouchEnabled (true)
Layer: registerScriptTouchHandler (onTouch)

Return layer

Function _ main02 ()
Local sc02 = CCScene: create ()
Sc02: addChild (MainScene_02 ())
Return sc02

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.