Corona Overlay并未显示

时间:2017-08-19 23:44:19

标签: lua overlay corona

我在场景之间创建加载场景时遇到了问题,我得到的解决方案是使用叠加层,我已经尝试了好几天,但由于某种原因我无法使用它。我有两个场景,一个显示项目列表,另一个显示基于单击哪一行的文本页面。问题现在是第一个场景,显示列表,下载要显示的图像,当然这需要一段时间,所以每当我转换到该屏幕时,应用程序似乎被冻结,而一切都加载。

我发现它确实进入了叠加场景,因为我输入的打印语句打印出来,但它不会显示任何内容,当我删除隐藏的代码时叠加之后,它仍然显示为挂起,它进入叠加场景,不显示任何内容,然后在最终显示叠加层之前渲染表格,而不是显示叠加层,将所有内容加载到下方,然后隐藏叠加,它似乎在当前场景中被冻结,加载下面的所有内容,显示叠加,然后立即隐藏叠加。

我的三个场景的代码在下面,我现在发布的那个是唯一一个实际工作过一次然后再来一次,它显示了叠加,而所有东西都按照我想要加载但是出于某种原因,它那次之后再也没有工作过。我对此感到非常沮丧,没有任何论坛给我一个解决方案,我真的很感激一些帮助。谢谢!

ItemListPage.lua

local composer = require ( "composer" )
local widget = require( "widget" )
local json = require( "json" )

-- Load the relevant LuaSocket modules
local http = require( "socket.http" )
local ltn12 = require( "ltn12" )

local scene = composer.newScene()


--NavigationBar elements initiated
--Removed for readability


--image handler
local function networkListener( event )
    if ( event.isError ) then
        print ( "Network error - download failed" )
    end

    print ( "event.response.fullPath: ", event.response.fullPath )
    print ( "event.response.filename: ", event.response.filename )
    print ( "event.response.baseDirectory: ", event.response.baseDirectory )
end


local function onRowRender( event )

    -- Get reference to the row group
    local row = event.row
    local params=event.row.params
    local itemRow=3;

    -- Cache the row "contentWidth" and "contentHeight" because the row bounds can change as children objects are added
    local rowHeight = row.contentHeight
    local rowWidth = row.contentWidth

    row.rowTitle = display.newText( row, params.topic, 0, 0, nil, 14 )
    row.rowTitle:setFillColor( 0 )
    row.rowTitle.anchorX = 0
    row.rowTitle.x = 0
    row.rowTitle.y = (rowHeight/2) * 0.5

    --Other elements removed for readabilty (it's all just text objects)

    --Download Image
    --params referring to items[i]
    local imagelink =params.imagelink

    -- Create local file for saving data
    local path = system.pathForFile( params.imagename, system.TemporaryDirectory )
    myFile = io.open( path, "w+b" ) 

    -- Request remote file and save data to local file
    http.request{
        url = imagelink, 
        sink = ltn12.sink.file( myFile )
    }

    row.Image = display.newImageRect(row, params.imagename, system.TemporaryDirectory, 25, 25)
    row.Image.x = 20
    row.Image.y = (rowHeight/2) * 1.5

    row:insert( row.rowTitle )
    row:insert( row.Image )
end

local function onRowTouch( event )
    local row = event.target
    local params=event.target.params

    composer.removeScene(composer.getSceneName("current"))
composer.gotoScene( "itempage" , {params=params})

end


function scene:create( event )
    local sceneGroup = self.view

end

function scene:show( event )
    local sceneGroup = self.view
    local phase = event.phase

    if phase == "will" then
        -- Called when the scene is still off screen and is about to move on screen
        --overlay
        composer.showOverlay( "loading", { isModal = true })

    elseif phase == "did" then

        --Table stuff
        local scrollBarOptions = {
            sheet = scrollBarSheet,  -- Reference to the image sheet
            topFrame = 1,            -- Number of the "top" frame
            middleFrame = 2,         -- Number of the "middle" frame
            bottomFrame = 3          -- Number of the "bottom" frame
        }
        -- Table
        local tableView = widget.newTableView(
            {
                left = 0,
                top = navBar.height,
                height = display.contentHeight-navBar.height,
                width = display.contentWidth,
                onRowRender = onRowRender,
                onRowTouch = onRowTouch,
                listener = scrollListener
            }
        )

        --json work
        local filename = system.pathForFile( "items.json", system.ResourceDirectory )
        local decoded, pos, msg = json.decodeFile( filename )

        if not decoded then
            print( "Decode failed at "..tostring(pos)..": "..tostring(msg) )
        else
            print( "File successfully decoded!" )
        end
        local items=decoded.items

        -- create a white background to fill screen
        local background = display.newRect( display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight )
        background:setFillColor( 1 )    -- white
        sceneGroup:insert( background )

        -- Insert rows
        for i = 1, #items do
            -- Insert a row into the tableView
            print( "Adding a row!" )
            tableView:insertRow{
                rowHeight = 100,
                rowColor = { default={ 0.8, 0.8, 0.8, 0.8 } },
                lineColor = { 1, 0, 0 },
                params=items[i]
            }
        end

        sceneGroup:insert( tableView )

        composer.hideOverlay( "fade", 100 )
    end 
end

-- other functions and elements unused and removed for readability

loading.lua

local composer = require ( "composer" )
local widget = require( "widget" )
local json = require( "json" )
local scene = composer.newScene()

-- Create the widget
function scene:create( event )
    local sceneGroup = self.view
    local background = display.newRect( display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight )
    background:setFillColor( 1 )    -- white

    local text = display.newText( "Loading scene", 0, 0, nil, 14 )
    text:setFillColor( 0 )
    text.anchorX = display.contentCenterX
    text.x = display.contentCenterX
    text.y = display.contentCenterY

    sceneGroup:insert( background )
    sceneGroup:insert( text )
    print ( "In loading create")

end


function scene:show( event )
    local sceneGroup = self.view
    local phase = event.phase

    if phase == "will" then

    elseif phase == "did" then

        print ( "In loading show")
    end 
end

-- other functions and elements unused and removed for readability

ItemDisplayPage.lua

local composer = require ( "composer" )
local widget = require( "widget" )
local json = require( "json" )
local scene = composer.newScene()


--NavigationBar elements initiated
--This creates the "back button", when clicked it returns to the previous scene, in this case "itemListPage"
--it takes, no parameters
local function handleLeftButton( event )
   if ( event.phase == "ended" ) then
        composer.removeScene(composer.getSceneName("current"))
    composer.gotoScene(composer.getSceneName("previous"))
   end
   return true
end
--Remaining navbar elements removed for readability

function scene:create( event )
local sceneGroup = self.view
local params=event.params

-- create a white background to fill screen
local background = display.newRect( display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight )
background:setFillColor( 1 )    -- white

--creating header bar
local bar = display.newRect( navBar.height + (headerBarHeight*0.5), display.contentCenterY, display.contentWidth, headerBarHeight )
bar:setFillColor( 1 )

-- create stuff
local title = display.newText(params.topic, 0, 0, nil, 14 )
title:setFillColor( 0 )
title.anchorX = 0
title.x = margin
title.y = ((2*headerBarHeight/2) * 0.5)+navBar.height

local Image = display.newImageRect(params.imagename, system.TemporaryDirectory, 25, 25)
Image.x = 50
Image.y = display.contentCenterY


-- all objects must be added to group (e.g. self.view)
sceneGroup:insert( background )
sceneGroup:insert( title )
sceneGroup:insert( Image)

end
-- other functions and elements unused and removed for readability

1 个答案:

答案 0 :(得分:0)

我建议您不要使用scene.show事件进行加载。

使用timer.performWithDelay加载所有数据:

--in scene:show
elseif phase == "did" then
  timer.performWithDelay(0, function()
    local scrollBarOptions = {
    --put your code here
    composer.hideOverlay( "fade", 100 )
  end)

您当前的代码没有显示叠加层,因为引擎会在渲染任何内容之前等待场景:显示事件。因此,在加载所有图像之后,重叠和图像的渲染出现

在我的代码中timer.performWithDelay没有阻止scene:show执行,因此您会在加载图片之前看到叠加效果