So basically my game works flawlessly on the first execution of the game , everything works as it is supposed too. When I try to repeat the game everything goes to bad here are some examples:
-
Game Ending Instantly
-
Random Collisions (Causing Game to end)
-
After 5-6 repeats i usally will get an error “toFront is a nil value” or something else that is been working for the past 5 times, and is now all the sudden broken
menu.lua
local storyboard = require( "storyboard" ) local scene = storyboard.newScene() -- include Corona's "widget" library local widget = require "widget" local function onPlayBtnRelease(event) -- go to level1.lua scene storyboard.gotoScene( "level1", "fade", 500 ) return true end function scene:createScene( event ) local screenGroup = self.view -- Drawing background local background = display.newImageRect( "assets/backgroundColor.png", display.contentWidth, display.contentHeight ) background:setReferencePoint( display.TopLeftReferencePoint ) background.x, background.y = 0, 0 screenGroup:insert(background) cloud = display.newImage("assets/nebula.png") screenGroup:insert(cloud) cloud2 = display.newImage("assets/nebula.png") cloud2.y = 500 cloud2.x = 500 cloud3 = display.newImage("assets/nebula.png") screenGroup:insert(cloud2) cloud3.y = 345 cloud3.x = 430 screenGroup:insert(cloud3) star =display.newImage("assets/starSmall.png") star.x = math.random(5 ,display.contentWidth ) star.y = math.random(5,display.contentHeight) screenGroup:insert(star) star1 =display.newImage("assets/starSmall.png") star1.x = math.random(5 ,display.contentWidth ) star1.y = math.random(5,display.contentHeight) screenGroup:insert(star1) star2 =display.newImage("assets/starSmall.png") star2.x = math.random(5 ,display.contentWidth ) star2.y = math.random(5,display.contentHeight) screenGroup:insert(star2) star3 =display.newImage("assets/starSmall.png") star3.x = math.random(5 ,display.contentWidth ) star3.y = math.random(5,display.contentHeight) screenGroup:insert(star3) star4 =display.newImage("assets/starSmall.png") star4.x = math.random(5 ,display.contentWidth ) star4.y = math.random(5,display.contentHeight) screenGroup:insert(star4) star5 =display.newImage("assets/starSmall.png") star5.x = math.random(5 ,display.contentWidth ) star5.y = math.random(5,display.contentHeight) screenGroup:insert(star5) star6 =display.newImage("assets/starSmall.png") star6.x = math.random(5 ,display.contentWidth ) star6.y = math.random(5,display.contentHeight) screenGroup:insert(star6) star7 =display.newImage("assets/starSmall.png") star7.x = math.random(5 ,display.contentWidth ) star7.y = math.random(5,display.contentHeight) screenGroup:insert(star7) star8 =display.newImage("assets/starSmall.png") star8.x = math.random(5 ,display.contentWidth ) star8.y = math.random(5,display.contentHeight) screenGroup:insert(star8) -- create/position logo/title image on upper-half of the screen printScore = display.newEmbossedText("Space Race", 0, 0, native.systemFont, 48, { 255, 255, 255, 255 }) printScore.x = display.contentWidth/2 printScore.y = display.contentHeight \*.2 printScore:setTextColor( 240 ,248,255 ) screenGroup:insert(printScore) print("im a menu") playBtn = widget.newButton { left = 100, top = 200, id = "playBtn", label = "Play !", onRelease = onPlayBtnRelease } playBtn.x = display.contentWidth/2 playBtn.y = display.contentHeight \*.4 screenGroup:insert(playBtn) end function scrollClouds(self,event) if self.y \> display.viewableContentHeight + 400 then self.y = -100 self.x = math.random(0,display.contentWidth) printScore:toFront() else self.y = self.y + .5 end end function scrollStars(self,event) if self.y \> display.contentHeight + 5 then self.y = math.random(0, 10) self.x = math.random(0,display.contentWidth) else self.y = self.y + 3 end end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local group = self.view local prior\_scene = storyboard.getPrevious() --s storyboard.purgeScene( prior\_scene ) -- storyboard.removeScene("level1") cloud.enterFrame = scrollClouds Runtime:addEventListener("enterFrame", cloud) cloud2.enterFrame = scrollClouds Runtime:addEventListener("enterFrame", cloud2) cloud3.enterFrame = scrollClouds Runtime:addEventListener("enterFrame", cloud3) -- add stars-- star.enterFrame = scrollStars Runtime:addEventListener("enterFrame", star) star1.enterFrame = scrollStars Runtime:addEventListener("enterFrame", star1) star2.enterFrame = scrollStars Runtime:addEventListener("enterFrame", star2) star3.enterFrame = scrollStars Runtime:addEventListener("enterFrame", star3) star4.enterFrame = scrollStars Runtime:addEventListener("enterFrame", star4) star5.enterFrame = scrollStars Runtime:addEventListener("enterFrame", star5) star6.enterFrame = scrollStars Runtime:addEventListener("enterFrame", star6) star7.enterFrame = scrollStars Runtime:addEventListener("enterFrame", star7) star8.enterFrame = scrollStars Runtime:addEventListener("enterFrame", star8) end -- Called when scene is about to move offscreen: function scene:exitScene( event ) local group = self.view Runtime:removeEventListener("enterFrame", cloud) Runtime:removeEventListener("enterFrame", cloud2) Runtime:removeEventListener("enterFrame", cloud3) Runtime:removeEventListener("enterFrame", star) Runtime:removeEventListener("enterFrame", star1) Runtime:removeEventListener("enterFrame", star2) Runtime:removeEventListener("enterFrame", star3) Runtime:removeEventListener("enterFrame", star4) Runtime:removeEventListener("enterFrame", star5) Runtime:removeEventListener("enterFrame", star6) Runtime:removeEventListener("enterFrame", star7) Runtime:removeEventListener("enterFrame", star8) end function scene:destroyScene( event ) local group = self.view playBtn:removeSelf( ) end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene
level1.lua
----------------------------------------------------------------------------------------- -- -- menu.lua -- ----------------------------------------------------------------------------------------- local storyboard = require( "storyboard" ) local scene = storyboard.newScene() -- include Corona's "widget" library local widget = require "widget" local function onPlayBtnRelease(event) -- go to level1.lua scene storyboard.gotoScene( "level1", "fade", 500 ) return true end function scene:createScene( event ) local screenGroup = self.view -- Drawing background local background = display.newImageRect( "assets/backgroundColor.png", display.contentWidth, display.contentHeight ) background:setReferencePoint( display.TopLeftReferencePoint ) background.x, background.y = 0, 0 screenGroup:insert(background) cloud = display.newImage("assets/nebula.png") screenGroup:insert(cloud) cloud2 = display.newImage("assets/nebula.png") cloud2.y = 500 cloud2.x = 500 cloud3 = display.newImage("assets/nebula.png") screenGroup:insert(cloud2) cloud3.y = 345 cloud3.x = 430 screenGroup:insert(cloud3) star =display.newImage("assets/starSmall.png") star.x = math.random(5 ,display.contentWidth ) star.y = math.random(5,display.contentHeight) screenGroup:insert(star) star1 =display.newImage("assets/starSmall.png") star1.x = math.random(5 ,display.contentWidth ) star1.y = math.random(5,display.contentHeight) screenGroup:insert(star1) star2 =display.newImage("assets/starSmall.png") star2.x = math.random(5 ,display.contentWidth ) star2.y = math.random(5,display.contentHeight) screenGroup:insert(star2) star3 =display.newImage("assets/starSmall.png") star3.x = math.random(5 ,display.contentWidth ) star3.y = math.random(5,display.contentHeight) screenGroup:insert(star3) star4 =display.newImage("assets/starSmall.png") star4.x = math.random(5 ,display.contentWidth ) star4.y = math.random(5,display.contentHeight) screenGroup:insert(star4) star5 =display.newImage("assets/starSmall.png") star5.x = math.random(5 ,display.contentWidth ) star5.y = math.random(5,display.contentHeight) screenGroup:insert(star5) star6 =display.newImage("assets/starSmall.png") star6.x = math.random(5 ,display.contentWidth ) star6.y = math.random(5,display.contentHeight) screenGroup:insert(star6) star7 =display.newImage("assets/starSmall.png") star7.x = math.random(5 ,display.contentWidth ) star7.y = math.random(5,display.contentHeight) screenGroup:insert(star7) star8 =display.newImage("assets/starSmall.png") star8.x = math.random(5 ,display.contentWidth ) star8.y = math.random(5,display.contentHeight) screenGroup:insert(star8) -- create/position logo/title image on upper-half of the screen printScore = display.newEmbossedText("Space Race", 0, 0, native.systemFont, 48, { 255, 255, 255, 255 }) printScore.x = display.contentWidth/2 printScore.y = display.contentHeight \*.2 printScore:setTextColor( 240 ,248,255 ) screenGroup:insert(printScore) print("im a menu") playBtn = widget.newButton { left = 100, top = 200, id = "playBtn", label = "Play !", onRelease = onPlayBtnRelease } playBtn.x = display.contentWidth/2 playBtn.y = display.contentHeight \*.4 screenGroup:insert(playBtn) end function scrollClouds(self,event) if self.y \> display.viewableContentHeight + 400 then self.y = -100 self.x = math.random(0,display.contentWidth) printScore:toFront() else self.y = self.y + .5 end end function scrollStars(self,event) if self.y \> display.contentHeight + 5 then self.y = math.random(0, 10) self.x = math.random(0,display.contentWidth) else self.y = self.y + 3 end end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local group = self.view local prior\_scene = storyboard.getPrevious() --s storyboard.purgeScene( prior\_scene ) -- storyboard.removeScene("level1") cloud.enterFrame = scrollClouds Runtime:addEventListener("enterFrame", cloud) cloud2.enterFrame = scrollClouds Runtime:addEventListener("enterFrame", cloud2) cloud3.enterFrame = scrollClouds Runtime:addEventListener("enterFrame", cloud3) -- add stars-- star.enterFrame = scrollStars Runtime:addEventListener("enterFrame", star) star1.enterFrame = scrollStars Runtime:addEventListener("enterFrame", star1) star2.enterFrame = scrollStars Runtime:addEventListener("enterFrame", star2) star3.enterFrame = scrollStars Runtime:addEventListener("enterFrame", star3) star4.enterFrame = scrollStars Runtime:addEventListener("enterFrame", star4) star5.enterFrame = scrollStars Runtime:addEventListener("enterFrame", star5) star6.enterFrame = scrollStars Runtime:addEventListener("enterFrame", star6) star7.enterFrame = scrollStars Runtime:addEventListener("enterFrame", star7) star8.enterFrame = scrollStars Runtime:addEventListener("enterFrame", star8) end -- Called when scene is about to move offscreen: function scene:exitScene( event ) local group = self.view Runtime:removeEventListener("enterFrame", cloud) Runtime:removeEventListener("enterFrame", cloud2) Runtime:removeEventListener("enterFrame", cloud3) Runtime:removeEventListener("enterFrame", star) Runtime:removeEventListener("enterFrame", star1) Runtime:removeEventListener("enterFrame", star2) Runtime:removeEventListener("enterFrame", star3) Runtime:removeEventListener("enterFrame", star4) Runtime:removeEventListener("enterFrame", star5) Runtime:removeEventListener("enterFrame", star6) Runtime:removeEventListener("enterFrame", star7) Runtime:removeEventListener("enterFrame", star8) end function scene:destroyScene( event ) local group = self.view playBtn:removeSelf( ) end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene
reset.lua
local storyboard = require( "storyboard" ) local scene = storyboard.newScene() function scene:createScene( event ) local group = self.view resetLevels() storyboard.gotoScene("level1") end function scene:willEnterScene( event ) local group = self.view end function resetLevels() storyboard.purgeScene("level1") storyboard.removeScene("level") end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local group = self.view end -- Called when scene is about to move offscreen: function scene:exitScene( event ) local group = self.view ----------------------------------------------------------------------------- -- INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.) ----------------------------------------------------------------------------- end -- Called prior to the removal of scene's "view" (display group) function scene:destroyScene( event ) local group = self.view ----------------------------------------------------------------------------- -- INSERT code here (e.g. remove listeners, widgets, save state, etc.) ----------------------------------------------------------------------------- end -- Called if/when overlay scene is displayed via storyboard.showOverlay() function scene:overlayBegan( event ) local group = self.view local overlay\_name = event.sceneName -- name of the overlay scene ----------------------------------------------------------------------------- -- This event requires build 2012.797 or later. ----------------------------------------------------------------------------- end -- Called if/when overlay scene is hidden/removed via storyboard.hideOverlay() function scene:overlayEnded( event ) local group = self.view local overlay\_name = event.sceneName -- name of the overlay scene ----------------------------------------------------------------------------- -- This event requires build 2012.797 or later. ----------------------------------------------------------------------------- end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "willEnterScene" event is dispatched before scene transition begins scene:addEventListener( "willEnterScene", scene ) -- "enterScene" event is dispatched whenever scene transition has finished scene:addEventListener( "enterScene", scene ) -- "exitScene" event is dispatched before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "didExitScene" event is dispatched after scene has finished transitioning out scene:addEventListener( "didExitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) -- "overlayBegan" event is dispatched when an overlay scene is shown scene:addEventListener( "overlayBegan", scene ) -- "overlayEnded" event is dispatched when an overlay scene is hidden/removed scene:addEventListener( "overlayEnded", scene ) --------------------------------------------------------------------------------- return scene
I have also attached the zip file of this game (it’s open source any way :D )