Hi all, I am still new at corona but here is what I was trying to do.
When I hit restartBtn, it runs a code that brings me to “restart.lua,” which loops me back to my “sprint.lua”(game level.)
It works the first time, however on the refresh the restartBtn does not work anymore. It will reset the score to zero, but it wont refresh the screen.
for testing purposes, I want the restartBtn so that I don’t have to restart the whole game.
time for some code…
local composer = require( "composer" ) local scene = composer.newScene() require ("object") require ("player") require ("variables") require ("objectProperties") \_G.gameMode = "sprint" --------------------------- local physics = nil --------------------------- local kidsBtn local easyBtn local advancedBtn local restartBtn local spawnRate = nil local spawnTimer local travelTime = nil local character = "pig" local lanes= {655, 555, 445} local item = "" local objectGroup = display.newGroup() local bgSpeed = 1500 local i = 1 local damage = nil local currentMode = "easyMode" local widget = require "widget" local appleArray = {1,2,4,5,6,7,8,9,11,10,3} local a = 1 local cartShape = nil ---------------------------------------------- local blenderSheetData = { width = 191 , height= 331, numFrames = 11, sheetContentWidth=385, sheetContentHeight=1990 } --blender sprite sheet local blenderSheet = graphics.newImageSheet("Items/Blender/blender\_Files.png", blenderSheetData)--actual display file local blenderData = { --blender animation {name = "fill", start = 1, count=11, time=400, loopCount = 0 } } local blender = display.newSprite (blenderSheet, blenderData) -- blender sprite blender.x, blender.y = 50,200 blender.xScale, blender.yScale = .4,.4 ----------------------------------------------- local player = nil local cart = nil -------------------------------------------------- local comboHighOptions = { --combo max, text = \_G.comboHigh, x = 510, y = 195, width = 600, --required for multiline and alignment height = 300, --required for multiline and alignment font = "Bigfish", fontSize = 50, align = "left" --new alignment field } local comboCounter = display.newEmbossedText( "", 185, 325, "Bigfish", 75 ) local comboHighCounter = display.newEmbossedText(comboHighOptions) comboHighCounter:setFillColor(255,255,0) ---------------------------------------------------- local scoreOptions = { --score text = \_G.score, x = 510, y = 150, width = 600, --required for multiline and alignment height = 300, --required for multiline and alignment font = "Bigfish", fontSize = 50, align = "left", --new alignment field strokeColor = 0, 0, 0, } local scoreBox = display.newEmbossedText( scoreOptions ) scoreBox:setFillColor(255,255,0) local maxImage = display.newImageRect ("GUI/max.png",400,100) maxImage.x, maxImage.y = 100,72 maxImage.xScale, maxImage.yScale = .5,.5 local scoreImage = display.newImageRect( "GUI/score.png",400,100) scoreImage.x, scoreImage.y = 100,25 scoreImage.xScale, scoreImage.yScale = .5,.5 local healthOptions = { --score text = "Health: "..\_G.health, x = 1010, y = 150, width = 600, --required for multiline and alignment height = 300, --required for multiline and alignment font = "Bigfish", fontSize = 50, align = "left", --new alignment field strokeColor = 0, 0, 0, } local healthCounter = display.newEmbossedText( healthOptions ) local cheeseOptions = {text = \_G.cheese, x = 850, y = 65, width = 600, height = 120, font = "Bigfish", fontSize = 75, align = "left"} local cheeseCounter = display.newEmbossedText( cheeseOptions ) print("1") local function modeSelect() if(currentMode == "kidsMode") then spawnRate = 1200; travelTime = 3700; end if(currentMode == "easyMode") then spawnRate = 1000; travelTime = 3300; end if(currentMode == "advancedMode") then spawnRate = 800; travelTime = 3000; end if(currentMode == "difficultMode") then spawnRate = 600; travelTime = 2600; end if(currentMode == "insaneMode") then spawnRate = 500; travelTime = 2000; end end function updateScore() cheeseCounter.text = \_G.cheese scoreBox.text = \_G.score comboCounter.xScale,comboCounter.yScale = 1,1 comboHighCounter.xScale,comboHighCounter.yScale = 1,1 if \_G.currentCombo \> \_G.comboHigh then \_G.comboHigh = \_G.currentCombo comboHighCounter.text = \_G.comboHigh end if \_G.currentCombo \< 5 then comboCounter.text = "" else comboCounter.text = \_G.currentCombo end transition.to(comboCounter,{time=400,xScale=1.5,yScale=1.5,}) end function updateHealth() healthCounter.text = "Health: "..\_G.health if \_G.health \<= 0 then healthCounter.text = "GAME OVER" --INSERT OTHER CODE HERE end end local function transition\_3() transition.to(comboHighCounter,{time=1000,x=comboHighCounter.x+50,y=comboHighCounter.y+50}) end local function transition\_2() transition.to(comboCounter,{time=1000,x=comboCounter.x+50,y=comboCounter.y+50}) end --removes crates local function removeObject(thisObject) display.remove( thisObject ) thisObject = nil end local function generateItem() local chance = math.random(100) if chance \< 5 then -- name, damage, stackable item = {"cheese", 0, "yes"} else if chance \< 63 then item = {"apple", 0, "yes"} else if chance \< 93 then item = {"crate", 50, "no"} else if chance \< 101 then item = {"steelCrate", 400, "no"} end end end end end --Spawns a single random item----- local function spawnRandomOne() local randomLane = lanes[math.random(1,3)] generateItem() local object = createObject(item[1],damage) objectGroup:insert(object) object.Name = "object".. i i = i+1 object.x = 1400 object.y = randomLane object.isHitTestable = true object:toBack() physics.addBody( object , { friction=0.5, bounce=0.3, radius=35} ) object:setCollision() object:setOnAnimationComplete() object.x = transition.to( object, { time=travelTime, x= -100, y = object.y ,onComplete=removeObject} ) end --This function spawns 2 items generated by the computer----- local function spawnRandomTwo() --Generates random numbers from the array "lanes" local randomLane = lanes[math.random(1,3)] local randomLane2 = lanes [math.random(1,3)] --This makes sure that the two variables are not the same-- if randomLane == randomLane2 then repeat randomLane2= lanes [math.random(1,3)] until randomLane ~= randomLane2 end ----------------------------------------------------------- --This creates the first item and assigns it to the randomLane variable, then adds the params generateItem() local object = createObject(item[1],item[2]) objectGroup:insert(object) object.Name = "object".. i i = i+1 object.x = 1400 object.y = randomLane object.isHitTestable = true object:toBack() physics.addBody( object , { friction=0.5, bounce=0.3, radius=35} ) object:setCollision() object:setOnAnimationComplete() object.x = transition.to( object, { time=travelTime, x= -100, y = object.y ,onComplete=removeObject} ) ------------------------------------------------------------ --This checks again to see if the variables are different numbers and assigns the 2nd item to a lane if randomLane2 ~= randomLane then generateItem() local object2 = createObject(item[1],item[2]) objectGroup:insert(object2) object2.Name = "object".. i i = i+1 object2.x = 1400 object2.y = randomLane2 object2.isHitTestable = true object2:toBack() physics.addBody( object2 , { friction=0.5, bounce=0.3, radius=35} ) object2:setCollision() object2:setOnAnimationComplete() object2.x = transition.to( object2, { time=travelTime, x= -100, y = object2.y ,onComplete=removeObject} ) end ----------------------------------------------------------- end local function spawnRandomThree() generateItem() local object = createObject(item[1],item[2]) objectGroup:insert(object) object.Name = item[1] object.Stackable = item[3] i = i+1 object.x = 1400 object.y = lanes[1] object.isHitTestable = true object:toBack() physics.addBody( object , { friction=0.5, bounce=0.3, radius=35} ) object:setCollision() object:setOnAnimationComplete() object.x = transition.to( object, { time=travelTime, x= -100, y = object.y ,onComplete=removeObject} ) generateItem() local object2 = createObject(item[1],item[2]) objectGroup:insert(object2) object2.Name = item[1] object2.Stackable = item[3] i = i+1 object2.x = 1400 object2.y = lanes[2] object2.stackable = item.stackable object2.isHitTestable = true object2:toBack() physics.addBody( object2 , { friction=0.5, bounce=0.3, radius=35} ) object2:setCollision() object2:setOnAnimationComplete() object2.x = transition.to( object2, { time=travelTime, x= -100, y = object2.y ,onComplete=removeObject} ) generateItem() if object.Stackable == "no" and object2.Stackable == "no" then print("yes") repeat generateItem() until item[3] ~= "no" end local object3 = createObject(item[1],item[2],item[3]) objectGroup:insert(object3) object3.Name = item[1] i = i+1 object3.x = 1400 object3.y = lanes[3] object3.isHitTestable = true object3:toBack() physics.addBody( object3 , { friction=0.5, bounce=0.3, radius=35} ) object3:setCollision() object3:setOnAnimationComplete() object3.x = transition.to( object3, { time=travelTime, x= -100, y = object3.y ,onComplete=removeObject} ) end local function spawnObject() local rNumber = math.random(100) if rNumber \< 33 then spawnRandomOne() else if rNumber \< 50 then spawnRandomTwo() else if rNumber \< 99 then spawnRandomThree() end end end end local function modeSelected() easyBtn:removeSelf() easyBtn = nil advancedBtn:removeSelf() advancedBtn = nil kidsBtn:removeSelf() kidsBtn = nil end local function restartBtnRelease() --this is the restartBtn code composer.gotoScene( "restart") player = nil cart = nil timer.cancel(spawnTimer) \_G.score = 0 updateScore() end -------Difficulty Select Buttons-------- local function kidsBtnRelease() currentMode = "kidsMode" modeSelect() modeSelected() spawnTimer = timer.performWithDelay( spawnRate, spawnObject, -1 ) return true -- indicates successful touch end local function easyBtnRelease() currentMode = "easyMode" modeSelect() modeSelected() spawnTimer = timer.performWithDelay( spawnRate, spawnObject, -1 ) return true -- indicates successful touch end local function advancedBtnRelease() currentMode = "advancedMode" modeSelect() modeSelected() spawnTimer = timer.performWithDelay( spawnRate, spawnObject, -1 ) return true -- indicates successful touch end function scene:create( event ) print("2") local sceneGroup = self.view physics = require "physics" physics.setGravity( 10, 10) physics.setDrawMode( "hybrid" ) physics.start() --sky background local sky = display.newImageRect( "Backgrounds/nightSky.png",1920,1080) sky.x = 500 sky.y = 300 --cheese score local cheeseDisplay = display.newImageRect( "Items/cheese.png",75,75) cheeseDisplay.x = 500 cheeseDisplay.y = 50 --street image local streetData = { width=1080, height=504, numFrames=4, sheetContentWidth=1084, sheetContentHeight=2018 } local streetSheet = graphics.newImageSheet( "Animations/Street/streetData0.png", streetData) --create street animation local streetSequence = { { name = "movement", start=1, count=4, time=200 }, } --created 2 street animations for now, until we make another one that fits. local street = display.newSprite( streetSheet, streetSequence ) street.x, street.y = 300,550 street.xScale,street.yScale = .7,.7 street:play() --2nd street animation local street2 = display.newSprite( streetSheet, streetSequence ) street2.x, street2.y = 1041,550 street2.xScale,street2.yScale = .7,.7 street2:play() -- Background buildings image sheet local buildingData = { width=640, height=360, numFrames=13, sheetContentWidth=1920, sheetContentHeight=1800 } local buildingSheet = graphics.newImageSheet( "Animations/Buildings/buildingsData.png", buildingData) --Background City Buildings Animations local buildingSequence = { { name= "movement", start=1, count=13, time= bgSpeed, loopCount=0 } } --create building sprite and size local buildings = display.newSprite( buildingSheet, buildingSequence ) buildings.x = 640 buildings.y = 200 buildings.xScale,buildings.yScale = 2,2 buildings:play() player = createPlayer(character) player.x, player.y = 200,515 cart = createCart(currentCart) cart.x, cart.y = 185,550 local cartShape = { -10, -10, 40, -10, 40, 20, -10, 20} player:applySwipeLayer() physics.addBody( cart, "static", { friction=0.5, bounce=0.3, shape=cartShape } ) sceneGroup:insert(sky) sceneGroup:insert(buildings) sceneGroup:insert(street) sceneGroup:insert(street2) sceneGroup:insert(blender) sceneGroup:insert(cheeseDisplay) sceneGroup:insert(comboHighCounter) sceneGroup:insert(comboCounter) sceneGroup:insert(scoreBox) sceneGroup:insert(maxImage) sceneGroup:insert(scoreImage) sceneGroup:insert(healthCounter) sceneGroup:insert(cheeseCounter) sceneGroup:insert(player) sceneGroup:insert(cart) kidsBtn = widget.newButton{ label="Kids Mode", labelColor = { default={200}, over={400} }, default="button.png", over="button-over.png", fontSize = 50, width=154, height=40, onRelease = kidsBtnRelease -- event listener function } kidsBtn.x = display.contentWidth\*.5 kidsBtn.y = display.contentHeight\*.3 easyBtn = widget.newButton{ label="Easy Mode", labelColor = { default={200}, over={400} }, default="button.png", over="button-over.png", fontSize = 50, width=154, height=40, onRelease = easyBtnRelease -- event listener function } easyBtn.x = display.contentWidth\*.5 easyBtn.y = display.contentHeight\*.5 advancedBtn = widget.newButton{ label="Advanced Mode", labelColor = { default={200}, over={400} }, default="button.png", over="button-over.png", fontSize = 50, width=154, height=40, onRelease = advancedBtnRelease -- event listener function } advancedBtn.x = display.contentWidth\*.5 advancedBtn.y = display.contentHeight\*.7 restartBtn = widget.newButton{ --creation of btn label="Restart", labelColor = { default={200}, over={400} }, default="button.png", over="button-over.png", fontSize = 40, width=154, height=40, onRelease = restartBtnRelease -- event listener function } restartBtn.x = 1200 restartBtn.y = 30 --sceneGroup:insert(kidsBtn) --sceneGroup:insert(easyBtn) --sceneGroup:insert(advancedBtn) --sceneGroup:insert(restartBtn) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase local bgSound1 = audio.loadStream( "Sounds/gameFace.mp3" ) if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then audio.play(bgSound1, { channel=1, loops=-1, fadein=5000 } ) end end function updateApples() if a==10 then blender:setFrame(appleArray[0]) a = 1 \_G.cheese = \_G.cheese + 1 cheeseCounter.text = \_G.cheese --add cheese here, and cheese animation else a = a+1 blender:setFrame(appleArray[a]) end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) elseif phase == "did" then -- Called when the scene is now off screen end end function scene:destroy( event ) -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to clean up the scene -- e.g. remove display objects, remove touch listeners, save state, etc. local sceneGroup = self.view physics.removeBody( cart, cartShape ) -- i know i am not timer.cancel(spawnTimer) -- destroying properly... composer.recycleOnSceneChange = false print("garbage collected") end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene
I have been playing around with it, because I still don’t quite understand how I should organize my code… Hopefully someone here could help me out a little