cocos-2dx 随记- 打飞机(HomeScene.lua) - 第6天

本文介绍如何使用Cocos2d-x创建主菜单场景,包括菜单项响应、过渡效果及背景音乐处理等内容。文章详细展示了如何通过Lua脚本实现不同菜单选项的功能,如启动游戏、进入设置页面等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值