1
votes

I have a program in which meteors fall toward a character and at line 7 corona simulator throws the error "attempt to index local 'background' (a nil value)" I dont know what is causing this and was hoping someone could help!

Relavent Code:

--Start the physics engine!
local physics = require ("physics")

--Get a background image!
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
3

3 Answers

1
votes

display.newImage returns nil. Most likely because you don't have a file called Images/Background.

1
votes

The filename given to display.newImage is assumed (by default) to reside in your app's resources directory, which is the same folder that contains your main.lua. Your code therefore assumes that in the folder that contains main.lua you have a folder called Images and in there, a file called Background. You have to give the extension as well such as Background.png.

0
votes

Your image is nil because it failed to create it. Double check your filename and path. Are you forgetting the extension for Background.png?