如何在Corona SDK中创建加载屏幕?

时间:2013-05-30 22:26:53

标签: android iphone loading corona

我的游戏是高清,我使用了很多高清图像和精灵表,占用了大量的纹理内存。结果是,在我的场景加载之前,我得到了一个丑陋的黑色屏幕,持续了几秒钟。所以我想制作一个加载屏幕。其实两个。一个用于我的主菜单,一个用于我的主游戏。我整天搜索了很多,但是我没有找到任何加载屏幕的步骤。

我想做什么:

- 有一个加载屏幕,只有一个文字说“加载...”和另一个文本,其中有一个百分比计算下一个屏幕资产的加载量。

- 我完成后,我想删除我的加载屏幕并立即启动我的主菜单场景或主要游戏场景。

我正在为Android开发,但欢迎任何关于iPhone的评论。

故事板如何知道我的场景跟踪是否加载以及百分比是多少?我应该把我的newImageRects放在哪里?我找不到一个教程。

2 个答案:

答案 0 :(得分:7)

在你的main.lua中你应该创建一个函数 loadAlImages(),你将在其中加载所有的高清图像和精灵表。

local loadingText = display.newText("LOADING ...", 0, 0, native.systemFont, 24)
myText:setTextColor(255, 255, 255)

local function loadAlImages()
    --create all your images here.

    --remove LOADING text
end

--if you still see black screen at the start try to increase delay > 500 ms  
timer.performWithDelay( 500, loadAlImages, 1 )

现在,如果您想显示和更新另一个文本,其中包含计算下一个屏幕资源加载量的百分比,您应该创建图像,精灵为 .isVisible = false ,当它们都是创建更改 .isVisible = true 。 您可以在创建一些图像后添加一些更新百分比文本的代码。

local loadingText = display.newText("LOADING ...", 0, 0, native.systemFont, 24)
myText:setTextColor(255, 255, 255)

local function loadAlImages()
    --create some images here.
    --update text's percentage to 20%
    --create some images here.
    --update text's percentage to 50%
    --create some sprites here.
    --update text's percentage to 90%
    --change **.isVisible=true** for all your created files but **.alpha=0**
    --update text's percentage to 100%
    --remove LOADING text
    --transition .alpha of all images to 1
end

timer.performWithDelay( 500, loadAlImages, 1 )

我认为您可以将所有图片文件放在一个显示组中,并在此组中设置 .isVisible = false 。这将为您节省一些代码行。 .alpha = 0

的情况相同

有很多方法。您可以声明变量然后在loadAlImages()函数中创建它们,或者您可以将它们全部放在表中并使用该表来获取所需的图像。 第一个例子:

local image

local function loadAlImages()
    --create some images here.

    image = display.newImageRect( "image.png", 100, 100 )
    image:setReferencePoint( display.CenterReferencePoint )
    image.x = display.contentCenterX
    image.y = display.contentCenterY

    --create some sprites here.
end

表格示例:

local imagesTable = { }

local function loadAlImages()
    --create some images here.

    local image = display.newImageRect( "image.png", 100, 100 )
    image:setReferencePoint( display.CenterReferencePoint )
    image.x = display.contentCenterX
    image.y = display.contentCenterY
    imagesTable.image = image

    --create some sprites here.
end

更多信息:
http://lua-users.org/wiki/ScopeTutorial
http://www.coronalabs.com/blog/2011/06/21/understanding-lua-tables-in-corona-sdk/
http://lua-users.org/wiki/TablesTutorial

答案 1 :(得分:1)

我认为,在场景中使用之前预先加载所有图像。

display.newImage()

功能会做到这一点。 所以这是你应该做的:

1.对图像不做任何操作,但使用display.newImage()函数调用加载图像。这将显示加载屏幕。调用之后,等待500ms并调用所有其他图像。当游戏进入主菜单时,删除加载屏幕 我的意思是:

local loadImg = display.newImageRect( "loading.png" .. blah blah )

timer.performWithDelay( 500, function() -- load all other images then main() end, 1 )