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 ) local sceneGroup = self.view -- Initialize the scene here -- Example: add display objects to "sceneGroup", add touch listeners, etc. local width = display.contentWidth local height = display.contentHeight local top = display.screenOriginY local left = display.screenOriginX local right = display.viewableContentWidth - left local bottom = display.viewableContentHeight - top local player, txt\_playerscore, txt\_playerlives, txt\_menu local tmr\_sendenemies, tmr\_sendbullets local enemy = {} local enemyCounter = 1 local bullets = {} local bulletsCounter = 1 local playerScore = 0 local playerLives = 3 local laser = audio.loadSound("sounds/laser.mp3") local ufohit = audio.loadSound("sounds/ufohit.mp3") local function returnToMenu() composer.gotoScene("menu", "slideRight") end local function sendenemies() local temp = math.random(1,4) enemy[enemyCounter] = display.newImageRect(sceneGroup, "images/ufo"..temp..".png", 40, 40) enemy[enemyCounter].x = right + enemy[enemyCounter].width enemy[enemyCounter].y = math.random(top+40, bottom-40) enemy[enemyCounter].name = "enemy" physics.addBody(enemy[enemyCounter]) transition.to(enemy[enemyCounter], {x=left-40, time=math.random(2500, 4000), onComplete=display.remove}) enemyCounter = enemyCounter + 1 end local function movePlayer(event) if(event.phase == "began") then transition.to(player, {y=event.y, time=100}) end end 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 local function onGameOver() timer.cancel(tmr\_sendenemies) timer.cancel(tmr\_sendbullets) Runtime:removeEventListener("touch", movePlayer) Runtime:removeEventListener("collision", onCollision) returnToMenu() end local function onCollision(event) if( (event.object1.name == "bullets" and event.object2.name == "enemy") or (event.object1.name == "enemy" and event.object2.name == "bullets")) then audio.play(ufohit) playerScore = playerScore + 1 txt\_playerscore.text = "Score: "..playerScore local pointsReward = display.newText(sceneGroup, "+1", 0, 0, native.systemFont, 14) pointsReward.x = event.object1.x pointsReward.y = event.object1.y transition.to(pointsReward, {alpha=0, delay=250, onComplete=display.remove}) local function removeObjects() display.remove(event.object1) display.remove(event.object2) end local tmr\_removeobjects = timer.performWithDelay(1, removeObjects, 1) end if( (event.object1.name == "player" and event.object2.name == "enemy") or (event.object1.name == "enemy" and event.object2.name == "player")) then playerLives = playerLives - 1 txt\_playerlives.text = "Lives: "..playerLives if(playerLives \<=0) then onGameOver() end if(event.object1.name == "enemy") then display.remove(enemy.object1) end if(event.object2.name) then display.remove(event.object2) end end end -- Create the background local background = display.newImageRect(sceneGroup, "images/background.png", display.actualContentWidth, display.actualContentHeight) background.x = width \* 0.5 background.y = height \* 0.5 -- Create the player ship player = display.newImageRect(sceneGroup, "images/player.png", 30, 40) player.x = left + 50 player.y = 50 player.name = "player" physics.addBody(player, "static") -- Create a text object for the player score txt\_playerscore = display.newText(sceneGroup, "Score: "..playerScore, 0, 0, native.systemFont, 14) txt\_playerscore.x = right - txt\_playerscore.width txt\_playerscore.y = bottom - txt\_playerscore.height -- Create a text object to track player lives txt\_playerlives = display.newText(sceneGroup, "Lives: "..playerLives, 0, 0, native.systemFont, 14) txt\_playerlives.x = left + txt\_playerlives.width txt\_playerlives.y = txt\_playerscore.y tmr\_sendenemies = timer.performWithDelay(1250, sendenemies, 0) tmr\_sendbullets = timer.performWithDelay(700, sendBullets, 0) Runtime:addEventListener("touch", movePlayer) 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 ) local sceneGroup = self.view -- Initialize the scene here -- Example: add display objects to "sceneGroup", add touch listeners, etc. local width = display.contentWidth local height = display.contentHeight local top = display.screenOriginY local left = display.screenOriginX local right = display.viewableContentWidth - left local bottom = display.viewableContentHeight - top local backgroundMusic = audio.loadStream("sounds/background.mp3") audio.play(backgroundMusic, {loops= -1}) local background = display.newImageRect(sceneGroup, "images/background.png", display.actualContentWidth, display.actualContentHeight) background.x = width \* 0.5 background.y = height \* 0.5 local title = display.newImageRect(sceneGroup, "images/title.png", 480, 183) title.x = width \* 0.5 title.y = top + 75 local function onStartTouch(event) if(event.phase == "ended") then composer.gotoScene("game", "fade") end end local btn\_start = widget.newButton{ width = 273, height = 71, defaultFile = "images/btn\_start.png", overFile = "images/btn\_start\_over.png", onEvent = onStartTouch } btn\_start.x = width \* 0.5 btn\_start.y = height \* 0.75 sceneGroup:insert(btn\_start) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen) elseif ( phase == "did" ) then -- Called when the scene is now on screen -- Insert code here to make the scene come alive -- Example: start timers, begin animation, play audio, etc. local previousScene = composer.getSceneName("previous") if(previousScene) then composer.removeScene(previousScene) end end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen) -- Insert code here to "pause" the scene -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then -- Called immediately after scene goes off screen end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view -- Insert code here to clean up the scene -- 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