Page 1 of 1

Next line not displaying on click

Posted: Tue Apr 18, 2023 1:21 am
by fictional_bookworm
this is my code, I am trying to program it so that it goes to next line on click (or display remaning characters if the line has more characters to print, but it is not doing so and I am not sure why it isn't.

Code: Select all


local dialoguePage = {}
local currentScreen = startScreen
local requestPage = require("requestPage")

textToDisplay = {
    "line1",
    "line2",
    "line3",
    "line4"
    }
currentLine = 1
numberOfCharsToDisplay = 0
timePassed = 0
timeForEachChar = 0.25

function dialoguePage.load()
end

function dialoguePage.update(dt)
    timePassed = timePassed + dt
    if timePassed >= timeForEachChar then
         timePassed = timePassed - timeForEachChar
         numberOfCharsToDisplay = numberOfCharsToDisplay + 1
         
    end
    love.mousepressed(x, y, button)

end

function dialoguePage.draw()
    love.graphics.print(textToDisplay[currentLine]:sub(1, numberOfCharsToDisplay), 0, 0)
end

function dialoguePage.mousepressed(x, y, button)
    if button == 1 then
        if numberOfCharsToDisplay >= #textToDisplay[currentLine] then
            -- Move to the next line of text
            currentLine = currentLine + 1
            if currentLine > #textToDisplay then
                -- If we've reached the end of the text, move to the next screen
                currentScreen = requestPage
            else
                -- Otherwise, reset the number of characters to display
                numberOfCharsToDisplay = 0
            end
        else
            -- If the current line is not fully displayed, show the full line and move to the next line
            numberOfCharsToDisplay = #textToDisplay[currentLine]
            currentLine = currentLine + 1
            if currentLine > #textToDisplay then
                -- If we've reached the end of the text, move to the next screen
                currentScreen = requestPage
            end
        end
    end
end

return dialoguePage

Re: Next line not displaying on click

Posted: Tue Apr 18, 2023 4:58 am
by knorke
Hello and welcome.
This was a bit tricky to figure out because you did not explain how exactly you wanted it to work.
At first I thought the text should not only get separated into lines but the lines should also be displayed as split parts, if they are too long. (for example to fit into the width of a message box)
But actually it was a typing animation thing.

I added functions love.draw/update/mousepressed functions that call your functions, so I could actually run&test.

Something like this works, not 100% sure if that is what you want:
The text gets "typed" and if you click it skips the typing animation and instantly shows the full line.

Code: Select all

local dialoguePage = {}
local currentScreen = startScreen
--local requestPage = require("requestPage")

textToDisplay = {
    "all of the kings horses and all the kings men",
    "they could not put humpy-dumpy together again.",
    "that is a true story.",
    "yes, yes. a very true story.",
    "the story continues. we need more lines to test",
    "this is the story about testing."
    }
currentLine = 1
numberOfCharsToDisplay = 0
timePassed = 0
timeForEachChar = 0.1

function dialoguePage.load()
end

function dialoguePage.update(dt)
    timePassed = timePassed + dt
    if timePassed >= timeForEachChar then
         timePassed = timePassed - timeForEachChar
         numberOfCharsToDisplay = numberOfCharsToDisplay + 1
     end
end

function love.draw ()
	dialoguePage.draw()
end

function love.mousepressed( x, y, button, istouch, presses )
	dialoguePage.mousepressed(x, y, button)
end

function love.update(dt)
	dialoguePage.update(dt)
end

function dialoguePage.draw()
  love.graphics.print (textToDisplay[currentLine],10,100)
    love.graphics.print ("numberOfCharsToDisplay:"..numberOfCharsToDisplay,10,120)

   love.graphics.print(textToDisplay[currentLine]:sub(1, numberOfCharsToDisplay), 0, 0)
end



function dialoguePage.mousepressed(x, y, button)
    if button == 1 then
        if numberOfCharsToDisplay >= #textToDisplay[currentLine] then
            -- Move to the next line of text
            currentLine = currentLine + 1
            if currentLine > #textToDisplay then
                -- If we've reached the end of the text, move to the next screen
                currentScreen = requestPage
            else
                -- Otherwise, reset the number of characters to display
                numberOfCharsToDisplay = 0
            end
        else
            -- If the current line is not fully displayed, show the full line and DO NOT move to the next line
            numberOfCharsToDisplay = #textToDisplay[currentLine]
            --currentLine = currentLine + 1 --NO!
            if currentLine > #textToDisplay then
                -- If we've reached the end of the text, move to the next screen
                currentScreen = requestPage
            end
        end
    end
end

return dialoguePage

The relevant fix is this:

Code: Select all

 -- If the current line is not fully displayed, 
 --show the full line and DO NOT move to the next line
 numberOfCharsToDisplay = #textToDisplay[currentLine]
--currentLine = currentLine + 1 --NO!
When skipping the text-animation you were also advancing to the next line. So I commented out that line.