创建工程略过。。。
1.将要切换场景的lua文件,里面含有2个方法,一个全局的一个局部的,全局的是在切换场景的时候用(为什么定义全局的,后面会有说明),场景只简单的展示了一张图片
- local s = cc.Director:getInstance():getWinSize()
- local GameMainLayer = nil
- GameMainLayer = function ()
- local ret = cc.Layer:create()
- local pBg = cc.Sprite:create("fruit/MM_0002.png")
- -- self.Bg:setAnchorPoint(cc.p(0.5, 0.5))
- pBg:setPosition(cc.p(s.width/2, s.height/2))
- ret:addChild(pBg, 1)
- return ret
- end
- function cc.exports.CreateGameScene()
- print("CreateGameScene...")
- local scene = cc.Scene:create()
- local layer = GameMainLayer()
- scene:addChild(layer)
- return scene
- end
因为要用到GameScene.lua文件,所以这里要引用一下;
重点是在按钮回调的方法changeSceneCallBack() 里面的CreatGameScene这个方法,由于在CreatGameScene在GameScene.lua里是一个全局的,所以这里就能很方便的调用,这也就是为什么在GameScene.lua将它定义为全局的原因
- require "app/views/GameScene"
- local MainScene = class("MainScene", cc.load("mvc").ViewBase)
- local s = cc.Director:getInstance():getWinSize()
- print(s.width, s.height)
- function MainScene:onCreate()
- -- add background image
- -- display.newSprite("HelloWorld.png")
- -- :move(display.center)
- -- :addTo(self)
- -- -- add HelloWorld label
- -- cc.Label:createWithSystemFont("Hello World", "Arial", 40)
- -- :move(display.cx, display.cy + 200)
- -- :addTo(self)
- self.Bg = cc.Sprite:create("fruit/logo.png")
- self.Bg:setPosition(cc.p(s.width/2, s.height/2))
- self:addChild(self.Bg, 1)
- local function changeSceneCallBack()
- print("changeSceneCallBack...")
- local _gameScene = CreateGameScene()
- if _gameScene then
- print("1111111")
- cc.Director:getInstance():replaceScene(_gameScene)
- else
- print("_gameScene nil...")
- end
- end
- local changeItem = cc.MenuItemImage:create("CloseNormal.png", "CloseSelected.png")
- changeItem:setScale(3.0)
- changeItem:registerScriptTapHandler(changeSceneCallBack)
- changeItem:setPosition(cc.p(s.width/2, 200))
- local changeMenu = cc.Menu:create(changeItem)
- changeMenu:setPosition(cc.p(0, 0))
- self:addChild(changeMenu, 1)
- end