Changing 'delay' in performWithDelay is not working. Or is the code wrong?

Guys i need help.

I want the “delay” to decrease when “5” crates collides with the grass so that it would be a lot faster to create more crates. But its not working. And its not either showing any errors, but it shows the text on the simulator “Decreasing delay at 0.5 second.” .

Here’s my code.



<code>

function scene:enterScene( event )

    local group = self.view

    

    physics.start()

    

    local NumOfCrates = display.newText( “0”, 0 , 0 , “Calibri”, 24 )

    NumOfCrates:setTextColor (255,255,255)

    NumOfCrates:setReferencePoint( display.TopRightReferencePoint )

    NumOfCrates.x = display.contentWidth - 30

    NumOfCrates.y = 20

    NumCrates = 0

        

–As the Crate collides with the grass, it counts how many crates collided.

    function CollisionCrate (self, event)

        if (event.phase == “began”) then

            NumCrates = NumCrates + 1

            NumOfCrates.text = NumCrates

        end

    end

        

    local GrassAp = display.newImage (“grass.png”)

            GrassAp.alpha = 1

            GrassAp.alpha=0

            transition.to(GrassAp,{alpha=1,time=500})

            GrassAp.y = 500

            GrassAp.rotation = 180

            physics.addBody (GrassAp, “static”,{ friction=0.5, bounce=0.3,isSensor = true })

    

            GrassAp.collision = CollisionCrate

            GrassAp:addEventListener (“collision”,GrassAp)

    

    local crates = 0

    

    local dTime = 1500

    

    local FallingCrates

        function spawnCrate(event)

            Crate1 = display.newImage (“crate.png”);

            Crate1.x = 70 + math.random(155)

            Crate1.y = -20

            Crate1.rotation = 1

            physics.addBody (Crate1, {density=0.3,bounce=0.5,friction=0.5})

            physics.setGravity (0,4)

–This is the code which I think is not working. But the " print (“Decreasing delay at 0.5 second.”) " can be seen in the simulator. But dTime is not changing at all.

                if NumCrates == 5 then

                    dTime = 500

                    print (“Decreasing delay at 0.5 second.”)

                end

        end

        

    FallingCrates    = timer.performWithDelay (dTime,spawnCrate,crates)

        

    print( “Game: enterScene event” )

            

end

<code>



Thanks in advance guys.

Why did you put the functions inside the enterScene event? Place them above the createScene event and put “local dTime = 1500” at the top of the document.

Also if you want to decrease the delay when 5 crates have been created then you should move the if statement at the top of the spawnCrate() function. At this moment there will be 6 crates spawned before the delay gets changed. You do not need to check this every time the function is called by the way

@CineTek … i did what you have said … i placed the spawnCrates functions on the createscene event. except for the functions for collisions because it caused some errors.

Placing the if statement at the top of the spawnCrate function wont work … the speed of creating the crates are still the same … but it atleast displays the  print (“Decreasing delay at 0.5 second.”) on the simulator … 

may you post the changed code, too? And please use the “<>” item in the interface to post your code :slight_smile:

What is the error for your collisions function? You need to place the functions properly to work otherwise Lua/Corona does not recognise them later.

local storyboard = require ("storyboard") local scene = storyboard.newScene() local physics = require ("physics") local dTime = 1000 local crates = 0 function scene:createScene (event) physics.start() local group = self.view local image = display.newImage( "hellbg.png" ) image.alpha=0 transition.to(image,{alpha=1,time=500}) local NumOfCrates = display.newText( "0", 0 , 0 , "Calibri", 24 ) NumOfCrates:setTextColor (255,255,255) NumOfCrates:setReferencePoint( display.TopRightReferencePoint ) NumOfCrates.x = display.contentWidth - 30 NumOfCrates.y = 20 NumCrates = 0 local GrassAp = display.newImage ("grass.png") GrassAp.alpha = 1 GrassAp.alpha=0 transition.to(GrassAp,{alpha=1,time=500}) GrassAp.y = 500 physics.addBody (GrassAp, "static",{ friction=0.5, bounce=0.3 }) if NumCrates == 3 then local dTime = 200 print ("Decreasing delay at 0.5 second.") end function spawnCrate(event) Crate1 = display.newImage ("crate.png"); Crate1.x = 70 + math.random(155) Crate1.y = -20 Crate1.rotation = 1 physics.addBody (Crate1, {density=0.3,bounce=0.5,friction=0.5}) physics.setGravity (0,4) return true end function CollisionCrate (self, event) if (event.phase == "began") then NumCrates = NumCrates + 1 NumOfCrates.text = NumCrates end end print ("Game: createScene event") end function scene:enterScene( event ) local group = self.view local GrassAp = display.newImage ("grass.png") GrassAp.alpha = 1 GrassAp.alpha=0 transition.to(GrassAp,{alpha=1,time=500}) GrassAp.y = 500 GrassAp.rotation = 180 physics.addBody (GrassAp, "static",{ friction=0.5, bounce=0.3,isSensor = true }) GrassAp.collision = CollisionCrate GrassAp:addEventListener ("collision",GrassAp) local FallingCrates FallingCrates = timer.performWithDelay (dTime,spawnCrate,crates) print( "Game: enterScene event" ) end

