The right location of mousepressed

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
User avatar
Azvd
Prole
Posts: 2
Joined: Sat Jul 22, 2017 8:45 am
Contact:

The right location of mousepressed

Post by Azvd »

Hi, guys!

As you can see, I'm new and starting right now with LÖVE.
So... I was reading the Getting Started and saw that love.update(dt) is called continuously to update everything.

Well, I was trying to do something and made this:

Code: Select all

function love.load()
  love.window.setTitle("Jogo Legal")
  char = love.graphics.newImage("avatar.png")
  imgx = 400
  imgy = 300
end

function love.update(dt)
  function love.mousepressed(x, y, button, istouch)
    if button == 1 then
      imgx = x
      imgy = y
    end
  end
end

function love.draw()
  love.graphics.draw(char, imgx, imgy)
end
I've did it before reading the whole tutorial. I've opened the mousepressed page of the wiki and saw that this function was outside the love.update. I went searching here in forums and saw some people using it outside update, even though it updated some variables.

Well, what's the right place for it? I mean, I'm not calling this function everytime, so it should be inside love.update, since I use it to update the coordinates of my image, no? I don't get it hahaha
Ignore if it's a stupid question, I'm still learning :rofl:

Cya,
Azvd
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: The right location of mousepressed

Post by Nixola »

The love.mousepressed function should be defined outside love.update; you don't even need to call it. Its purpose is to be available so that LÖVE itself can call it when a mouse button is pressed. What you have technically works, but redefines the function every frame which is unneeded.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Azvd
Prole
Posts: 2
Joined: Sat Jul 22, 2017 8:45 am
Contact:

Re: The right location of mousepressed

Post by Azvd »

Nixola wrote: Sat Jul 22, 2017 9:05 am The love.mousepressed function should be defined outside love.update; you don't even need to call it. Its purpose is to be available so that LÖVE itself can call it when a mouse button is pressed. What you have technically works, but redefines the function every frame which is unneeded.
Oh, I see. Thank you @Nixola! Very helpful and fast on answering :ultrahappy:
Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests