Page 1 of 1

The rectangle isn't being drawn.

Posted: Fri Feb 19, 2021 8:26 pm
by Salmon
I am new to love2d, and this is probably a stupid question, but for some reason, my rectangle drawing function isn't working. I am trying to practice making separate files to keep the main file clean, and I am not sure if it is that the function isn't loading or that the draw function doesn't like external functions.

Code: Select all

-- main.lua

require("rectangle")
function love.load()
  function rectMake()
  end
end

function love.update(dt)
  function rectUpdate()
  end
end

function love.draw()
  function drawRect()
  end
end


-- rectangle.lua

function rectMake()
  rect = {}
  rect.x = 200
  rect.y = 250
  rect.wide = 100
  rect.height = 100
  rect.speed = 100
  rect.color = {}
  rect.color.red = 0
  rect.color.green = 69
  rect.color.blue = 255
end

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

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

  if love.keyboard.isDown("left") then
    rect.x = rect.x - rect.speed * dt
  end

  if love.keyboard.isDown("right") then
    rect.x = rect.x + rect.speed * dt
  end
end

function drawRect()
  love.graphics.setColor(rect.color.red,rect.color.green,rect.color.blue)
  love.graphics.rectangle("line",rect.x,rect.y,rect.wide,rect.height)
end

Re: The rectangle isn't being drawn.

Posted: Fri Feb 19, 2021 9:17 pm
by Xii

Code: Select all

function love.load()
  function rectMake()
  end
end

function love.update(dt)
  function rectUpdate()
  end
end

function love.draw()
  function drawRect()
  end
end
You're redefining these functions here to do nothing. What you probably meant was to call them:

Code: Select all

function love.load()
  rectMake()
end

function love.update(dt)
  rectUpdate()
end

function love.draw()
  drawRect()
end

Re: The rectangle isn't being drawn.

Posted: Fri Feb 19, 2021 10:36 pm
by Salmon
Thank you! this worked and this actually helps me understand Love2d a bit!