Extreme lag and occasional moments of speed-ups

Also, I have a question relating to this, how would I remove all elements of a display group, I don’t want to remove the display group itself.

There is also an Attempt to getLinearVelocity error as well in line 181 in the getVelocities function

The errors are either in applyForce or getLinearVelocity in the getVelocities(), endJump(), or touchScreen functions. I tried putting if statements to check if character was nil but the errors still occurred.

I have tested the shown and hidden phases when the speed up occurs and the scene:show phase == “did”, where all my event listeners are occurs twice, the crash occurs when scene:hide phase == “will” occurs twice, this is where all event listeners are removed, it tries to remove an EventListener that has already been removed.

Shown = scene:show, phase == “did”

Hidden = scene:hide, phase == “will”

This screenshot shows what was printed in the logs.

https://www.youtube.com/watch?v=str0z-HFb90

In this video, pay close attention to the logs.

What does this instruction do? How do you move enemies?

moveEnemiesTimer = timer.performWithDelay(2, moveEnemies, -1) 

You don’t have to check (code below) both arguments obj1 and obj2 in removeOnPlayerHit. Just simply call removeOnPlayerHit(event.object1) or removeOnPlayerHit(event.object2)

if(obj1 ~= nil and obj1.id == "enemy") then   display.remove(obj1) end if(obj2 ~= nil and obj2.id == "enemy") then   display.remove(obj2) end

You can even rewrite removeOnPlayerHit function simpler like that

local function removeOnPlayerHit(obj)   if ( ( obj ~= nil ) and ( (obj.id == "enemy") or (obj.id == "enemy2") or (obj.id == "enemy3") or (obj.id == "enemy4") ) ) then     display.remove(obj)   end end

Solution find on http://stackoverflow.com/questions/22436782/corona-sdk-remove-all-objects-in-a-group

while middleGroup.numChildren \> 0 do   local child = middleGroup[1]   if child then child:removeSelf() end end

I notice on video in slow motion speed that about 7 sec on console you have twice times printed hidden and shown (hidden  hidden  shown shown in this order). This happens after you back from “try again” scene. Also on about 21 sec after you click “Try again” console print shown hidden and again shown. I think it is too many. I don’t why this happen. May be check code in “try again” scene.

what does word self refer to in endJump and touchScreen function? May be you mean character.x and character.y?

I have changed the self.x and self.y to character.x and character.y, also the move enemies timer calls the function moveEnemies(), and here is the code for try again scene:

local composer = require( "composer" ) local scene = composer.newScene() local widget = require "widget" widget.setTheme("widget\_theme\_ios7") -- ----------------------------------------------------------------------------------- -- Code outside of the scene event functions below will only be executed ONCE unless -- the scene is removed entirely (not recycled) via "composer.removeScene()" -- ----------------------------------------------------------------------------------- local background local button -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- local function resumeGame() composer.gotoScene("level1") end -- create() function scene:create( event ) local sceneGroup = self.view background = display.newRect(\_CX, \_CY, \_CW, \_CH) background:setFillColor(0) button = widget.newButton( { x = \_CX, y = \_CY, id = "button", label = "Try Again", onEvent = resumeGame } ) sceneGroup:insert(background) sceneGroup:insert(button) end -- show() function scene:show( event ) 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 -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene

Try this (in try again scene)

local function resumeGame(event) if ( "ended" == event.phase ) then composer.gotoScene("level1") end end

It seems resumeGame function is call twice. One with argument event.phase == began and second time with argument event.phase == ended. 

Alternatively replace onEvent with onRelease when create button.

Read about it https://docs.coronalabs.com/api/library/widget/newButton.html 

I have tried the game 7 times, and no crashes, lag, or speed-ups have occurred, I hope its safe to say that the problem has been resolve, shown and hidden are printing normally now, instead of multiple times and no stuttering occurs when transitioning between try again and level1, thank you very much.

I’m glad I could help:) Good luck :slight_smile: