【问题标题】:keyboard keyisdown is not activating键盘按键未激活
【发布时间】:2021-08-29 09:07:59
【问题描述】:

当我按下 d 和 a 按钮时,我试图让我的桨上下移动。 当我按下 d 和 a 按钮时,什么都没有发生……他们只是坐在那里…… 无论如何,如果您知道,请告诉我代码如下。谢谢 我知道缩进是错误的,如果这篇文章我必须更改:)

  window_width = 580
   window_height = 420

   playerScore = 0
        enemyScore = 0

   pSpeed = 200

       player1Y = 100
    player2Y = -20




   function love.load()

  love.window.setMode(window_width, window_height)

   end

    function love.draw()
      love.graphics.printf('hello pong', 0, window_height / 2 - 6, window_width, 'center')
     love.graphics.rectangle("fill", 500, player1Y , 10, 50)




   love.graphics.rectangle("fill", 40, 220, 10, 50)

   love.graphics.rectangle("fill", 100, 230, 7, 5)


 end
 function update(dt)

if love.keyboard.isDown('d') then                     
   player1Y = player1Y + pSpeed * dt
    elseif love.keyboard.isDown('a') then               
     player1Y = player1Y + -pSpeed * dt
        end
      end

【问题讨论】:

    标签: lua love2d


    【解决方案1】:

    尝试使用pSpeed = 10 而不是 200,这样您的矩形不会在 2 个步骤中超出 window_height。
    每个矩形都有change colors,所以更容易看到发生了什么。

    function love.draw()
        love.graphics.setColor( 0, 0, 0 )  --  black
        love.graphics.printf( 'hello pong', 0, window_height /2 -6, window_width, 'center' )
    
        love.graphics.setColor( 1, 0, 0 )  --  red
        love.graphics.rectangle( "fill", 500, player1Y, 10, 50 )
    
        love.graphics.setColor( 0, 1, 0 )  --  green
        love.graphics.rectangle( "fill", 40, 220, 10, 50 )
    
        love.graphics.setColor( 0, 0, 1 )  --  blue
        love.graphics.rectangle( "fill", 100, 230, 7, 5 )
    end
    

    【讨论】:

      【解决方案2】:

      我认为你应该写“love.update(dt)”而不是只写“update(dt)”。我试着改变它,它奏效了!此外,缩进不会影响代码在 Lua 中的工作方式,但会大大提高可读性。

      【讨论】:

        猜你喜欢
        • 2012-09-21
        • 1970-01-01
        • 2015-03-27
        • 1970-01-01
        • 2013-08-16
        • 1970-01-01
        • 2011-09-17
        • 2018-03-16
        • 2016-10-11
        相关资源
        最近更新 更多