Help with mousepressed function

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
Johna321
Prole
Posts: 1
Joined: Sat Mar 19, 2016 7:44 pm

Help with mousepressed function

Post by Johna321 »

I just got Love like an hour ago, and I've never used Lua before, so I'm still trying to wrap my head around some of the functions. I'm trying to make a program in which, you are a sprite (yoshi) and can move around with WASD, if you press the left mouse button, then yoshi turns to mario. Just in case, the variable mega is yoshi. :awesome:

Heres the code:

Code: Select all

function love.load()
   mega = love.graphics.newImage("yoshi.png")
   var = "Use WASD to move your character around!"
   mario = love.graphics.newImage("mario.png")
   charx = 200
   chary = 100
   mario = false
end

function love.update(dt)
   function love.draw()
      love.graphics.print(var,300,400)
      if mario == false then
         love.graphics.draw(mega,charx,chary,0,0.2,0.2)
      elseif mario == true then
         love.graphics.draw(mario,charx,chary)
      end
   end

   if love.keyboard.isDown("w") then
      chary = chary - 10
   elseif love.keyboard.isDown("s") then
      chary = chary + 10
   end

   if love.keyboard.isDown("a") then
      charx = charx - 10
   elseif love.keyboard.isDown("d") then
      charx = charx + 10
   end

   function love.mousepressed(x,y,button)
      if button == '1' then
         mario = true
         charx = x
         chary = y
      end
   end
end
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Help with mousepressed function

Post by s-ol »

the mouse button parameter to love.mousepressed is a number (like 1) but you are comparing it to a string ('1'). Remove the quotes and it should work.

Also move love.draw and love.mousepressed out of love.update! Right now you are redefining those two functions every frame.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Ikroth
Citizen
Posts: 79
Joined: Thu Jul 18, 2013 4:44 am

Re: Help with mousepressed function

Post by Ikroth »

Also, you define the variable mario as two different things.

Code: Select all

mario = love.graphics.newImage("mario.png")
Then, in the same function, you define mario to be:

Code: Select all

mario = false
So, later when you set mario equal to true and you attempt to draw the image:

Code: Select all

elseif mario == true then
	love.graphics.draw(mario,charx,chary)
end
LÖVE will throw an error, because you are trying to draw mario, which is a boolean, when it was actually expecting something you could draw (like an Image). So, you'll want to assign the image to a different variable, like marioImage.

(As a sidenote: writing "elseif mario == true" is redundant, because "if" already checks if an expression is true. So, writing: "elseif mario then" is equivalent.)
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 7 guests