Here it is … i have fixed the collision function and placed it on create scene event … last problem is the dTimer … i just don’t know why it wont change the timer delay. The speed is still constant… I wanted to make the crates spawn so fast after 5 crates collided … did i make my code wrong again?

You need to put functions outside of any storyboard related events like createScene and enterScene.

Put a print statement like "print("dTime is: "…dTime) at the beginning of your spawnCrate() function.

Your code should look like this : 

... local dTime = 1000 local crates = 0 local numCrates = 0 local FallingCrates local function spawnCrates() print("dTime is: "..dTime) if NumCrates == 3 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dTime = 200 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print ("Decreasing delay at 0.5 second.") &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .... .... end local function CollisionCrate(self, event) .... end function scene:createScene(event) print("Game: createScene event") .... end function scene:enterScene(event) print("Game: enterScene event") ... FallingCrates = timer.performWithDelay(dTime, spawnCrate, crates) ... end

You need to fill in the rest of the code you are using to create the objects and remove the “…” from the code

@CineTek

The dTime is changing from “dTime is: 1000” to “dTime is: 200” on the simulator, but the speed of spawning the crate is not changing.

I copied the code from you and there was no error but the delay time of spawning the crate didn’t change at all. What seems to be the problem? … is it corona itself?

I am not sure if the timer is able to change it´s values while it is still active. You can try to create separate timers in your spawnCrates() function like this:

..... local function spawnCrates() print("dTime is: "..dTime) if NumCrates == 3 then dTime = 200 print ("Decreasing delay at 0.5 second.") end local newCrateTimer = timer.performWithDelay(dTime, spawnCrates) .... .... end ....

This should create a new timer each time a new crate gets created. You have to change your code from “FallingCrates = timer.performWithDelay(dTime, spawnCrate, crates)” to

“FallingCrates = timer.performWithDelay(dTime, spawnCrate, 1)”

Or if you need the crates variable in your FallingCrates timer, then you could use timer.cancel(FallingCrates) in your spawnCrates function right after you have set the if-statement.

The first solution should fix your problem at the moment though.

But please note, this is all theory-crafting! I did not test anything above for myself outside of my brain :stuck_out_tongue:

I’d love to put another newCrateTimer but how will i be able to pause and resume? It will create such error that the timer is expired

I wouldn’t use a timer for something like this:

[lua]

local dTime = 1500

local dTimer = system.getTimer()

local gameIsActive = false

local gameLoop = function ()  – put this below spawnCrates function

  if gameIsActive then

    local t = system.getTimer()

    if t - dTimer > dTime then

        dTimer = system.getTimer()

        spawnCrates()

    end

  end

end

gameIsActive = true – put this in enterScene

Runtime:addEventListener(“enterFrame”,gameLoop) – put this in enterScene

[/lua]

@nick_sherman

thankkkkkkk youuu so much!!! this is what i’ve been looking for!!! thank you infinity times!

Why did you put the functions inside the enterScene event? Place them above the createScene event and put “local dTime = 1500” at the top of the document.

Also if you want to decrease the delay when 5 crates have been created then you should move the if statement at the top of the spawnCrate() function. At this moment there will be 6 crates spawned before the delay gets changed. You do not need to check this every time the function is called by the way

@CineTek … i did what you have said … i placed the spawnCrates functions on the createscene event. except for the functions for collisions because it caused some errors.

Placing the if statement at the top of the spawnCrate function wont work … the speed of creating the crates are still the same … but it atleast displays the  print (“Decreasing delay at 0.5 second.”) on the simulator … 

may you post the changed code, too? And please use the “<>” item in the interface to post your code :slight_smile:

What is the error for your collisions function? You need to place the functions properly to work otherwise Lua/Corona does not recognise them later.

