0

我是游戏开发的新手,我正在尝试为我当前的项目创建一个简单的 GUI 框架。我目前正在使用 Director Class 1.4 进行场景管理。我有我的项目o改变场景,但现在我想创建一个弹出窗口。我只想让弹出窗口显示在我所在的当前场景上。下面是我的 main.lua 和 menu.lua(我的初始场景)的代码。如果有人可以帮助我,我将不胜感激。请尽量具体,因为我对 Corona 和一般编程非常陌生。

主程序.lua

_W = display.contentWidth
_H = display.contentHeight

local director = require ("director");

local mainGroup = display.newGroup();

local function main()

mainGroup:insert(director.directorView);

director:changeScene("menu");

return true;
end

main();

菜单.lua

module(..., package.seeall)

function new()
    local localGroup = display.newGroup();

    local bg = display.newImage("Images/background1.PNG");  

    local myText = display.newText("Merchant", 0, 0, native.systemFont, 24)
    myText:setTextColor(255, 255, 255)
    myText:setReferencePoint(display.CenterReferencePoint);
    myText.x = _W/2; myText.y = _H/2;

    local hero_btn = display.newImage("Images/weaponcraft.PNG", 25, 25);
    hero_btn:setReferencePoint(display.BottomLeftReferencePoint);
    hero_btn.x = 252; hero_btn.y = 475;
    hero_btn.scene = "heroMain";

    local craft_btn = display.newImage("Images/smithing.PNG", 25, 25);
    craft_btn:setReferencePoint(display.BottomLeftReferencePoint);
    craft_btn.x = 7; craft_btn.y = 475; 
    craft_btn.scene = "craftMain";

    local inventory_btn = display.newImage("Images/inventory1.png");
    inventory_btn:setReferencePoint(display.CenterReferencePoint);
    inventory_btn.x = _W/2; inventory_btn.y = 430;
    --inventory_btn.scene = "inventory";

    function changeScene(e)
        if(e.phase == "ended") then
            director:changeScene(e.target.scene);
        end
    end     

    localGroup:insert(bg);
    localGroup:insert(hero_btn);
    localGroup:insert(craft_btn);

    hero_btn:addEventListener("touch", changeScene);
    craft_btn:addEventListener("touch", changeScene);

    return localGroup;
end
4

2 回答 2

0

您可以创建自定义弹出窗口,并可以自己处理我正在放置示例代码。

--function to create a dialog to be shown on the game over
local function gameOverAlertScreen()
    --Display item used in the popup screen
    local alertBox , restartBtn, levelBtn, soundOnOffSwitch, quitOnOffSwitch, imageSheetSoundOnOff, imageSheetQuitOnOff
    --initial constans used for positioning the display items
    local startTX = -400
    local startTY = halfH
    local btnInitailY = halfH -50
    local btnMargin = 10

    --cancel the game pausse timer
    if(gameTimer_main) then
        timer.cancel(gameTimer_main)
        gameTimer_main = nil
    end

    local optionSoundSheet =
    {
        -- The params below are required
        width = 51,height = 51,numFrames = 2,-- The params below are optional; used for dynamic resolution support
        sheetContentWidth = 102,  -- width of original 1x size of entire sheet
        sheetContentHeight = 51  -- height of original 1x size of entire sheet
    }
    local optionQuitSheet =
    {
        -- The params below are required
        width = 51,height = 51,numFrames = 2,-- The params below are optional; used for dynamic resolution support
        sheetContentWidth = 102,  -- width of original 1x size of entire sheet
        sheetContentHeight = 51  -- height of original 1x size of entire sheet
    }

    isFirstTime=true
    --pauseScreen()
    dialog=true

    alertBox = display.newImage("image/popup_dialog.png")
    alertBox.x = halfW; alertBox.y = halfH
    alertBox:setReferencePoint(display.CenterReferencePoint)

    --creating the restart button fot the popup dialog
    restartBtn = widget.newButton{
        defaultFile="image/replay_btn.png",overFile = "image/replay_btn_hover.png",
        isEnable=true,onRelease =  onReplayBtnGameOverRelease -- event listener function
    }
    restartBtn.x = halfW
    restartBtn.y = btnInitailY

    --creating the level button
    levelBtn = widget.newButton{
        defaultFile="image/menu.png",overFile = "image/menu_hover.png",
        isEnable=true, onRelease =  onLevelBtnGameOverRelease -- event listener function
    }
    levelBtn.x = halfW
    levelBtn.y = restartBtn.y + restartBtn.height + btnMargin

    --creating the sound on off switch
    imageSheetSoundOnOff = graphics.newImageSheet( "image/sound.png", optionSoundSheet )
    soundOnOffSwitch = widget.newSwitch
    {
        left = screenW * 0.18,top = screenH * 0.73, style = "checkbox", sheet = imageSheetSoundOnOff,
        initialSwitchState = soundOn,frameOff = "2",frameOn =  "1",onRelease = onSoundButtonClicked, onPress = onSoundButtonClicked,
    }


    --creating the quit on off switch
    imageSheetQuitOnOff = graphics.newImageSheet( "image/vibration.png", optionQuitSheet )

    quitOnOffSwitch = widget.newSwitch
    {
        left = screenW * 0.7,top = screenH * 0.735,style = "checkbox",sheet = imageSheetQuitOnOff,onPress = onViberationButtonClicked,
        initialSwitchState = vibrationOn,frameOff = "2",frameOn =  "1",
    }
    --soundOnOffSwitch:setState({ isOn = soundOn })
    --quitOnOffSwitch:setState({ isOn = vibrationOn})

    --create/position logo/title image on upper-half of the screen
    local titleLogo = display.newImageRect( "image/gameover.png",144,30)
    titleLogo:setReferencePoint( display.CenterReferencePoint )
    titleLogo.x = halfW
    titleLogo.y =  btnInitailY - 65

    if popupGameOverGroup == nil then
        popupGameOverGroup = display.newGroup()
    end
    -- inserting the buttons and the alert dialog to the popup group
    popupGameOverGroup:insert(alertBox)
    popupGameOverGroup:insert(restartBtn)
    popupGameOverGroup:insert(levelBtn)
    popupGameOverGroup:insert(soundOnOffSwitch)
    popupGameOverGroup:insert(quitOnOffSwitch)
    popupGameOverGroup:insert(titleLogo)

    transition.from(popupGameOverGroup, {time =1000,x=startTX,y=titleLogo.y,xScale = 1, yScale = 1,
        transition = easing.inOutExpo})

    localGroup:insert(popupGameOverGroup)

    showAdd()
end
于 2013-07-16T08:39:51.740 回答
0

您可以使用该native.showAlert功能。这将显示一个弹出对话框。

像这样的东西:

local function onComplete( event )

local action = event.action
if "clicked" == event.action then
    if 1 == event.index then

    end
end

local alert = native.showAlert( "You are in scene1!", "Congratulations!", { "OK" }, onComplete )

这将显示一个对话框,其中包含“您在场景 1 中!” 标题,“恭喜!” 副标题,以及单击(或点击)它时关闭对话框的“确定”按钮。

将这些代码放在场景的前面,并将native.showAlert属性更改为您想要的单词。

于 2012-05-25T23:21:51.747 回答