Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

cannot transition frome game.lua to restart.lua
Started by Davy1222 Aug 23 2018 10:49 AM

12 replies to this topic
restart collision

Best Answer nick_sherman , 23 August 2018 - 11:16 AM

You aren't inserting any of your objects into sceneGroup, therefore they will not be automatically removed by Composer.

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Davy1222

[GLOBAL: userInfoPane.html]
Davy1222
  • Observer

  • 12 posts
  • Corona SDK

my game is supposed to restart once a collision happens with my penguin and the ice,  but once the collision it happens the game just stops and the replay screen is not shown and nothing from the game.lua goes away

if i click where the play button is supposed to be in the restart.lua it'll start the game but the game just restart and the ice blocks are invisible

 

 

this is my code for the game 

 

-----------------------------------------------------------------------------------------
--
-- main.lua
-- 
----------------------------------------------------------------------------
 
display.setStatusBar(display.HiddenStatusBar)
 
local _W = display.contentWidth
local _H = display.contentHeight
local scrollSpeed = 3
 
local physics = require "physics"
physics.start()
 
local composer = require( "composer" )
local scene = composer.newScene()
 
 
--21 here you declare all your local variables:
local score = 0
 
--24 here you declare all your local functions:  -- whatever your code requires...
local function doSomething()
    score = score + 1
end
 
local function move(event)
bg2.x = bg2.x - scrollSpeed/2
bg3.x = bg3.x - scrollSpeed/2
bg4.x = bg4.x - scrollSpeed/2
bg5.x = bg5.x - scrollSpeed/2
bg6.x = bg6.x - scrollSpeed/2
bg7.x = bg7.x - scrollSpeed
bg8.x = bg8.x - scrollSpeed
bg9.x = bg9.x - scrollSpeed
bg10.x = bg10.x - scrollSpeed
bg11.x = bg11.x - scrollSpeed
 
if(-bg2.x + bg2.contentWidth) > 1080 then
bg2:translate(2000,0)
end
if(-bg3.x + bg3.contentWidth) > 1080 then
bg3:translate(2000,0)
end
if(-bg4.x + bg4.contentWidth) > 1080 then
bg4:translate(2000,0)
end
if(-bg5.x + bg5.contentWidth) > 1080 then
bg5:translate(2000,0)
end
if(-bg6.x + bg6.contentWidth) > 1080 then
bg6:translate(2000,0)
end
if(-bg7.x + bg7.contentWidth) > 1080 then
bg7:translate(2000,0)
end
if(-bg8.x + bg8.contentWidth) > 1080 then
bg8:translate(2000,0)
end
if(-bg9.x + bg9.contentWidth) > 1080 then
bg9:translate(2000,0)
end
if(-bg10.x + bg10.contentWidth) > 1080 then
bg10:translate(2000,0)
end
if(-bg11.x + bg11.contentWidth) > 1080 then
bg11:translate(2000,0)
end
end
 
 
local function activatePengs(self,event)
self:applyForce(0, -45, self.x, self.y)
 
end
 
local function touchScreen(event)
-- print("touch")
if event.phase == "began" then
peng.enterFrame = activatePengs
Runtime:addEventListener("enterFrame", peng)
end
 
if event.phase == "ended" then
Runtime:removeEventListener("enterFrame", peng)
 
end
end
 
-- local function endGame()
    -- composer.gotoScene( "restart", { time=800, effect="crossFade" } )
-- end
 
local function onCollision(event)
if event.phase == "began" then
print "collide"
composer.gotoScene( "restart" )
end
end
 
 
 
 
-- now comes four required functions for Composer:
 
function scene:create( event )
 
    local sceneGroup = self.view
 
    -- put any thing you need to create here
local bg1 = display.newImageRect("bg.png", 800, 1000)
bg1.x = _W*1/2; bg1.y = _H/2;
 
bg2 = display.newImage("ice2.png",140,210)
 
bg3 = display.newImage("ice2.png",540,210)
 
bg4 = display.newImage("ice2.png",940,210)
 
bg5 = display.newImage("ice2.png",1340,210)
 
bg6 = display.newImage("ice2.png",1740,210)
 
bg7 = display.newImage("ice1.png",140,420)
 
bg8 = display.newImage("ice1.png",540,420)
 
bg9 = display.newImage("ice1.png",940,420)
 
bg10 = display.newImage("ice1.png",1340,420)
 
bg11 = display.newImage("ice1.png",1740,420)
 
peng = display.newImage("peng.png", 80, 201)
physics.addBody(peng, "dynamic", {density=.18, bounce=0.1, friction=.5, radius=55})
 
icebok = display.newImage("icebok.png", 480, 201)
physics.addBody(icebok, "static", {density=.1, bounce=0.1, friction=.5, radius=1})
icebok.speed = 4
icebok.initY = icebok.y + math.random(0,500)
-- icebok.amp = math.random(10,50)
-- icebok.angle = math.random(1,720)
 
icebok1 = display.newImage("icebok.png", 680, 301)
physics.addBody(icebok1, "static", {density=.1, bounce=0.1, friction=.5, radius=1})
icebok1.speed = 4
icebok1.initY = icebok1.y + math.random(0,500)
-- icebok1.amp = math.random(10,50)
-- icebok1.angle = math.random(1,720)
 
icebok2 = display.newImage("icebok.png", 880, 401)
physics.addBody(icebok2, "static", {density=.1, bounce=0.1, friction=.5, radius=1})
icebok2.speed = 4
icebok2.initY = icebok2.y + math.random(0,500)
-- icebok2.amp = math.random(10,50)
-- icebok2.angle = math.random(1,720)
 
 
icebok3 = display.newImage("icebok.png", 1080, 451)
physics.addBody(icebok3, "static", {density=.1, bounce=0.1, friction=.5, radius=1})
icebok3.speed = 4
icebok3.initY = icebok3.y + math.random(0,500)
-- icebok2.amp = math.random(10,50)
-- icebok2.angle = math.random(1,720)
 
 
function moveiceboks(self,event)
if self.x < -10 then
self.x = math.random (480,1080)
self.y = math.random(0,500)
self.speed = 4
-- self.amp = math.random(10,50)
-- self.angle = math.random(1,720)
 
else
self.x = self.x - self.speed
-- self.angle = self.angle + .1
-- self.y = self.amp * math.tan(self.angle) + self.initY
-- self.y = math.random(0,500)
end
 
 
end
 
 
 
 
    -- sceneGroup:insert ( background )
 
    -- the rest of your creations go here
 
 
end
 
function scene:show( event )
 
     local sceneGroup = self.view
if event.phase == "will" then
         -- put code here you want to happen just before the scene comes on the screen
-- physics.start()
Runtime:addEventListener("enterFrame", move)
Runtime:addEventListener("touch", touchScreen)
icebok.enterFrame = moveiceboks
Runtime:addEventListener("enterFrame",icebok)
icebok1.enterFrame = moveiceboks
Runtime:addEventListener("enterFrame",icebok1)
icebok2.enterFrame = moveiceboks
Runtime:addEventListener("enterFrame",icebok2)
icebok3.enterFrame = moveiceboks
Runtime:addEventListener("enterFrame",icebok3)
 
Runtime:addEventListener("collision", onCollision)
 
      else
         -- put code here you want to happen after the scene comes on the screen
-- physics.start()
-- Runtime:addEventListener("enterFrame", move)
Runtime:addEventListener("touch", touchScreen)
 
-- icebok.enterFrame = moveiceboks
-- Runtime:addEventListener("enterFrame",icebok)
 
-- icebok1.enterFrame = moveiceboks
-- Runtime:addEventListener("enterFrame",icebok1)
 
-- icebok2.enterFrame = moveiceboks
-- Runtime:addEventListener("enterFrame",icebok2)
 
-- icebok3.enterFrame = moveiceboks
-- Runtime:addEventListener("enterFrame",icebok3)
 
Runtime:addEventListener("collision", onCollision)
 
    end
 
end
 
function scene:hide( event )
    local sceneGroup = self.view
if event.phase == "will" then
         -- put code here you want to happen just before the scene leaves the screen
Runtime:removeEventListener("enterFrame", move)
Runtime:removeEventListener("touch", touchScreen)
 
-- icebok.enterFrame = moveiceboks
Runtime:removeEventListener("enterFrame",icebok)
 
-- icebok1.enterFrame = moveiceboks
Runtime:removeEventListener("enterFrame",icebok1)
 
-- icebok2.enterFrame = moveiceboks
Runtime:removeEventListener("enterFrame",icebok2)
 
-- icebok3.enterFrame = moveiceboks
Runtime:removeEventListener("enterFrame",icebok3)
 
Runtime:removeEventListener("collision", onCollision)
 
 
else
         -- put code here you want to happen after the scene has left the screen
 
-- Runtime:removeEventListener("enterFrame", move)
 
Runtime:removeEventListener("touch", touchScreen)
 
Runtime:removeEventListener("enterFrame",icebok)
 
Runtime:removeEventListener("enterFrame",icebok1)
 
Runtime:removeEventListener("enterFrame",icebok2)
 
Runtime:removeEventListener("enterFrame",icebok3)
 
Runtime:removeEventListener("collision", onCollision)
 
 
end
 
end
 
function scene:destroy( event )
     local sceneGroup = self.view
 
     -- put code here if you have things you need to remove that you created in create scene that does NOT go into the sceneGroup)
 
end
 
-- these must be the last 5 lines in the file
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
return scene
 
 
 
and the restart 
 
-----------------------------------------------------------------------------------------
--
-- main.lua
--
----------------------------------------------------------------------------
 
display.setStatusBar(display.HiddenStatusBar)
 
local _W = display.contentWidth
local _H = display.contentHeight
local scrollSpeed = 3
local composer = require( "composer" )
local scene = composer.newScene()
 
 
local function gotoGame()
    composer.gotoScene("game", { time=800, effect="crossFade" } )
print "game"
end
 
-- local function gotoHighScores()
    -- composer.gotoScene( "highscores" )
-- end
 
function scene:create( event )
 
    local sceneGroup = self.view
 
local bg1 = display.newImageRect( sceneGroup,"bg.png", 800, 1000)
bg1.x = _W*1/2; bg1.y = _H/2;
 
bg2 = display.newImage( sceneGroup, "ice2.png",140,210)
 
bg7 = display.newImage( sceneGroup, "ice1.png",140,420)
 
 
-- title = display.newImage( sceneGroup, "title.png", 160, 90)
 
local playButton = display.newText( sceneGroup, "Play", 160, 250, native.systemFont, 40 )
playButton:setFillColor( 0.23, 0.60, 1 )
 
-- local highScoresButton = display.newText( sceneGroup, "High Score", 160, 350, native.systemFont, 40 )
-- highScoresButton:setFillColor( 0.23, 0.60, 1 )
 
playButton:addEventListener("tap", gotoGame)
-- highScoresButton:addEventListener("tap", gotoHighScores)
 
composer.removeScene( "game" )   
end
 
 
 
 
 
function scene:show( event )
 
composer.removeScene( "game" )
-- local sceneGroup = self.view
 
-- local phase = event.phase
 
 
 
 
-- if ( phase == "will" ) then
 
-- Code here runs when the scene is still off screen (but is about to come on screen)
 
 
 
 
-- elseif ( phase == "did" ) then
 
-- Code here runs when the scene is entirely on screen
 
 
 
 
-- end
 
 
 
 
end
 
function scene:hide( event )
 
 
end
 
function scene:destroy( event )
  
end
 
-- these must be the last 5 lines in the file
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
return scene
 
 
 
sorry if i posted this wrong 

 



