Game freezes while pressing buttons ( video explanation included )

Hi community,

I have recently built my app and now when i run it, the game runs fine but as i keep tapping buttons, the game begins to stick and lag… 

Any help?

Below shows a video of what happens

https://www.youtube.com/watch?v=xVrZt_n6XEw

Around the 25 second mark, you can see the game starting to lag,

Any Help will be greatly appreciated!

Darren.

  1. What is your button code doing (better to show us by pasting your touch listener into a code block here, rather than just tell.)  You may be over-looping or doing something expensive?

  2. Have you checked for leaks?  Perhaps you are not removing objects as needed?

  3. Have you profiled your code when clicking the button?

PS - Pretty looking game so far.

The video is helpful, but we need to see some code to know what’s going on. This could be a memory leak where you keep creating objects over and over and never deleting them. It could be a case of increasing enterFrame listeners (game loops) that are taking more and more CPU. We don’t know how you’re moving your objects or creating them. Please don’t dump your whole project on us, but we will need to see your creation code and movement code.

When you post it, please do a copy/paste from your editor and click on the blue <> button (with Bold/Italic) and paste the code in the box that pops up.

Thanks

Rob

First of all, thank you for your responses.

Below shows an example function for the blue button showed in the video. There are 4 button functions each with their respective color and is identical in operation except the colors are different. ( Below shows blueButt for blue button, but there are also yellowButt, greenButt and redButt)

local blueButt function blueButt() basketsIdx = 3 basket:removeSelf() basket = display.newImage("Blue Basket.png") basket.x = centerX basket.y = display.contentHeight + 25 basket.xScale = 4 physics.addBody(basket,"static") basket.type = "blue" basket.collision = basketCollision yellowButton:addEventListener("tap", yellowButt) redButton:addEventListener("tap", redButt) greenButton:addEventListener("tap", greenButt) basket:addEventListener("collision", basket) end

Here shows the collision function called;

function basketCollision(self,event) if (event.phase == "began") then if (event.target.type == event.other.type) then event.other:removeSelf() audio.play(scoreIncreaseAudio) score = score + 1 if (score \> 5) then physics.setGravity(0,6.0) elseif (score \> 10) then physics.setGravity(0,8.0) elseif (score \> 15 ) then physics.setGravity(0,10.0) elseif( score \> 25)then physics.setGravity(0,12.0) end displayScore:removeSelf() displayScore = display.newText(score, 0, 0,"Chintzy CPU Shadow BRK", 50 ) displayScore.x = centerX displayScore.y = 10 elseif (event.target.type ~= event.other.type) then physics.setGravity(0,15) event.target:removeSelf() timer.cancel(dropsTimer) youLose() end end end

Any help will be appreciated 

Thank you again so much for taking the time to reply to my original message!

Darren.

You seem to be completely ignoring phases in your button listener - AND - doing physics work in it.

Use the phases…

local function blueButt( event ) if( event.phase == "began" ) then -- or use "ended" if that makes sense. -- do work end end

I have added in phases across all buttons such that and it still sticks after the button is clicked a couple times 

function blueButt( event ) if( event.phase == "began" ) then -- same code as previous blueButt function end end

Sorry, that’s about the limit of my suggestions.  Sounds like you need to get someone with more experience to look at your whole game.

Additional Notes: 

  • It looks like you’re using tap listeners and not touch listeners, in which case there are no phases.  
  • I don’t personally use tap listeners because I want more control than tap provides.  So, sorry if that was confusing.

My feeling is there are a number of minor mistakes accumulating into a major issue for your game.  Have you measured frame rate over time?  How about memory usage?  (I don’t mean on your PC either, but by adding code to your app to show these values.)

This answer (to another forum post) has a basic module in it for showing FPS and memory usage: 

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/05/leaktest.zip

I’m guessing that as your game runs, the memory usage will climb in an unbounded fashion and FPS will fall.  If so, you’ve got some kind of memory leak or runaway processes.

Are you using transition for the circles ? Are the circles generating or do you recicle them ?

I’d rather use ‘enterFrame’ insteade of transition. Also If you’re creating the objects infinitly, then that’s the problem. I had this issue once. 

good luck.

Couple of things I noticed in your blueButt function. Why are  you re-adding tap listeners every time you tap a button? You only need to add the tap listeners once per button when you create the button. As it is each tap is probably calling more and more functions each time. You also need to do a

return true

at the end of the tap handler so that it stops propagating the event.

Next I’m not sure why you’re destroying the basket each time either. There are much better ways to do it:

  1. :setFillColor()  – lets you color the basket. One object. No re-creating.

  2. Create all four once, stack them on top of each other and use .isVisible to toggle which one is scene. Might be a bit problematic with the physics. If you’re only using physics for the collision aspect, see: https://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

