背景是Nil值?

我有一个程序,陨石会朝向一个角色落下,在第7行上,Corona模拟器抛出了错误“尝试索引本地变量'background'(一个无值的值)”,我不知道什么原因导致这种情况,希望有人能帮忙!

相关代码:

-- 开始物理引擎!
local physics = require ("physics")

-- 获取一个背景图片!
local screen_adjustment = 1
local background = display.newImage("Images/Background", true)
background.xScale = (screen_adjustment*background.contentWidth)/background.contentWidth
background.yScale = background.xScale
background.x = display.contentWidth / 2
background.y = display.contentHeight / 2
点赞
用户938694
用户938694

display.newImage返回nil。最可能是因为您没有名为Images/Background的文件。

2014-02-16 15:32:14
用户869951
用户869951

display.newImage 的文件名默认情况下被认为是在你的应用资源目录中,这个目录与你的 main.lua 相同。因此你的代码假设在包含 main.lua 的文件夹中有一个名为 Images 的文件夹,在其中还有一个名为 Background 的文件。你还需指定扩展名,如 Background.png

2014-02-16 16:05:38
用户1078537
用户1078537

因为无法创建图像,所以您的图像为 null。请仔细检查您的文件名和路径。您是否忘记了 Background.png 的扩展名?

2014-02-17 18:26:10