简体   繁体   English

Love2D Lua错误:尝试调用字段“ isDown”(nil值)

[英]Love2D Lua error: attempt to call field 'isDown' (a nil value)

This is my code in Love2D: 这是我在Love2D中的代码:

function love.load()   
    ninja = love.graphics.newImage("Ninja.png")
    x = 0
    y = 0
    speed = 256
end

function love.update(dt)
    if love.keyboard.isDown("right") then
        ninja = love.graphics.newImage("NinjaRight.png")
        x = x + (speed * dt)
    end

    if love.keyboard.isDown("left") then
        ninja = love.graphics.newImage("NinjaLeft.png")
        x = x - (speed * dt)
    end

    if love.keyboard.isDown("down") then
        y = y + (speed * dt)
    end

    if love.keyboard.isDown("up") then
        y = y - (speed * dt)
    end

    if love.joystick.isDown(joystick, 1, 2, 3, 4) then
        a = 5
    end
end


function love.draw()
    love.graphics.draw(ninja, x, y)
end

I want to make the game to recognize a controller when connected. 我想使游戏在连接时能够识别控制器。 But when I run the game, I receive the error: 但是当我运行游戏时,我收到错误消息:

attempt to call field 'isDown'(a nil value) 尝试调用字段“ isDown”(nil值)

Where is the problem? 问题出在哪儿?

Since LÖVE 0.9.0 Joystick related isDown() function is moved to another namespace / table /You name ir or more "object" like structure. 由于LÖVE 0.9.0与操纵杆相关的isDown()函数已移至另一个namespace / table /您可以命名ir或更多“对象”之类的结构。 [1] [1]

So, in Your code You should use it something like this: 因此,在您的代码中,您应该使用如下代码:

--Get table of all connected Joysticks:
local joysticks = love.joystick.getJoysticks()

--Pick first one:
local joystick = joysticks[1]

if joystick:isDown(1, 2, 3, 4) then
    a = 5
end

Where joystick is Your Joystick object. 其中joystick是您的Joystick对象。 [2] [2]

Be aware, love.keyboard.isDown() usage haven't changed yet. 请注意, love.keyboard.isDown()的用法尚未更改。 But, I guess, it's about to too. 但是,我想也是。 Sooner or later. 迟早。

[1] https://love2d.org/wiki/Joystick:isDown [1] https://love2d.org/wiki/Joystick:isDown

[2] https://love2d.org/wiki/love.joystick.getJoysticks [2] https://love2d.org/wiki/love.joystick.getJoysticks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM