scene change isnt working properly

so when i add this piece of code into the scene create it works perfect but when i put it into the scene show it lags and crashes corona…

 local i = 5 local circle = {} local spawnCircles = function () local FallDown = math.random(display.contentWidth \* 0.2, display.contentWidth \* 0.8 ) circle[i] = display.newImageRect("circle.png", 31, 31 ) circle[i].x = FallDown circle[i].y = -70 physics.addBody( circle[i], "dynamic", {density = .01, friction = 0.5, bounce = 0.1 }) circle[i].gravityScale = 0.1 physics.setGravity( 0, 10 ) circle[i]:addEventListener( "collision", circle) i = i + 1 if i == 10 then -- i = 10 means 10 circles will be created in the circleTable i = 5 end end myTimer = timer.performWithDelay( 2000, spawnCircles, -1) --1000 is the time 

this is the game.lua code…

local composer = require( "composer" ) local scene = composer.newScene() local \_W = display.contentWidth local \_H = display.contentHeight local physics = require("physics" ) physics.setScale( 30 ) physics.setGravity( 0, 0 ) physics.start( ) physics.setDrawMode( "hybrid" ) function scene:create( event ) local sceneGroup = self.view physics.start() ---------------------------- local BackGround = display.newImageRect("BackGround.png", 1080, 1920) return true end ---------------------------- ---------------------------- function scene:show( event ) local sceneGroup = self.view physics.start() ----------------------------- ----------------------------- local i = 5 local circle = {} local spawnCircles = function () local FallDown = math.random(display.contentWidth \* 0.2, display.contentWidth \* 0.8 ) circle[i] = display.newImageRect("circle.png", 31, 31 ) circle[i].x = FallDown circle[i].y = -70 physics.addBody( circle[i], "dynamic", {density = .01, friction = 0.5, bounce = 0.1 }) circle[i].gravityScale = 0.1 physics.setGravity( 0, 10 ) circle[i]:addEventListener( "collision", circle) i = i + 1 if i == 10 then -- i = 10 means 10 circles will be created in the circleTable i = 5 end end myTimer = timer.performWithDelay( 2000, spawnCircles, -1) --1000 is the time --------------- return scene end -- this end ends the scene:show -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

this game.lua crashes corona … can you tell me what im doing wrong? thanks

can anyone help?! please

Are you getting any error messages?  Any additional message in your console log?

Rob

Thats the weird/funny part… im not getting anything at all… no errors… it just starts working and in 2 seconds it starts spawning millions of balls and lags out corona… 

and its only supposed to create 10 balls in a circle table…

@roaminggamer

Hey I was looking through the forums and you seem to really know what your doing so I was wonder if you could help me out? Thanks 

Hi.  I read through the code and there are a number of issues (not putting you down here OK, just listing them)

  1. Thanks for posting the code in a code block but it needed a little more cleaning to make it legible.  However, I grabbed it and fixed it.
  2. You don’t want to use an endless timer (see my variation below).  They seem like a good idea, but often lead to issues if you make a small mistake.  Better to call timer again from the function.
  3. You ignored the fact that show() is called twice (once for phase == will, again for phase == did).
  4. You almost never want to define a function body inside another function body, especially not in show() or create() ,etc.  When using composer declare the functions outside the scene functions.  If you must declare them at the top of your file and define the below, or just define them before the scene functions (see my sample below).
  5. Others… can’t remember now.

Anyways, here is a working example ( I changed some code since I don’t have all of yours and don’t have your art assets):

local composer = require( "composer" ) local physics = require("physics" ) local scene = composer.newScene() local \_W = display.contentWidth local \_H = display.contentHeight local cw = display.contentWidth \* 0.2 local ch = display.contentWidth \* 0.8 local function onCollision( self, event ) return true end local maxCircles = 5 local circles = {} local function spawnCircles( group ) if( #circles \>= maxCircles) then return end --local circle = display.newImageRect( group, "circles.png", 31, 31 ) local circle = display.newCircle( group, 31, 31, 10 ) circles[#circles+1] = circle circle.x = math.random( cw, ch ) circle.y = -70 physics.addBody( circle, "dynamic", { density = .01, friction = 0.5, bounce = 0.1 }) circle.gravityScale = 0.1 timer.performWithDelay( 2000, function() spawnCircles( group ) end ) circle.collision = onCollision circle:addEventListener( "collision" ) end function scene:create( event ) local sceneGroup = self.view physics.setScale( 30 ) physics.setGravity( 0, 10 ) physics.start( ) physics.setDrawMode( "hybrid" ) end function scene:show( event ) local sceneGroup = self.view local willDid = event.phase if( willDid == "will" ) then elseif( willDid == "did" ) then timer.performWithDelay( 2000, function() spawnCircles( sceneGroup ) end ) end end -- ------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

I’m not at home ATM but will be in a couple hours… Could you get the code in order? Lol it not in that code.box thing… And so you rewrote my code and if I put in into a game .lua it should work like me first code?

Already fixed, I post code, read the post, tweak it, re-save… etc.

Sometimes while doing this, the forms code formatter freaks out.  I have fixed it.

Please note, I encourage you and all readers to do the same.  Post your questions/replies, read them, see a problem in formatting, spelling, etc. fix it.  

The higher the quality of posts and responses the better they stand the test of time and stay useful to other readers later.

That is why they let us re-edit posts after we submit them.  Nobody gets it perfect on the first try.

Especially me.  I am very fumble fingered.

Oh thanks!! I have another question… Is it possible to do like

Local function images()

–add images in here and then

Return true

End

Is that possible or not? Just a question popped into my head and I can’t test it atm…
Pretty much put all my images in a function…

I don’t understand the question.  That looks like a basic function body, so my answer should be, “Yes”, but what do you mean:

–add images in here and then 

I’m almost home so I’ll get back to you on an answer

Ok so i think i know why the code isnt working… check this video i made out… make sure you play in in full screen in youtube or you wont see anything…

https://youtu.be/7ks_17H3SQ8

You have to test for the will and did phases.  The scene:show() function will get called twice and if you don’t put your code inside an if statement testing for the phase, you will get things duplicated just like you are.

In your scene:show please do:

function scene:show( event ) if event.phase == "did" then           -- put your code here     end end

Rob

Thank you so much ! finnaly ! haha i was pretty stuck… i guess i need to learn how to use composer better haha. thanks again

can anyone help?! please

Are you getting any error messages?  Any additional message in your console log?

Rob

Thats the weird/funny part… im not getting anything at all… no errors… it just starts working and in 2 seconds it starts spawning millions of balls and lags out corona… 

and its only supposed to create 10 balls in a circle table…

@roaminggamer

Hey I was looking through the forums and you seem to really know what your doing so I was wonder if you could help me out? Thanks