【问题标题】:Why won't my text in a Love2D game update?为什么我在 Love2D 游戏中的文字不会更新?
【发布时间】:2021-01-12 10:00:12
【问题描述】:

我终于解决了我的游戏中没有显示文本的问题,现在下一个问题是它不会更新。应该发生的是,当我按向上和向下箭头键时,所选文本会发生变化,然后按 Enter 会使我进入新状态,或者退出游戏。

我的StartState.lua 中的代码现在如下所示:

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

我已经在我的main.lua 文件中添加了一个StateMachine

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

我在这里做错了什么?

【问题讨论】:

  • 尝试查看love.keyboard.wasPressed() 以查看任何问题,但如果没有,请注意选项编号。

标签: text lua love2d


【解决方案1】:

love.keyboard.wasPressed 不是标准的 Love2D API。应该在代码中的其他地方实现吗?首先检查这个。 然后尝试将一些print("condition xxx is verified")放在你的代码应该执行的条件中,然后你会发现条件没有被验证。

例子:

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

【讨论】:

  • 其实我做了love.keyboard.wasPressed()函数但是我没有把它包含在问题中。
猜你喜欢
  • 2021-01-04
  • 2020-12-01
  • 2021-09-21
  • 1970-01-01
  • 2020-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多