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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
sololove2d
Prole
Posts: 7
Joined: Fri May 22, 2020 6:13 pm

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

Post by sololove2d » Fri May 22, 2020 6:45 pm

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.

sololove2d
Prole
Posts: 7
Joined: Fri May 22, 2020 6:13 pm

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

Post by sololove2d » Fri May 22, 2020 8:47 pm

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.

User avatar
4vZEROv
Citizen
Posts: 90
Joined: Wed Jan 02, 2019 8:44 pm

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

Post by 4vZEROv » Sat May 23, 2020 2:13 am

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.

sololove2d
Prole
Posts: 7
Joined: Fri May 22, 2020 6:13 pm

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

Post by sololove2d » Sat May 23, 2020 12:20 pm

Thanks lot. I will check out timer.lua.

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

User avatar
4vZEROv
Citizen
Posts: 90
Joined: Wed Jan 02, 2019 8:44 pm

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

Post by 4vZEROv » Sat May 23, 2020 3:17 pm

Yes, you can see the mainloop/gameloop here :
https://love2d.org/wiki/love.run

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 26 guests