I have this little game working however I am unable to get the current scene removed before I go to a new scene.
Game is simple, I have falling objects, and a character, if the character collides with a specific object type then score is increased, but if the character collides with another specific object type then the game is supposed to end and go to the main menu. Which works, but if I go back to the game scene the game is still going, and if I add scene.removeScene() then I get an error.
Error:
Attempt to index field 'view' (a nil value)
Here is the code in question FYI: I have tried typing in the actual scene name into the removeScene() function
local function playerCollision (self, event) if event.phase == "began" then if event.other.type == "food" then score = score + 1 print(score) else composer.removeScene(composer.getSceneName( "current" ), true) composer.gotoScene("mainMenu", {effect="slideRight", time=300}) end event.other:removeSelf() end end --event lister for movement bg:addEventListener("touch", movePlayer) currentPlayer.collision = playerCollision currentPlayer:addEventListener("collision", currentPlayer) end
Here is the full code
local composer = require("composer") local scene = composer.newScene() local bg local name local score = 0 local pileUp = true local mRandom = math.random local mAbs = math.abs local physics = require("physics") physics:start() physics.setDrawMode("hybrid") local currentPlayer local ground local objects = {"apple.png", "mango.png", "orange.png", "pear.png", "bomb.png", "bomb.png", "bomb.png"} local math = require("math") function scene:create(e) bg = display.newImageRect( "images/background2.png", width\*2, height ) bg.x = 0 bg.y = y name = display.newText(e.params.player..":"..score,0,0,system.nativeFont,24) name.x = 60 name.y = 0 name:setFillColor(.4,0,.2) currentPlayer = display.newCircle( x, display.actualContentHeight-150, 25 ) physics.addBody(currentPlayer, "static", {density=1, friction = .5, radius = 25}) ground = display.newRect(x,display.actualContentHeight-40,display.actualContentWidth, 50) scene.view:insert(bg) scene.view:insert(name) scene.view:insert(currentPlayer) scene.view:insert(ground) end function scene:show(e) local function spawnObjects() local objIdx = mRandom(#objects) local objName = objects[objIdx] local object = display.newImageRect("images/"..objName, 45,45) object.x = mRandom(20, display.actualContentWidth-100) object.y = mRandom(-10, 0) object.rotation = mRandom(-60, -10 ) if objIdx \< 5 then object.type = "food" else object.type = "bomb" end scene.view:insert(object) physics.addBody(object, "dynamic", {density=1, friction = .5, radius = 25}) end timer.performWithDelay(mRandom(1500, 4000),spawnObjects, 0) -- Move player local function movePlayer(event) if event.phase == "began" then local speed = (1500/display.actualContentWidth - display.screenOriginX)\*(mAbs(currentPlayer.x - event.x)) transition.to(currentPlayer, {time=speed, x = event.x}) end end local function playerCollision (self, event) if event.phase == "began" then if event.other.type == "food" then score = score + 1 print(score) else composer.removeScene(composer.getSceneName( "current" ), true) composer.gotoScene("mainMenu", {effect="slideRight", time=300}) end event.other:removeSelf() end end --event lister for movement bg:addEventListener("touch", movePlayer) currentPlayer.collision = playerCollision currentPlayer:addEventListener("collision", currentPlayer) end function scene:hide(e) if e.phase == "will" then end end scene:addEventListener("create", scene) scene:addEventListener("show", scene) scene:addEventListener("hide", scene) return scene