local storyboard = require ("storyboard") local scene = storyboard.newScene() local physics = require ("physics") local dTime = 1000 local crates = 0 function scene:createScene (event) physics.start() local group = self.view local image = display.newImage( "hellbg.png" ) image.alpha=0 transition.to(image,{alpha=1,time=500}) local NumOfCrates = display.newText( "0", 0 , 0 , "Calibri", 24 ) NumOfCrates:setTextColor (255,255,255) NumOfCrates:setReferencePoint( display.TopRightReferencePoint ) NumOfCrates.x = display.contentWidth - 30 NumOfCrates.y = 20 NumCrates = 0 local GrassAp = display.newImage ("grass.png") GrassAp.alpha = 1 GrassAp.alpha=0 transition.to(GrassAp,{alpha=1,time=500}) GrassAp.y = 500 physics.addBody (GrassAp, "static",{ friction=0.5, bounce=0.3 }) if NumCrates == 3 then local dTime = 200 print ("Decreasing delay at 0.5 second.") end function spawnCrate(event) Crate1 = display.newImage ("crate.png"); Crate1.x = 70 + math.random(155) Crate1.y = -20 Crate1.rotation = 1 physics.addBody (Crate1, {density=0.3,bounce=0.5,friction=0.5}) physics.setGravity (0,4) return true end function CollisionCrate (self, event) if (event.phase == "began") then NumCrates = NumCrates + 1 NumOfCrates.text = NumCrates end end print ("Game: createScene event") end function scene:enterScene( event ) local group = self.view local GrassAp = display.newImage ("grass.png") GrassAp.alpha = 1 GrassAp.alpha=0 transition.to(GrassAp,{alpha=1,time=500}) GrassAp.y = 500 GrassAp.rotation = 180 physics.addBody (GrassAp, "static",{ friction=0.5, bounce=0.3,isSensor = true }) GrassAp.collision = CollisionCrate GrassAp:addEventListener ("collision",GrassAp) local FallingCrates FallingCrates = timer.performWithDelay (dTime,spawnCrate,crates) print( "Game: enterScene event" ) end

Here it is … i have fixed the collision function and placed it on create scene event … last problem is the dTimer … i just don’t know why it wont change the timer delay. The speed is still constant… I wanted to make the crates spawn so fast after 5 crates collided … did i make my code wrong again?

You need to put functions outside of any storyboard related events like createScene and enterScene.

Put a print statement like "print("dTime is: "…dTime) at the beginning of your spawnCrate() function.

Your code should look like this : 

... local dTime = 1000 local crates = 0 local numCrates = 0 local FallingCrates local function spawnCrates() print("dTime is: "..dTime) if NumCrates == 3 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dTime = 200 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print ("Decreasing delay at 0.5 second.") &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .... .... end local function CollisionCrate(self, event) .... end function scene:createScene(event) print("Game: createScene event") .... end function scene:enterScene(event) print("Game: enterScene event") ... FallingCrates = timer.performWithDelay(dTime, spawnCrate, crates) ... end

You need to fill in the rest of the code you are using to create the objects and remove the “…” from the code

@CineTek

The dTime is changing from “dTime is: 1000” to “dTime is: 200” on the simulator, but the speed of spawning the crate is not changing.

I copied the code from you and there was no error but the delay time of spawning the crate didn’t change at all. What seems to be the problem? … is it corona itself?

I am not sure if the timer is able to change it´s values while it is still active. You can try to create separate timers in your spawnCrates() function like this:

..... local function spawnCrates() print("dTime is: "..dTime) if NumCrates == 3 then dTime = 200 print ("Decreasing delay at 0.5 second.") end local newCrateTimer = timer.performWithDelay(dTime, spawnCrates) .... .... end ....

This should create a new timer each time a new crate gets created. You have to change your code from “FallingCrates = timer.performWithDelay(dTime, spawnCrate, crates)” to

“FallingCrates = timer.performWithDelay(dTime, spawnCrate, 1)”

Or if you need the crates variable in your FallingCrates timer, then you could use timer.cancel(FallingCrates) in your spawnCrates function right after you have set the if-statement.

The first solution should fix your problem at the moment though.

But please note, this is all theory-crafting! I did not test anything above for myself outside of my brain :stuck_out_tongue:

I’d love to put another newCrateTimer but how will i be able to pause and resume? It will create such error that the timer is expired

I wouldn’t use a timer for something like this:

[lua]

local dTime = 1500

local dTimer = system.getTimer()

local gameIsActive = false

local gameLoop = function ()  – put this below spawnCrates function

  if gameIsActive then

    local t = system.getTimer()

    if t - dTimer > dTime then

        dTimer = system.getTimer()

        spawnCrates()

    end

  end

end

gameIsActive = true – put this in enterScene

Runtime:addEventListener(“enterFrame”,gameLoop) – put this in enterScene

[/lua]