The rectangle isn't being drawn.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Salmon
Prole
Posts: 2
Joined: Fri Feb 19, 2021 4:49 pm

The rectangle isn't being drawn.

Post 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
Attachments
example.love
(645 Bytes) Downloaded 117 times
User avatar
Xii
Party member
Posts: 137
Joined: Thu Aug 13, 2020 9:09 pm
Contact:

Re: The rectangle isn't being drawn.

Post 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
Salmon
Prole
Posts: 2
Joined: Fri Feb 19, 2021 4:49 pm

Re: The rectangle isn't being drawn.

Post by Salmon »

Thank you! this worked and this actually helps me understand Love2d a bit!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest