user14273546
user14273546

Reputation:

Why won't my text in a Love2D game update?

I finally fixed the problem regarding the text not showing up in my game, now the next problem was it won't update. What was supposed to happen is that when I press the up and down arrow keys, the selected text would change, then pressing Enter would bring me to a new state, or quitting the game.

The code in my StartState.lua now looks like this:

StartState = Class{__includes = BaseState}

local option = 1

function StartState:update(dt)
    if love.keyboard.wasPressed('up') then
        if option == 1 then
            option = 2
        else
            option = 1
        end
    elseif love.keyboard.wasPressed('down') then
        if option == 2 then
            option = 1
        else
            option = 2
        end
    end

    if love.keyboard.wasPressed('enter') then
        if option == 1 then
            gStateMachine:change('play')
        else
            gStateMachine:change('quit')
        end
    end
end

function StartState:render()
    local backgroundWidth = gTextures['start-background']:getWidth()
    local backgroundHeight = gTextures['start-background']:getHeight()

    love.graphics.draw(gTextures['start-background'], 0, 0, 0, VIRTUAL_WIDTH / (backgroundWidth - 1), 
        VIRTUAL_HEIGHT / (backgroundHeight - 1))

    love.graphics.setFont(gFonts['large'])
    love.graphics.setColor(153/255, 217/255, 234/255, 255/255)
    love.graphics.printf('Attack Them All', 0, VIRTUAL_HEIGHT / 2 - 50, VIRTUAL_WIDTH, 'center')

    love.graphics.setFont(gFonts['medium'])
    love.graphics.setColor(0/255, 0/255, 0/255, 255/255)
    love.graphics.printf('Play', 0, (VIRTUAL_HEIGHT / 2) + 2, VIRTUAL_WIDTH + 2, 'center')
    love.graphics.printf('Quit', 0, (VIRTUAL_HEIGHT / 2) + 22, VIRTUAL_WIDTH + 2, 'center')

    if option == 1 then
        love.graphics.setColor(255/255, 174/255, 201/255, 255/255)
        love.graphics.printf('Play', 0, (VIRTUAL_HEIGHT / 2), VIRTUAL_WIDTH, 'center')
        love.graphics.setColor(237/255, 28/255, 36/255, 255/255)
        love.graphics.printf('Quit', 0, (VIRTUAL_HEIGHT / 2) + 20, VIRTUAL_WIDTH, 'center')
    else
        love.graphics.setColor(237/255, 28/255, 36/255, 255/255)
        love.graphics.printf('Play', 0, (VIRTUAL_HEIGHT / 2), VIRTUAL_WIDTH, 'center')
        love.graphics.setColor(255/255, 174/255, 201/255, 255/255)
        love.graphics.printf('Quit', 0, (VIRTUAL_HEIGHT / 2) + 20, VIRTUAL_WIDTH, 'center')
    end

    love.graphics.setColor(255/255, 255/255, 255/255, 255/255)
end

I already added up a StateMachine on my main.lua file:

function love.load()
    love.graphics.setDefaultFilter('nearest', 'nearest')

    math.randomseed(os.time())

    love.window.setTitle('Attack Them All')
    
    push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
        vsync = true,
        fullscreen = false,
        resizable = true
    })
    
    gStateMachine = StateMachine {
        ['start'] = function() return StartState() end,
        ['play'] = function() return PlayState() end,
        ['quit'] = function() return QuitState() end
    }
    
    gStateMachine:change('start')
end

function love.update(dt)
    gStateMachine:update(dt)
end

What am I doing wrong here?

Upvotes: 0

Views: 247

Answers (1)

David Mekersa
David Mekersa

Reputation: 71

love.keyboard.wasPressed is not a standard Love2D API. Should be implemented somewhere else in your code? Check this first. Then try to put some print("condition xxx is verified") in your conditions where your code is supposed to be executed, then you'll find where the condition is not verified.

Example:

if love.keyboard.wasPressed('up') then
   print("up is pressed")
   ...

Upvotes: 0

Related Questions