You don’t need to deal with physics at all if you’re moving your objects by transitions or translates or enterFrame movement.

  1. Use a graphics.newImageSheet() that has your four images in it, and then make a sprite where each color is a sprite frame that can be played. See: https://coronalabs.com/blog/2013/11/26/tutorial-techniques-for-swapping-images/

Rob

  1. What is your button code doing (better to show us by pasting your touch listener into a code block here, rather than just tell.)  You may be over-looping or doing something expensive?

  2. Have you checked for leaks?  Perhaps you are not removing objects as needed?

  3. Have you profiled your code when clicking the button?

PS - Pretty looking game so far.

The video is helpful, but we need to see some code to know what’s going on. This could be a memory leak where you keep creating objects over and over and never deleting them. It could be a case of increasing enterFrame listeners (game loops) that are taking more and more CPU. We don’t know how you’re moving your objects or creating them. Please don’t dump your whole project on us, but we will need to see your creation code and movement code.

When you post it, please do a copy/paste from your editor and click on the blue <> button (with Bold/Italic) and paste the code in the box that pops up.

Thanks

Rob

First of all, thank you for your responses.

Below shows an example function for the blue button showed in the video. There are 4 button functions each with their respective color and is identical in operation except the colors are different. ( Below shows blueButt for blue button, but there are also yellowButt, greenButt and redButt)

local blueButt function blueButt() basketsIdx = 3 basket:removeSelf() basket = display.newImage("Blue Basket.png") basket.x = centerX basket.y = display.contentHeight + 25 basket.xScale = 4 physics.addBody(basket,"static") basket.type = "blue" basket.collision = basketCollision yellowButton:addEventListener("tap", yellowButt) redButton:addEventListener("tap", redButt) greenButton:addEventListener("tap", greenButt) basket:addEventListener("collision", basket) end

Here shows the collision function called;

function basketCollision(self,event) if (event.phase == "began") then if (event.target.type == event.other.type) then event.other:removeSelf() audio.play(scoreIncreaseAudio) score = score + 1 if (score \> 5) then physics.setGravity(0,6.0) elseif (score \> 10) then physics.setGravity(0,8.0) elseif (score \> 15 ) then physics.setGravity(0,10.0) elseif( score \> 25)then physics.setGravity(0,12.0) end displayScore:removeSelf() displayScore = display.newText(score, 0, 0,"Chintzy CPU Shadow BRK", 50 ) displayScore.x = centerX displayScore.y = 10 elseif (event.target.type ~= event.other.type) then physics.setGravity(0,15) event.target:removeSelf() timer.cancel(dropsTimer) youLose() end end end

Any help will be appreciated 

Thank you again so much for taking the time to reply to my original message!

Darren.

You seem to be completely ignoring phases in your button listener - AND - doing physics work in it.

Use the phases…

local function blueButt( event ) if( event.phase == "began" ) then -- or use "ended" if that makes sense. -- do work end end

I have added in phases across all buttons such that and it still sticks after the button is clicked a couple times 

function blueButt( event ) if( event.phase == "began" ) then -- same code as previous blueButt function end end

Sorry, that’s about the limit of my suggestions.  Sounds like you need to get someone with more experience to look at your whole game.

Additional Notes: 

  • It looks like you’re using tap listeners and not touch listeners, in which case there are no phases.  
  • I don’t personally use tap listeners because I want more control than tap provides.  So, sorry if that was confusing.

My feeling is there are a number of minor mistakes accumulating into a major issue for your game.  Have you measured frame rate over time?  How about memory usage?  (I don’t mean on your PC either, but by adding code to your app to show these values.)

This answer (to another forum post) has a basic module in it for showing FPS and memory usage: 

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/05/leaktest.zip

I’m guessing that as your game runs, the memory usage will climb in an unbounded fashion and FPS will fall.  If so, you’ve got some kind of memory leak or runaway processes.

Are you using transition for the circles ? Are the circles generating or do you recicle them ?

I’d rather use ‘enterFrame’ insteade of transition. Also If you’re creating the objects infinitly, then that’s the problem. I had this issue once. 

good luck.

Couple of things I noticed in your blueButt function. Why are  you re-adding tap listeners every time you tap a button? You only need to add the tap listeners once per button when you create the button. As it is each tap is probably calling more and more functions each time. You also need to do a

return true

at the end of the tap handler so that it stops propagating the event.

Next I’m not sure why you’re destroying the basket each time either. There are much better ways to do it:

  1. :setFillColor()  – lets you color the basket. One object. No re-creating.

  2. Create all four once, stack them on top of each other and use .isVisible to toggle which one is scene. Might be a bit problematic with the physics. If you’re only using physics for the collision aspect, see: https://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

You don’t need to deal with physics at all if you’re moving your objects by transitions or translates or enterFrame movement.

  1. Use a graphics.newImageSheet() that has your four images in it, and then make a sprite where each color is a sprite frame that can be played. See: https://coronalabs.com/blog/2013/11/26/tutorial-techniques-for-swapping-images/

Rob