Page 1 of 1

Can't get out of a loop in a function?

Posted: Fri May 22, 2020 6:45 pm
by sololove2d
First post, please be gentle :P

I Wrote a function, that I WOULD LIKE it to do the following:

When player sprite runs to certain x, y.
Player sprite will not move for 1.5 second.
During this 1.5 sec, showing a turning coin.
After this 1.5, roll a random number, if greater than N, player sprite continue to move its original direction.

Code: Select all

function resolveTrap(dt, player)
  for i, t in ipairs(traps) do
    if distanceBetween(t.x + t.width/2, t.y + t.height/2, player.body:getX(), player.body:getY()) <= 25 and player.clear == false then
      trapDiff = t.difficulty
      local directionOrigin = player.direction
      player.direction = 0
      
      gameMode = 3
      rollingTimer = rollingTimer + dt
      if rollingTimer >= 1.5 then
        skillOut = math.random(0, player.skill01)
        if skillOut > t.difficulty then
          player.direction = directionOrigin
          player.clear = true
        elseif skillOut <= t.difficulty then
          player.direction = -directionOrigin
        end

      end
      gameMode = 2


    end
  end
end
In love.draw function:

Code: Select all

 
  if h01.direction == 0 then
    rollingNumber.animation:draw(sprites.coin_sheet, 100, 600, nil, nil, nil, 20.5, 21)
  end
  
In love.update function:

Code: Select all

  
  if rollingTimer > 2 then
    rollingTimer = 0
  end
  
My issue seems to be, after the number checking, the player sprite does not continue to move. I am not sure if I am closing the loop properly at this point. If I remove rollingTimer and such, it works fine, just no sprite pause, and continue to move right away.

Re: Can't get out of a loop in a function?

Posted: Fri May 22, 2020 8:47 pm
by sololove2d
After some debugging. It seems that the if statement check with the random number is not executing for some reason.

Code: Select all

        if skillOut > t.difficulty then
          player.direction = directionOrigin
          player.clear = true
        elseif skillOut <= t.difficulty then
          player.direction = -directionOrigin
        end
Could not find out where is the logic stops it.

Re: Can't get out of a loop in a function?

Posted: Sat May 23, 2020 2:13 am
by 4vZEROv
You should try to use a library like: https://github.com/vrld/hump/blob/master/timer.lua
It is made specialy to make it easy to do stuff like "after x time do that", "during x time do that" .

Code: Select all

      if rollingTimer >= 1.5 then
        skillOut = math.random(0, player.skill01)
        if skillOut > t.difficulty then
          player.direction = directionOrigin
          player.clear = true
        elseif skillOut <= t.difficulty then
          player.direction = -directionOrigin
        end
 
Here when rollingTimer is more than 1.5, the body of the if statement will be called every gameloop, so skillOut will be set every time.

Re: Can't get out of a loop in a function?

Posted: Sat May 23, 2020 12:20 pm
by sololove2d
Thanks lot. I will check out timer.lua.

When you say game loop, that means every dt frame?

Re: Can't get out of a loop in a function?

Posted: Sat May 23, 2020 3:17 pm
by 4vZEROv
Yes, you can see the mainloop/gameloop here :
https://love2d.org/wiki/love.run