Page 1 of 1

Function call in love.update seems to only happen once

Posted: Tue Mar 03, 2020 9:22 am
by oasl
I'm working on a Tetris clone for practice, and I've run into a problem defining the floor so that the pieces don't fall off the screen. I've got two classes, Pieces and Blocks. Pieces are made up of Blocks, and store the coordinates of each block in a nested table:

Code: Select all

    -- stores upper left corner coordinates for each block to use in collision detection
    self.coordinates = {{x = self.blockA.x, y = self.blockA.y}, {x = self.blockB.x, y = self.blockB.y},
                        {x = self.blockC.x, y = self.blockC.y}, {x = self.blockD.x, y = self.blockD.y}}
I've written a function called Piece:collides() that right now only checks the y coordinate of each block and sets the piece's dy to 0. In the current code, I've made the y coordinate to hit pretty small in order to eliminate the possibility that it's stopping after it falls off the screen:

Code: Select all

function Piece:collides()
    for _, v in pairs(self.coordinates) do
        if v.y >= 50 then
            return true
        end
    end

    return false
end
One of the other things I've checked with this function is reversing the operator to v.y <= 50 to check that it could trigger at all. When I do this, the piece never moves, as its starting position is lower than 50.

Finally, here is my love.update() function in main, where it's called:

Code: Select all

function love.update(dt)

    if gameState == 'play' then
        piece.dy = GRAVITY

        if love.keyboard.isDown('left') then
            piece.dx = -25
        elseif love.keyboard.isDown('right') then
            piece.dx = 25
        else
            piece.dx = 0
        end

        if love.keyboard.isDown('down') then
            piece:drop()
        else
            piece.dy = GRAVITY
        end
        
        if piece:collides() then
            piece.dy = 0
        end

        piece:update(piece.dx, piece.dy, dt)
    end

end
Everything else in this function works as expected. Piece:drop() is probably the closest related to Piece:collides(), as it's also changing the dy:

Code: Select all

function Piece:drop()
    self.dy = GRAVITY * 5
end
The other test I tried was printing the y coordinates to the screen as it ran to make sure the piece was where I expected it to be. It went above 50 without ever stopping.

I'm really not sure where to go from here. As far as I can tell it seems to be only calling Piece:collides() once when gameState turns to 'play', but I see no reason why that should be happening.

Please let me know if there's any other code I should post.

Re: Function call in love.update seems to only happen once

Posted: Sat Mar 21, 2020 7:45 am
by bobbyjones
Do you have the complete .love file available? I will be able to help if you can provide that.