0
votes

I don't know what I'm doing wrong... Im getting the error in the player.draw() function. the bad argument that its talking about is in the love.graphics.rectangle() method. It's saying that v.x is a bad argument. But it should be working. both v.x and v.y should be working. Because its accessing fields of elements within the table or array right? Can someone tell me what I'm doing wrong and how to fix this? much appreciation! Here is my code:

require "scripts.player"

width = love.graphics.getWidth()

block = {}
block.width = 60
block.height = 10
block.speed = 150
block.timer = 0
block.timerLim = math.random(1,2)
block.spawnX = math.random(0, width - player.width)

function block.spawn(x,y)
    table.insert(block, {x = x, y = y})
end

function block.move(dt)
    for i,v in ipairs(block) do
        v.y = v.y + block.speed * dt
    end
end

function block.draw()
    for i,v in ipairs(block) do 
        love.graphics.setColor(255,0,255)
        love.graphics.rectangle("fill", v.x, v.y, block.width block.height)
    end
end

function block.spawnHandler(dt)
    block.timer = block.timer + dt
    if block.timer > block.timerLim then
        block.spawn(spawnX, -10)
        block.timer = 0
        block.timerLim = math.random(1,2)
        block.spawnX = math.random(0, width - block.width)
    end
end

-- Parent Functions --

function DRAW_BLOCK()
    block.draw()
end

function UPDATE_BLOCK(dt)
    block.move(dt)
    block.spawnHandler(dt)
end
1

1 Answers

1
votes

in function block.spawnHandler you meant block.spawnX instead of spawnX.

Since spawnX does not exist, its value is nil, which goes into the spawn function and gets set as the x value of the coordinate which then makes its way into rectangle and is the bad argument.

You can read more about that and how to prevent it from biting you again here: https://www.lua.org/pil/13.4.1.html

In short, lua is really weird, until you 'fix' it by making accesses to nonexistent variables throw errors instead of return nil, and about 800 other things like that. Once you 'fix' all those things, you have something which is merely weird and is not quite lua anymore.