Falling objects slow down

So i have been working on a game
And everything is ok
But when i play the game for sometime the random objects which fall from above the screen start falling at a very slow speed what kind of problem is this

Sounds like you’re running into FPS issues, which can be caused by a variety of things that accumulate over time. Without code, it’s difficult to help further.

[lua]--------------------------------------------------------------


local composer = require( “composer” )
local scene = composer.newScene()


local physics = require(“physics”)
physics.start()

local addTimer

function scene:create( event )
local sceneGroup = self.view
local background = display.newImage( “background.png” )
background.x = display.contentCenterX
background.y = display.contentCenterY

sceneGroup:insert( background )

–creating global variables
halfW = display.contentWidth*0.5
halfH = display.contentHeight*0.5

– Called when the scene’s view does not exist.

– INSERT code here to initialize the scene
– e.g. add display objects to ‘sceneGroup’, add touch listeners, etc.
end

function scene:show( event )
local sceneGroup = self.view
local phase = event.phase

if phase == “will” then
– Called when the scene is still off screen and is about to move on screen
elseif phase == “did” then

local pop_sound = audio.loadSound(“pop.mp3”)
local break_sound = audio.loadSound(“break.wav”)
local soundtrack = audio.loadStream(“motivation.wav”)

score = 0
scoreText = display.newText(score, halfW, 20,native.systemFontBold, 40 )
scoreText:setFillColor(255,255,255)
–creating touch event function
local function ballTouched(event)
if ( event.phase == “began”) then
Runtime:removeEventListener (“enterFrame”,event.self )
event.target:removeSelf()
score = score + 1
scoreText.text = score
audio.play(pop_sound)
end
end

local function endGame()
composer.gotoScene(“menu”)
end

–creating touch event function
local function eggTouched(event)
if (event.phase == “began”) then

physics.pause()
timer.performWithDelay(200,endGame)
Runtime:removeEventListener (“enterFrame”,event.self )
event.target:removeSelf()
score = math.floor(score * 0)
scoreText.text = score
audio.play(break_sound)

end
end

–creating offscreen function

local function offscreen(self,event)
if (self.y or self.x == nil) then
return
end
if(self.y or self .x > display.contentHeight + 50) then
Runtime:removeEventListener (“enterFrame”,event.self )
self:removeSelf()
end
end

local function addNewBallOrEgg()
local startX = math.random(display.contentWidth*0.2 , display.contentWidth*0.8)
if(math.random(1,2.7)==1) then

– EGG
local egg = display.newImage(“egg.png”, startX,-400)
physics.addBody(egg)
egg.enterFrame = offscreen
Runtime:addEventListener (“enterFrame”,egg )
egg:addEventListener(“touch”,eggTouched)

if score >= 150 then
egg.gravityScale = 2
end

if score >= 350 then
egg.gravityScale = 2.5
end

else
–ball

local ball = display.newImage(“ball.png”, startX,-400)
physics.addBody(ball)
ball.enterFrame = offscreen
Runtime:addEventListener (“enterFrame”,ball)
ball:addEventListener(“touch”,ballTouched)

if score >= 150 then
ball.gravityScale = 2
end

if score >= 350 then
ball.gravityScale = 2.5
end

return addNewBallOrEgg
end
end

–calling the function and adding the timer
addNewBallOrEgg()

addTimer = timer.performWithDelay( 300,addNewBallOrEgg,0)

audio.play(soundtrack,{loops = -1})

– Called when the scene is now on screen

– INSERT code here to make the scene come alive
– e.g. start timers, begin animation, play audio, etc.
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
physics.pause()

audio.stop()

timer.cancel(addTimer)
addTimer = nil
scoreText:removeSelf()

– INSERT code here to pause the scene
– e.g. stop timers, stop animation, unload sounds, etc.)
elseif phase == “did” then

composer.removeScene(“game”)
– Called whenscreen
end
end

function scene:destroy( event )
local sceneGroup = self.view

display.remove(egg)
egg = nil

display.remove(ball)
ball = nil

– Called prior to the removal of scene’s “view” (sceneGroup)

– INSERT code here to cleanup the scene
– e.g. remove display objects, remove touch listeners, save state, etc. the scene is now off
end


– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )


return scene [/lua]

Here is the code

When posting code, please click the blue <> button in the row with Bold and Italic and paste your code into the window that pops up.

It will help people help you.

Rob

So any solution ?

@coderm8, please edit your post and format your code. Very few people are going to read your code with it being unformatted.

If you want you can use 

[lua] paste your code in between the code tags [/lua] 

but using the <> code formatting button is easier.

Rob

Done

Now i really need a solution :disappointed_relieved:

@coderm8,

Hi.  I haven’t really read through the code too much, as I’m not a fan of big code posts.  Also, I find that one scene file is generally not the whole picture. i.e. The problem could be somewhere else entirely.

My guess is you’re not deleting all of your objects.  Eventually, there will be too many physics objects and the game slows down.

One way to check for this is to see if your memory usage grows and grows over time.  This indicates a memory leak.

  • Get SSK2 and copy the folder to your game: https://roaminggamer.github.io/RGDocs/pages/SSK2/

  • Add this code at the top of main.lua

    require “ssk2.loadSSK” _G.ssk.init() – START METERS ssk.meters.create_fps(true) ssk.meters.create_mem(true)

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/meters/

Now run the game and see how memory behaves as you play, and play, … till the slowdown.

Other than that, I’d have to see the whole project.  So, if it worth it to you and you are still stuck in a few days, consider trying the Hitman route: https://forums.coronalabs.com/topic/69420-hire-a-hitman-problem-solver-is-back/

