在HomeScene里面添加菜单
local size = cc.Director:getInstance():getWinSize()
local defaults = cc.UserDefault:getInstance()
local HomeScene = class("HomeScene",function()
return cc.Scene:create()
end)
function HomeScene:create()
local scene = HomeScene.new()
scene:addChild(scene:createLayer())
return scene
end
function HomeScene:ctor()
-- 场景生命周期事件处理
local function onNodeEvent(event)
if event == "enter" then
self:onEnter()
elseif event == "onEnterTransitionFinish" then
self:onEnterTransitionFinish()
elseif event == "exit" then
self:exit()
elseif event == "onExitTransitionStart" then
self:onExitTransitionStart()
elseif event == "cleanup" then
self:cleanup()
end
end
self:registerScpritHandler(onNodeEvent)
end
-- 创建层
function HomeScene:createLayer()
cclog("HomeScene init")
local layer = cc.Layer:create()
--场景瓦片地图背景
local bg = cc.TMXTiledMap:create("map/red_bg.tmx")
layer:addChild(bg)
local top = cc.Sprite:createWithSpriteFrameName("home-top.png")
layer:addChild(top)
top:setPosition(cc.p(size.width/2,size.height-top:getContentSize().height/2))
local buttom = cc.Sprite:createWithSpriteFrameName("home-end.png")
buttom:setPosition(cc.p(size.width/2,buttom:getContentSize().height/2))
layer:addChild(buttom)
local function menuItemCallBack(tag, sender)
--播放音效
if defaults:getBoolForKey(SOUND_KEY) then
AudioEngine:playEffect(sound_1)
end
--[[
HomeMenuActionType = {
MenuItemStart = 100,
MenuItemSetting = 101,
MenuItemHelp = 102,
}
]]
if tag == HomeMenuActionTypess.MenuItemStart then
local GamePlayScene = require("GamePlayScene")
local scene = GamePlayScene.create()
local ts = cc.TransitionCrossFade:create(1,scene)
cc.Director:getInstance():pushScene(ts)
elseif tag == HomeMenuActionType.MenuItemSetting then
local GamePlayScene = require("SettingScene")
local scene = GamePlayScene.create()
local ts = cc.TransitionCrossFade:create(1,scene)
cc.Director:getInstance():pushScene(ts)
else
local GamePlayScene = require("HelpScene")
local scene = GamePlayScene.create()
local ts = cc.TransitionCrossFade:create(1,scene)
cc.Director:getInstance():pushScene(ts)
end
end
--开始菜单
local startSpriteNormal = cc.Sprite:createWithSpriteFrameName("button.start.png")
local startSpriteSelected = cc.Sprite:createWithSpriteFrameName("button.start-on.png")
local startMenuItem = cc.MenuItemSprite:create(startSpriteNormal,startSpriteSelected)
startMenuItem:registerScriptTapHandler(menuItemCallBack)
startMenuItem:setTag(HomeMenuActionType.MenuItemStart)
--设置菜单
local settingSpriteNormal = cc.Sprite:createWithSpriteFrameName("button.setting.png")
local settingSpriteSelected = cc.Sprite:createWithSpriteFrameName("button.setting-on.png")
local settingMenuItem = cc.MenuItemSprite:create(startSpriteNormal,startSpriteSelected)
settingMenuItem:registerScriptTapHandler(menuItemCallBack)
settingMenuItem:setTag(HomeMenuActionType.MenuItemSetting)
--帮助菜单
local helpSpriteNormal = cc.Sprite:createWithSpriteFrameName("button.help.png")
local helpSpriteSelected = cc.Sprite:createWithSpriteFrameName("button.help-on.png")
local helpMenuItem = cc.MenuItemSprite:create(startSpriteNormal,startSpriteSelected)
helpMenuItem:registerScriptTapHandler(menuItemCallBack)
helpMenuItem:setTag(HomeMenuActionType.MenuItemHelp)
local mu = cc.Menu:create(startMenuItem,settingMenuItem,helpMenuItem)
mu:setPosition(size.width/2,size.height/2)
mu:alignItemsVerticallyWithPadding(12)
layer:addChild(mu)
--...
return layer
end
function HomeScene:onEnter()
cclog("onEnter...")
end
function HomeScene:onEnterTransitionFinish()
if defaults:getBoolForKey(MUSIC_KEY) then --判断MUSIC_KEY是否为true
AudioEngine.playMusic(bg_music_1,true)
end
end
function HomeScene:onExitTransitionStart()
end
function HomeScene:cleanup()
end