Am i using the Draw function wrong?

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
Ungerbla
Prole
Posts: 1
Joined: Tue Oct 03, 2017 2:20 pm

Am i using the Draw function wrong?

Post by Ungerbla »

Code: Select all

local maxX = 800
local maxZ = 600



-- veribles for the movement of the drawing
local x = 1
local z = 1

-- veribles for the color function
local red = 255
local green = 0
local blue = 0
local alpha = 255


function love.load()    -- loads the window
    love.window.setMode(maxX, maxZ, {resizable=false, vsync=false, minwidth=400, minheight=300})
end




function color()  -- function used when drawing
  red = red
  green = green
  blue = blue
  alpha = 255
  
    return red, green, blue, alpha
end




function rectangle() -- function used when drawing a shape
  local shape = "fill"
  x = x
  z = z
  local sizeX = 10
  local sizeY = 10
  
  return shape, x, z, sizeX, sizeY
  end

local clock = os.clock
function sleep(n)  -- seconds
  local t0 = clock()
  while clock() - t0 <= n do end
end

  
  
  
  
  
function love.draw()
love.graphics.setColor(color())
love.graphics.rectangle(rectangle())
end


function Xmove()
  if x == maxX then
    x = 1
    z = z + 1
  else
    x = x + 1
  
  end

end


function draw()
  for i = 1, 500 do
  love.draw()
  Xmove()
  end
end


--Game start here

love.load()
draw()

grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Am i using the Draw function wrong?

Post by grump »

Yes. Don't call love.load() and love.draw() manually. love.load() will be called automatically when the game starts, love.draw() will be called automatically when you should draw something. Remove the last two lines, and do the things that you do in draw() in love.draw() instead.
Atone
Prole
Posts: 6
Joined: Mon Aug 28, 2017 5:16 am

Re: Am i using the Draw function wrong?

Post by Atone »

To expand on grump's answer a little: The game loop (where love.load, love.draw, love.update, etc get called) is actually defined in the love.run function. If you don't make a love.run function, a sensible default is provided for you (check it out on the wiki). So if you're just using the default, love will call love.load exactly once at the start of the game and then call love.draw and love.update once every frame. No need for you to call them after defining them.
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: Am i using the Draw function wrong?

Post by sphyrth »

Let's put this in bare-bones code form:

Code: Select all

function love.load()
   -- Initialize some stuff
   -- I think you should put your x, y, red, etc... things here.
end

-- Correction 1: Your Xmove() should work like this. Not in draw()
function Xmove()
end

function love.update(dt)
   Xmove()
end

-- Correction 2: This is how these two functions should work. Your code got it backwards
function draw()
   -- Draw your rectangles
end

function love.draw()
   draw()
end

-- Correction 3: You don't do any of these bottom lines anymore. Just remove them from your code
love.load()
love.draw()
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 6 guests