"Attempt to index upvalue 'level' (a nill value)" <-- only on Android device, works in simulator

I get the following error when running my game on my Android device although it works perfectly in the simulator :

“Attempt to index upvalue ‘level’ (a nill value)” for line 105

Line 105: level.x, level.y = display.contentCenterX, display.contentCenterY

This is my code:

------------------------------------------------------------------------------------------------------------ -- Init ------------------------------------------------------------------------------------------------------------ ------ Delta Time ------ local runtime = 0 local function getDeltaTime() local temp = system.getTimer() -- Get current game time in ms local dt = (temp-runtime) / (1000/60) -- 60 fps or 30 fps as base runtime = temp -- Store game time return dt end local deltaTime = getDeltaTime() ------ Game ------ local composer = require("composer") local scene = composer.newScene() local level = nil local solid = display.newGroup() local player = nil local options = { -- Player Sprite Sheet options width = 69, height = 145, numFrames = 3 } -- Physics local physics = require "physics" local gravity = 50 physics.start() physics.setGravity(0, gravity\*deltaTime) -- Input local leftInputArrow local rightInputArrow -- Player States local pressLeft local pressRight local pressUp ------------------------------------------------------------------------------------------------------------ -- Functions ------------------------------------------------------------------------------------------------------------ -- Right Input Arrow function activateRightInputArrow(self,event) player:setLinearVelocity(400\*deltaTime, 0) player.xScale = 1; end -- left Input Arrow function activateLeftInputArrow(self,event) player:setLinearVelocity(-400\*deltaTime, 0) player.xScale = -1; end function touchScreen(event) if event.phase == "ended" then end end function touchObjects(event) print(event.target.id) if event.phase == "began" or event.phase == "moving" then display.currentStage:setFocus(event.target) if event.target.id == "pressLeft" then pressLeft.enterFrame = activateLeftInputArrow Runtime:addEventListener("enterFrame", pressLeft) Runtime:removeEventListener("enterFrame", pressRight) end if event.target.id == "pressRight" then pressRight.enterFrame = activateRightInputArrow Runtime:addEventListener("enterFrame", pressRight) Runtime:removeEventListener("enterFrame", pressLeft) end end if event.phase == "ended" then Runtime:removeEventListener("enterFrame", pressRight) Runtime:removeEventListener("enterFrame", pressLeft) display.currentStage:setFocus(nil) end end function playerLogic() end ------------------------------------------------------------------------------------------------------------ -- Scene Control ------------------------------------------------------------------------------------------------------------ function scene:create(event) local screenGroup = self.view -- Level level = display.newImage("assets\\level1.png") level.x, level.y = display.contentCenterX, display.contentCenterY level.alpha = 1 local solid1 = display.newRect(solid, 960, 848, 1920, 100) solid1:setFillColor(1, 0, 0) physics.addBody(solid1, "static", {bounce = 0, friction = 1}) screenGroup:insert(solid1) screenGroup:insert(level) -- Input pressLeft = display.newImage("assets\\InputArrowRun.png") pressLeft.x = 150 pressLeft.y = 940 pressLeft.id = "pressLeft" pressLeft:addEventListener("touch", touchObjects) pressRight = display.newImage("assets\\InputArrowRun.png") pressRight.x = 300 pressRight.y = 940 pressRight.xScale = -1 pressRight.id = "pressRight" pressRight:addEventListener("touch", touchObjects) pressUp = display.newImage("assets\\InputArrowRun.png") pressUp.x = 1770 pressUp.y = 940 pressUp.rotation = 90 -- Player playerSpriteSheet = graphics.newImageSheet("assets\\Max4Strip.png", options) player = display.newImage(playerSpriteSheet, 1) player.x = 200 player.y = 700 physics.addBody(player, "dynamic", {bounce = 0, density = 2, friction = 1}) player.isFixedRotation = true end function scene:show(event) end function scene:hide(event) end function scene:destroy(event) end function enterFrame() deltaTime = getDeltaTime() physics.setGravity( 0, gravity\*deltaTime ) end scene:addEventListener("create", scene) scene:addEventListener("show", scene) scene:addEventListener("hide", scene) scene:addEventListener("destroy", scene) Runtime:addEventListener("enterFrame", enterFrame) Runtime:addEventListener("touch", touchScreen) return scene

This line of code failed:

level &nbsp;&nbsp;&nbsp;&nbsp;= display.newImage("assets\\level1.png")

If you look at your console log on your device there will be a warning about not being able to find level1.png.

First almost all of our devices would use  “assets/leve1.png”. I’m thinking the double backslashes are the problem. For Windows, the OS uses a single backslash to indicate directories, but devices and OS X are all based on Unix/Linux which are front slashes. Our simulator on Windows probably handles both ways, but you should be using a single forward slash.

Also on device, filenames are case sensitive, on the simulator, they are not. So if your file is really level1.PNG it will be fine on the sim, but not on the device. Check that as well.

Rob

Thanks, that fixed it.

This line of code failed:

level &nbsp;&nbsp;&nbsp;&nbsp;= display.newImage("assets\\level1.png")

If you look at your console log on your device there will be a warning about not being able to find level1.png.

First almost all of our devices would use  “assets/leve1.png”. I’m thinking the double backslashes are the problem. For Windows, the OS uses a single backslash to indicate directories, but devices and OS X are all based on Unix/Linux which are front slashes. Our simulator on Windows probably handles both ways, but you should be using a single forward slash.

Also on device, filenames are case sensitive, on the simulator, they are not. So if your file is really level1.PNG it will be fine on the sim, but not on the device. Check that as well.

Rob

Thanks, that fixed it.