When i restart a game the following error appears: game.lua:70: bad argument #-1 to 'newImageRect' (Proxy expected, got nil)

I have tried to remove previous scene, but this didn’t solve the problem. Actually, i have another app with similar error

This is the line 70:

bullets[bulletsCounter] = display.newImageRect(sceneGroup, "images/bullet.png", 21, 5)

It is located inside the function:

local function sendBullets()         bullets[bulletsCounter] = display.newImageRect(sceneGroup, "images/bullet.png", 21, 5)         bullets[bulletsCounter].x = player.x + 25         bullets[bulletsCounter].y = player.y         bullets[bulletsCounter].name = "bullets"         physics.addBody(bullets[bulletsCounter])         transition.to(bullets[bulletsCounter], {x=right+40, time=2000, onComplete=display.remove})         audio.play(laser)     end

This is game.lua file.

local composer = require( "composer" ) local scene = composer.newScene() local widget = require("widget") local physics = require("physics") physics.start() physics.setGravity(0, 0) -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called -- ----------------------------------------------------------------------------------------------------------------- -- Local forward references should go here -- ------------------------------------------------------------------------------- -- "scene:create()" function scene:create( event ) &nbsp;&nbsp;&nbsp; local sceneGroup = self.view &nbsp;&nbsp;&nbsp; -- Initialize the scene here &nbsp;&nbsp;&nbsp; -- Example: add display objects to "sceneGroup", add touch listeners, etc. &nbsp;&nbsp;&nbsp; local width = display.contentWidth &nbsp;&nbsp;&nbsp; local height = display.contentHeight &nbsp;&nbsp;&nbsp; local top = display.screenOriginY &nbsp;&nbsp;&nbsp; local left = display.screenOriginX &nbsp;&nbsp;&nbsp; local right = display.viewableContentWidth - left &nbsp;&nbsp;&nbsp; local bottom = display.viewableContentHeight - top &nbsp;&nbsp;&nbsp; local player, txt\_playerscore, txt\_playerlives, txt\_menu &nbsp;&nbsp;&nbsp; local tmr\_sendenemies, tmr\_sendbullets &nbsp;&nbsp;&nbsp; local enemy = {} &nbsp;&nbsp;&nbsp; local enemyCounter = 1 &nbsp;&nbsp;&nbsp; local bullets = {} &nbsp;&nbsp;&nbsp; local bulletsCounter = 1 &nbsp;&nbsp;&nbsp; local playerScore = 0 &nbsp;&nbsp;&nbsp; local playerLives = 3 &nbsp;&nbsp;&nbsp; local laser = audio.loadSound("sounds/laser.mp3") &nbsp;&nbsp;&nbsp; local ufohit = audio.loadSound("sounds/ufohit.mp3") &nbsp;&nbsp;&nbsp; local function returnToMenu() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; composer.gotoScene("menu", "slideRight") &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; local function sendenemies() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local temp = math.random(1,4) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; enemy[enemyCounter] = display.newImageRect(sceneGroup, "images/ufo"..temp..".png", 40, 40) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; enemy[enemyCounter].x = right + enemy[enemyCounter].width &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; enemy[enemyCounter].y = math.random(top+40, bottom-40) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; enemy[enemyCounter].name = "enemy" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; physics.addBody(enemy[enemyCounter]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transition.to(enemy[enemyCounter], {x=left-40, time=math.random(2500, 4000), onComplete=display.remove}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; enemyCounter = enemyCounter + 1 &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; local function movePlayer(event) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(event.phase == "began") then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transition.to(player, {y=event.y, time=100}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; local function sendBullets() bullets[bulletsCounter] = display.newImageRect(sceneGroup, "images/bullet.png", 21, 5) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bullets[bulletsCounter].x = player.x + 25 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bullets[bulletsCounter].y = player.y &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bullets[bulletsCounter].name = "bullets" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; physics.addBody(bullets[bulletsCounter]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transition.to(bullets[bulletsCounter], {x=right+40, time=2000, onComplete=display.remove}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; audio.play(laser) &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; local function onGameOver() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; timer.cancel(tmr\_sendenemies) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; timer.cancel(tmr\_sendbullets) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Runtime:removeEventListener("touch", movePlayer) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Runtime:removeEventListener("collision", onCollision) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; returnToMenu() &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; local function onCollision(event) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if( (event.object1.name == "bullets" and event.object2.name == "enemy") or (event.object1.name == "enemy" and event.object2.name == "bullets")) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; audio.play(ufohit) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; playerScore = playerScore + 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txt\_playerscore.text = "Score: "..playerScore &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local pointsReward = display.newText(sceneGroup, "+1", 0, 0, native.systemFont, 14) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pointsReward.x = event.object1.x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pointsReward.y = event.object1.y &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transition.to(pointsReward, {alpha=0, delay=250, onComplete=display.remove}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local function removeObjects() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; display.remove(event.object1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; display.remove(event.object2) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local tmr\_removeobjects = timer.performWithDelay(1, removeObjects, 1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if( (event.object1.name == "player" and event.object2.name == "enemy") or (event.object1.name == "enemy" and event.object2.name == "player")) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; playerLives = playerLives - 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txt\_playerlives.text = "Lives: "..playerLives &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(playerLives \<=0) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; onGameOver() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(event.object1.name == "enemy") then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; display.remove(enemy.object1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(event.object2.name) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; display.remove(event.object2) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; -- Create the background &nbsp;&nbsp;&nbsp; local background = display.newImageRect(sceneGroup, "images/background.png", display.actualContentWidth, display.actualContentHeight) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; background.x = width \* 0.5 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; background.y = height \* 0.5 &nbsp;&nbsp;&nbsp; -- Create the player ship &nbsp;&nbsp;&nbsp; player = display.newImageRect(sceneGroup, "images/player.png", 30, 40) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; player.x = left + 50 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; player.y = 50 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; player.name = "player" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; physics.addBody(player, "static") &nbsp;&nbsp;&nbsp; -- Create a text object for the player score &nbsp;&nbsp;&nbsp; txt\_playerscore = display.newText(sceneGroup, "Score: "..playerScore, 0, 0, native.systemFont, 14) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txt\_playerscore.x = right - txt\_playerscore.width &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txt\_playerscore.y = bottom - txt\_playerscore.height &nbsp;&nbsp;&nbsp; -- Create a text object to track player lives &nbsp;&nbsp;&nbsp; txt\_playerlives = display.newText(sceneGroup, "Lives: "..playerLives, 0, 0, native.systemFont, 14) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txt\_playerlives.x = left + txt\_playerlives.width &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; txt\_playerlives.y = txt\_playerscore.y &nbsp;&nbsp;&nbsp; tmr\_sendenemies = timer.performWithDelay(1250, sendenemies, 0) &nbsp;&nbsp;&nbsp; tmr\_sendbullets = timer.performWithDelay(700, sendBullets, 0) &nbsp;&nbsp;&nbsp; Runtime:addEventListener("touch", movePlayer) &nbsp;&nbsp;&nbsp; Runtime:addEventListener("collision", onCollision) end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) -- ------------------------------------------------------------------------------- return scene

This is menu.lua file

local composer = require( "composer" ) local scene = composer.newScene() local widget = require("widget") -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called -- ----------------------------------------------------------------------------------------------------------------- -- Local forward references should go here -- ------------------------------------------------------------------------------- -- "scene:create()" function scene:create( event ) &nbsp;&nbsp;&nbsp; local sceneGroup = self.view &nbsp;&nbsp;&nbsp; -- Initialize the scene here &nbsp;&nbsp;&nbsp; -- Example: add display objects to "sceneGroup", add touch listeners, etc. &nbsp;&nbsp;&nbsp; local width = display.contentWidth &nbsp;&nbsp;&nbsp; local height = display.contentHeight &nbsp;&nbsp;&nbsp; local top = display.screenOriginY &nbsp;&nbsp;&nbsp; local left = display.screenOriginX &nbsp;&nbsp;&nbsp; local right = display.viewableContentWidth - left &nbsp;&nbsp;&nbsp; local bottom = display.viewableContentHeight - top &nbsp;&nbsp;&nbsp; local backgroundMusic = audio.loadStream("sounds/background.mp3") &nbsp;&nbsp;&nbsp; audio.play(backgroundMusic, {loops= -1}) &nbsp;&nbsp;&nbsp; local background = display.newImageRect(sceneGroup, "images/background.png", display.actualContentWidth, display.actualContentHeight) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; background.x = width \* 0.5 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; background.y = height \* 0.5 &nbsp;&nbsp;&nbsp; local title = display.newImageRect(sceneGroup, "images/title.png", 480, 183) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; title.x = width \* 0.5 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; title.y = top + 75 &nbsp;&nbsp;&nbsp; local function onStartTouch(event) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(event.phase == "ended") then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; composer.gotoScene("game", "fade") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; local btn\_start = widget.newButton{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; width = 273, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; height = 71, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; defaultFile = "images/btn\_start.png", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; overFile = "images/btn\_start\_over.png", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; onEvent = onStartTouch &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; btn\_start.x = width \* 0.5 &nbsp;&nbsp;&nbsp; btn\_start.y = height \* 0.75 &nbsp;&nbsp;&nbsp; sceneGroup:insert(btn\_start) end -- "scene:show()" function scene:show( event ) &nbsp;&nbsp;&nbsp; local sceneGroup = self.view &nbsp;&nbsp;&nbsp; local phase = event.phase &nbsp;&nbsp;&nbsp; if ( phase == "will" ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- Called when the scene is still off screen (but is about to come on screen) &nbsp;&nbsp;&nbsp; elseif ( phase == "did" ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- Called when the scene is now on screen &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- Insert code here to make the scene come alive &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- Example: start timers, begin animation, play audio, etc. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local previousScene = composer.getSceneName("previous") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(previousScene) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; composer.removeScene(previousScene) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; end end -- "scene:hide()" function scene:hide( event ) &nbsp;&nbsp;&nbsp; local sceneGroup = self.view &nbsp;&nbsp;&nbsp; local phase = event.phase &nbsp;&nbsp;&nbsp; if ( phase == "will" ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- Called when the scene is on screen (but is about to go off screen) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- Insert code here to "pause" the scene &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- Example: stop timers, stop animation, stop audio, etc. &nbsp;&nbsp;&nbsp; elseif ( phase == "did" ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- Called immediately after scene goes off screen &nbsp;&nbsp;&nbsp; end end -- "scene:destroy()" function scene:destroy( event ) &nbsp;&nbsp;&nbsp; local sceneGroup = self.view &nbsp;&nbsp;&nbsp; -- Called prior to the removal of scene's view &nbsp;&nbsp;&nbsp; -- Insert code here to clean up the scene &nbsp;&nbsp;&nbsp; -- Example: remove display objects, save state, etc. end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

What is line 70 of game.lua? It’s a call to display.newImageRect().

Why would the first parameter you’re passing be nil?

Rob

This is the line 70:

bullets[bulletsCounter] = display.newImageRect(sceneGroup, "images/bullet.png", 21, 5)

It is located inside the function:

local function sendBullets() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bullets[bulletsCounter] = display.newImageRect(sceneGroup, "images/bullet.png", 21, 5) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bullets[bulletsCounter].x = player.x + 25 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bullets[bulletsCounter].y = player.y &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bullets[bulletsCounter].name = "bullets" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; physics.addBody(bullets[bulletsCounter]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transition.to(bullets[bulletsCounter], {x=right+40, time=2000, onComplete=display.remove}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; audio.play(laser) &nbsp;&nbsp;&nbsp; end

For some reason it thinks “sceneGroup” is nil, but I don’t see any reason in your code why that would be the case.

You never increment bulletCounter so you’re overwriting the bullet each time. Is this happening right away on the first bullet or later on?

Rob

When i play the game for the 1st time, it works absolutely fine. This error appears when i play it again. To be more specific, this happens when an enemy collides with the player

This is send enemies function: (i have tested the app again, and it says the error is located here, line 52,

and it says the same: game.lua:52: bad argument #-1 to ‘newImageRect’ (Proxy expected, got nil) )

local function sendenemies() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local temp = math.random(1,4) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; enemy[enemyCounter] = display.newImageRect(sceneGroup, "images/ufo"..temp..".png", 40, 40) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; enemy[enemyCounter].x = right + enemy[enemyCounter].width &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; enemy[enemyCounter].y = math.random(top+40, bottom-40) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; enemy[enemyCounter].name = "enemy" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; physics.addBody(enemy[enemyCounter]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transition.to(enemy[enemyCounter], {x=left-40, time=math.random(2500, 4000), onComplete=display.remove}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; enemyCounter = enemyCounter + 1 &nbsp;&nbsp;&nbsp; end

Here, i tried to delete the

enemyCounter = enemyCounter + 1

but it didn’t solve the problem

One possibility is that your collision listener is not getting removed:

 local function onGameOver() timer.cancel(tmr\_sendenemies) timer.cancel(tmr\_sendbullets) Runtime:removeEventListener("touch", movePlayer) Runtime:removeEventListener("collision", onCollision) returnToMenu() end

The function onCollision is defined after this function. So when Lua processes this file, it scans top to bottom in a single pass. When it parses the removeEventListener() line, onCollision hasn’t been previously defined so it gets a memory address in global space. Just below this function you have onCollision defined but it will have a different addresses than the created global variable uses in the function.

The simple solution is to move this function below your onCollision() function and see if that helps.

Rob

Thank you so much, Rob!! Indeed, this solved the problem, however, i removed “local” from onGameOver function, otherwise it shows an error.

You have a chicken and egg problem. One function needs to know about the other one before it’s defined. Making the function global solves the problem, but if you have another onGameOver function in another scene (unlikely), they would trash each other.  “onGameOver” is likely only going to be in your game scene, but other functions like onCollision you might have in multiple places.

Typically you can place:

local onGameOver

at the top of the scene outside of any function. Then when you declare the function, instead of either:

local function onGameOver -- or function onGameOver

you can use an anonymous function declaration to allow you to keep it local and work around the chicken and egg problem.

onGameOver = function()

Give that a try!

Rob

Thank you! It works. I have done the same things for another app, and it works too. great.

What is line 70 of game.lua? It’s a call to display.newImageRect().

Why would the first parameter you’re passing be nil?

Rob

This is the line 70:

bullets[bulletsCounter] = display.newImageRect(sceneGroup, "images/bullet.png", 21, 5)

It is located inside the function:

local function sendBullets() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bullets[bulletsCounter] = display.newImageRect(sceneGroup, "images/bullet.png", 21, 5) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bullets[bulletsCounter].x = player.x + 25 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bullets[bulletsCounter].y = player.y &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bullets[bulletsCounter].name = "bullets" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; physics.addBody(bullets[bulletsCounter]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transition.to(bullets[bulletsCounter], {x=right+40, time=2000, onComplete=display.remove}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; audio.play(laser) &nbsp;&nbsp;&nbsp; end

For some reason it thinks “sceneGroup” is nil, but I don’t see any reason in your code why that would be the case.

You never increment bulletCounter so you’re overwriting the bullet each time. Is this happening right away on the first bullet or later on?

Rob

When i play the game for the 1st time, it works absolutely fine. This error appears when i play it again. To be more specific, this happens when an enemy collides with the player

This is send enemies function: (i have tested the app again, and it says the error is located here, line 52,

and it says the same: game.lua:52: bad argument #-1 to ‘newImageRect’ (Proxy expected, got nil) )

local function sendenemies() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local temp = math.random(1,4) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; enemy[enemyCounter] = display.newImageRect(sceneGroup, "images/ufo"..temp..".png", 40, 40) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; enemy[enemyCounter].x = right + enemy[enemyCounter].width &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; enemy[enemyCounter].y = math.random(top+40, bottom-40) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; enemy[enemyCounter].name = "enemy" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; physics.addBody(enemy[enemyCounter]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transition.to(enemy[enemyCounter], {x=left-40, time=math.random(2500, 4000), onComplete=display.remove}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; enemyCounter = enemyCounter + 1 &nbsp;&nbsp;&nbsp; end

Here, i tried to delete the

enemyCounter = enemyCounter + 1

but it didn’t solve the problem

One possibility is that your collision listener is not getting removed:

 local function onGameOver() timer.cancel(tmr\_sendenemies) timer.cancel(tmr\_sendbullets) Runtime:removeEventListener("touch", movePlayer) Runtime:removeEventListener("collision", onCollision) returnToMenu() end

The function onCollision is defined after this function. So when Lua processes this file, it scans top to bottom in a single pass. When it parses the removeEventListener() line, onCollision hasn’t been previously defined so it gets a memory address in global space. Just below this function you have onCollision defined but it will have a different addresses than the created global variable uses in the function.

The simple solution is to move this function below your onCollision() function and see if that helps.

Rob

Thank you so much, Rob!! Indeed, this solved the problem, however, i removed “local” from onGameOver function, otherwise it shows an error.

You have a chicken and egg problem. One function needs to know about the other one before it’s defined. Making the function global solves the problem, but if you have another onGameOver function in another scene (unlikely), they would trash each other.  “onGameOver” is likely only going to be in your game scene, but other functions like onCollision you might have in multiple places.

Typically you can place:

local onGameOver

at the top of the scene outside of any function. Then when you declare the function, instead of either:

local function onGameOver -- or function onGameOver

you can use an anonymous function declaration to allow you to keep it local and work around the chicken and egg problem.

onGameOver = function()

Give that a try!

Rob

Thank you! It works. I have done the same things for another app, and it works too. great.