Corona SDK - 弹出窗口(带有Director类)

时间:2012-04-04 13:55:38

标签: corona

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

main.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();

menu.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

2 个答案:

答案 0 :(得分: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属性更改为您想要的单词。

答案 1 :(得分: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