Page 1 of 1

Text doesn't get updated [SOLVED]

Posted: Mon May 27, 2024 3:23 pm
by simon_rle
Solution:

Instead of checking if the ball is below 0. I added a margin. 7 seemed to be the sweet spot.
Definitely not the best solution, but a solution nonetheless.

New to love2d

Making a pong game to get the feel for it. However cannot get my score updated even though I think I am doing everything right.
The initial score (0 0) gets on the screen but after a goal happens the score is not updated on the screen. I have also tried with removing dt in arguments in the scoreUpdate function.

Score = {}

function Score:load()
self.x = love.graphics.getWidth() / 2 - 100
self.y = love.graphics.getHeight() - love.graphics.getHeight() + 50

self.xPlayerScore = love.graphics.getWidth() / 2
self.yPlayerScore = love.graphics.getHeight() - love.graphics.getHeight() + 50
self.playerScore = 0

self.xAiScore = love.graphics.getWidth() / 2 + 40
self.yAiScore = love.graphics.getHeight() - love.graphics.getHeight() + 50
self.aiScore = 0


end

function Score:update(dt)
self:scoreUpdate(dt)


end

function Score:scoreUpdate(dt)
if Ball.x < 0 then
self.aiScore = self.aiScore + 1 * dt

end

if Ball.x + Ball.width > love.graphics.getWidth() then
self.playerScore = self.playerScore + 1 * dt

end
end

function Score:draw()
love.graphics.print("SCORE:", self.x, self.y)
love.graphics.print(tostring(self.playerScore), self.xPlayerScore, self.yPlayerScore)
love.graphics.print(tostring(self.aiScore), self.xAiScore, self.yAiScore)


end

Re: Text doesn't get updated [SOLVED]

Posted: Tue May 28, 2024 3:26 am
by zorg
Considering you didn't share more code, i'm going to assume the problem lies elsewhere that affects only the left side, otherwise you'd have said that that margin was required on the other side as well... probably something with checking the ball's position, and changing it if it goes below 0, which makes sense... but that also should change if it goes above the window width, who knows. :awesome:

A more elegant solution could be Ball.x + Ball.width < 0 and Ball.x > love.graphics.getWidth(), if you want to allow the ball actually going out of bounds.

Re: Text doesn't get updated [SOLVED]

Posted: Tue May 28, 2024 8:30 am
by simon_rle
I did have to add a margin to the right side too, forgot to add. I'll try your solution later today and update my original post for anyone who's facing the same issue