Are you sure these lines are working?

Runtime:removeEventListener ("enterFrame",event.self )

Maybe try

Runtime:removeEventListener ("enterFrame",event.target )

Rob

It is still not working the objects fall really slowly after a period of time

Who are you responding to and what did you change?

[lua] Runtime:removeEventListener (“enterFrame”,event.self )[/lua]

[Lua] Runtime:removeEventListener (“enterFrame”,event.target ) [/lua]

I changed this

Sorry, but I think I’m going to drop out of this conversation.   (I have a limited time budget to provide free help and I’m afraid I have used it up for today.)

Please re-read my post above and consider a hit or alternately zipping up the whole project and sharing it here for input from the community at large.

I don’t even bother reading code that is so badly formatted… If you want help then put the effort in.  Suggest you spend time with the tutorials.

Writing this for others…

General pointers (reiterating good advice already given)… slow down over time normally means you are not removing objects and therefore Corona spends more and more time checking display objects for visibility. 

Other factors are multiple events - i.e. events being added but not removed will compound over time. 

Never ending timers are generally bad form and enterFrame() is much better, easier to read and easier to debug.

Loading audio in scene:show() is bad practise.

Hiding functions in scene events is bad practise.

Sounds like you’re running into FPS issues, which can be caused by a variety of things that accumulate over time. Without code, it’s difficult to help further.

[lua]--------------------------------------------------------------


local composer = require( “composer” )
local scene = composer.newScene()


local physics = require(“physics”)
physics.start()

local addTimer

function scene:create( event )
local sceneGroup = self.view
local background = display.newImage( “background.png” )
background.x = display.contentCenterX
background.y = display.contentCenterY

sceneGroup:insert( background )

–creating global variables
halfW = display.contentWidth*0.5
halfH = display.contentHeight*0.5

– Called when the scene’s view does not exist.

– INSERT code here to initialize the scene
– e.g. add display objects to ‘sceneGroup’, add touch listeners, etc.
end

function scene:show( event )
local sceneGroup = self.view
local phase = event.phase

if phase == “will” then
– Called when the scene is still off screen and is about to move on screen
elseif phase == “did” then

local pop_sound = audio.loadSound(“pop.mp3”)
local break_sound = audio.loadSound(“break.wav”)
local soundtrack = audio.loadStream(“motivation.wav”)

score = 0
scoreText = display.newText(score, halfW, 20,native.systemFontBold, 40 )
scoreText:setFillColor(255,255,255)
–creating touch event function
local function ballTouched(event)
if ( event.phase == “began”) then
Runtime:removeEventListener (“enterFrame”,event.self )
event.target:removeSelf()
score = score + 1
scoreText.text = score
audio.play(pop_sound)
end
end

local function endGame()
composer.gotoScene(“menu”)
end

–creating touch event function
local function eggTouched(event)
if (event.phase == “began”) then

physics.pause()
timer.performWithDelay(200,endGame)
Runtime:removeEventListener (“enterFrame”,event.self )
event.target:removeSelf()
score = math.floor(score * 0)
scoreText.text = score
audio.play(break_sound)

end
end

–creating offscreen function

local function offscreen(self,event)
if (self.y or self.x == nil) then
return
end
if(self.y or self .x > display.contentHeight + 50) then
Runtime:removeEventListener (“enterFrame”,event.self )
self:removeSelf()
end
end

local function addNewBallOrEgg()
local startX = math.random(display.contentWidth*0.2 , display.contentWidth*0.8)
if(math.random(1,2.7)==1) then

– EGG
local egg = display.newImage(“egg.png”, startX,-400)
physics.addBody(egg)
egg.enterFrame = offscreen
Runtime:addEventListener (“enterFrame”,egg )
egg:addEventListener(“touch”,eggTouched)

if score >= 150 then
egg.gravityScale = 2
end

if score >= 350 then
egg.gravityScale = 2.5
end

else
–ball

local ball = display.newImage(“ball.png”, startX,-400)
physics.addBody(ball)
ball.enterFrame = offscreen
Runtime:addEventListener (“enterFrame”,ball)
ball:addEventListener(“touch”,ballTouched)

if score >= 150 then
ball.gravityScale = 2
end

if score >= 350 then
ball.gravityScale = 2.5
end

return addNewBallOrEgg
end
end

–calling the function and adding the timer
addNewBallOrEgg()

addTimer = timer.performWithDelay( 300,addNewBallOrEgg,0)

audio.play(soundtrack,{loops = -1})

– Called when the scene is now on screen

– INSERT code here to make the scene come alive
– e.g. start timers, begin animation, play audio, etc.
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
physics.pause()

audio.stop()

timer.cancel(addTimer)
addTimer = nil
scoreText:removeSelf()

– INSERT code here to pause the scene
– e.g. stop timers, stop animation, unload sounds, etc.)
elseif phase == “did” then

composer.removeScene(“game”)
– Called whenscreen
end
end

function scene:destroy( event )
local sceneGroup = self.view

display.remove(egg)
egg = nil

display.remove(ball)
ball = nil

– Called prior to the removal of scene’s “view” (sceneGroup)

– INSERT code here to cleanup the scene
– e.g. remove display objects, remove touch listeners, save state, etc. the scene is now off
end


– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )


return scene [/lua]

Here is the code

When posting code, please click the blue <> button in the row with Bold and Italic and paste your code into the window that pops up.

It will help people help you.

Rob

So any solution ?

@coderm8, please edit your post and format your code. Very few people are going to read your code with it being unformatted.

If you want you can use 

[lua] paste your code in between the code tags [/lua] 

but using the <> code formatting button is easier.

Rob