[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,509 posts
  • Corona SDK

  Best Answer

You aren't inserting any of your objects into sceneGroup, therefore they will not be automatically removed by Composer.

[TOPIC: post.html]
#3

Davy1222

[GLOBAL: userInfoPane.html]
Davy1222
  • Observer

  • 12 posts
  • Corona SDK

You aren't inserting any of your objects into sceneGroup, therefore they will not be automatically removed by Composer.

May i ask where? I attempted to  put everything in the scene group at the start of each function but that didnt change anything

 

EDIT: nevermind i got what you meant after staring at the fact that i did it in the restart but not the game, thx for your help youre a lifesaver

 

EDIT AGAIN: Now I'm getting another problem lol, im getting an error game.lua:74: attempt to call method 'applyForce' (a nil value) stack traceback: game.lua:74: in function '?' 

?: in function <?:190>



[TOPIC: post.html]
#4

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,509 posts
  • Corona SDK

Self is not passed to enterFrame. You'll need to specifically reference the object 'peng'. Be careful, though - if the player touches the screen before peng is created, you'll get a nil exception, so check it is not nil before accessing it.

 

I'd also recommend looking into lua scope. You are doing 'local bg1' and then everything after that appears to be global. You could run into problems with memory leaks (memory increasing and increasing when moving back and forth between scenes).



[TOPIC: post.html]
#5

Davy1222

[GLOBAL: userInfoPane.html]
Davy1222
  • Observer

  • 12 posts
  • Corona SDK

Self is not passed to enterFrame. You'll need to specifically reference the object 'peng'. Be careful, though - if the player touches the screen before peng is created, you'll get a nil exception, so check it is not nil before accessing it.

 

I'd also recommend looking into lua scope. You are doing 'local bg1' and then everything after that appears to be global. You could run into problems with memory leaks (memory increasing and increasing when moving back and forth between scenes).

well i added self to enter frame but it gives me the error before restarting



[TOPIC: post.html]
#6

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,509 posts
  • Corona SDK

No, I mean 'event' is the only parameter ever passed to an enterFrame listener. You can't use self, you must reference the object directly.



[TOPIC: post.html]
#7

Davy1222

[GLOBAL: userInfoPane.html]
Davy1222
  • Observer

  • 12 posts
  • Corona SDK

No, I mean 'event' is the only parameter ever passed to an enterFrame listener. You can't use self, you must reference the object directly.

well i changed all the selfs to peng and then i changed local function touchScreen(event) to local function touchScreen(peng,event) and now I get an error for the event (game.lua:77:attempt to index local 'event' (a nil value) stack traceback: game.lua:77: in function '?'   ?:in function <?:190>) 

do i need to change the event.phase to peng.phase in the 'if event.phase == "began" then'

 

also i appreciate you taking the time to respond, this is my first game so im probably making rookie mistakes :P



[TOPIC: post.html]
#8

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,509 posts
  • Corona SDK

You cannot pass anything but event to touchScreen because it is an enterFrame listener.

 

Because you made 'peng' a global variable (bad practise but we'll gloss over that for now), you can just reference 'peng' in touchScreen. This is assuming 'touchScreen' is always fired AFTER peng has been created.

 

I think the problem is you haven't worked through the basic tutorials and samples (and understood them....) to get a handle on the API, so these mistakes are inevitable.



[TOPIC: post.html]
#9

Davy1222

[GLOBAL: userInfoPane.html]
Davy1222
  • Observer

  • 12 posts
  • Corona SDK

You cannot pass anything but event to touchScreen because it is an enterFrame listener.

 

Because you made 'peng' a global variable (bad practise but we'll gloss over that for now), you can just reference 'peng' in touchScreen. This is assuming 'touchScreen' is always fired AFTER peng has been created.

 

I think the problem is you haven't worked through the basic tutorials and samples (and understood them....) to get a handle on the API, so these mistakes are inevitable.

i attempted to reference peng in function but i just get the same error for peng that i got for event

 

local function activatePengs(peng,event)
   peng:applyForce(0, -45, peng.x, peng.y)
end
 
 
local function touchScreen(event)
 
    peng = event.target
 
-- print("touch")
 
    if event.phase == "began" then
        peng.enterFrame = activatePengs
        Runtime:addEventListener("enterFrame", peng)
    end
 
    if event.phase == "ended" then
        Runtime:removeEventListener("enterFrame", peng)
 
    end
end


[TOPIC: post.html]
#10

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,509 posts
  • Corona SDK

Change this:

local function activatePengs (event)


You were still trying to pass peng to the listener. The first (only) parameter on an enterFrame listener is always event. If you call it peng, then now peng isn't your object anymore within that function, peng is event and doesn't have a function called applyForce.

Try this example:

local dogfood = "Winalot"
local catfood = "Whiskers"
local buyCatfood = function (catfood)
 print ("In function: "..catfood)
end
buyCatfood(dogfood)
print ("Outside function: "..catfood)


The above example shows you passing dogfood to the catfood function. Even if you call the parameter catfood in the function, it will still actually be acting on dogfood. You could call it geoff, it will still be dogfood.

Outside of the function, catfood will refer to catfood again.

This is the same as calling event 'peng' in the listener parameter list.

[TOPIC: post.html]
#11

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,509 posts
  • Corona SDK

You also need to remove peng = event.target from touchScreen.

Doing this overwrites peng with event.target.

[TOPIC: post.html]
#12

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,509 posts
  • Corona SDK

And the biggest piece of advice is still to slow down and work through tutorials. The error messages are there for a reason, they tell you exactly went wrong and where, but you need to know what is right before that makes any sense...

[TOPIC: post.html]
#13

Davy1222

[GLOBAL: userInfoPane.html]
Davy1222
  • Observer

  • 12 posts
  • Corona SDK

And the biggest piece of advice is still to slow down and work through tutorials. The error messages are there for a reason, they tell you exactly went wrong and where, but you need to know what is right before that makes any sense...

the confusing part is when i go from start.lua to game.lua, everything is fine but going from restart.lua to game.lua is what causes the error

and if i click play fast enough after the collision happens it works aagain

is it possible there's a problem in my restart.lua?




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: restart, collision