Mouse functions not working correctly [SOLVED]

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
miniaturedog
Prole
Posts: 11
Joined: Sat Apr 27, 2019 8:21 pm

Mouse functions not working correctly [SOLVED]

Post by miniaturedog »

Earlier I made a post asking how touch functions in Love2d work and was given the suggestion to try mouse functions instead, since I don't need to manage simultaneous touches. My goal is to make the player move left or right depending on which (horizontal) half of the screen is touched/clicked, but only one side seems to work and I'm not sure which part of my code is causing the problem. Here is a snippet with all of the mouse code from my player.lua file included:

Code: Select all

anim8 = require "libraries/anim8/anim8"

function playerload()
  sprite = love.graphics.newImage("assets/turtle_walking2.png")
 -- player attributes and animations, etc are here
  mouseX = love.mouse.getX()
  mouseLeft = mouseX < love.graphics.getWidth() / 2
  mouseRight = mouseX > love.graphics.getWidth() / 2
end

function playerupdate(dt)
  player.animation:update(dt)

  -- mouse / touch controls
  if love.mouse.isDown(1) and mouseRight then
    player.x = player.x + (player.speed * dt)
    player.animation = player.animations.right
  end
  if love.mouse.isDown(1) and mouseLeft then
    player.x = player.x - (player.speed * dt)
    player.animation = player.animations.left
  end
end

-- idle animations
function love.mousereleased(x, y, button, istouch)
  if mouseRight == true then
    player.animation = player.animations.idleRight
  end
  if mouseLeft == true then
    player.animation = player.animations.idleLeft
  end
end

function playerdraw()
  player.animation:draw(player.sprite, player.x + player.ox, player.y + player.oy, 0, 2, 2, player.ox, player.oy)
end
I also have attached the .love file (which for some reason has appeared with a ".love.zip" extension?). Either way, any help or suggestions are appreciated!
Attachments
turtle-game.love.zip
(406.62 KiB) Downloaded 124 times
Last edited by miniaturedog on Wed May 01, 2019 5:16 pm, edited 1 time in total.
User avatar
keharriso
Party member
Posts: 109
Joined: Fri Nov 16, 2012 9:34 pm

Re: Mouse functions not working correctly

Post by keharriso »

Add this to player.lua:

Code: Select all

function love.mousepressed(x, y, button, istouch, presses)
  mouseX = love.mouse.getX()
  mouseLeft = mouseX < love.graphics.getWidth() / 2
  mouseRight = mouseX > love.graphics.getWidth() / 2
end
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
User avatar
miniaturedog
Prole
Posts: 11
Joined: Sat Apr 27, 2019 8:21 pm

Re: Mouse functions not working correctly

Post by miniaturedog »

keharriso wrote: Wed May 01, 2019 4:45 pm Add this to player.lua:

Code: Select all

function love.mousepressed(x, y, button, istouch, presses)
  mouseX = love.mouse.getX()
  mouseLeft = mouseX < love.graphics.getWidth() / 2
  mouseRight = mouseX > love.graphics.getWidth() / 2
end
That works perfectly, thank you!
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Mouse functions not working correctly [SOLVED]

Post by zorg »

My understanding is that the x parameter that you get from mousepressed should be the same what love.mouse.getX returns (at the moment you pressed a button somewhere, i mean), so it' practically not needed there; just doing mouseX = x should work.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 5 guests