Mouse moved character

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
dominicus
Prole
Posts: 4
Joined: Tue Sep 08, 2015 8:21 pm

Mouse moved character

Post by dominicus »

Well my problem seems to be the logic one or i just don't understand how love engine works, but either way i have some code,
problem lies in character movement it moves a little then stops i want it to move to exact destination point clicked by mouse.
Its just part of code...

Code: Select all

player = {}
player.image = love.graphics.newImage("data/player.png");
player.x = 0
player.y = 0
player.speed = 100/20
function player.draw()
	love.graphics.draw(player.image, player.x, player.y);
end
function player.move_to(_x, _y)
	dx = _x - player.x
	dy = _y - player.y
	length = math.sqrt(dx*dx+dy*dy);

	dx = (dx/length)
	dy = (dy/length)

	player.x = (player.x + dx * player.speed)
	player.y = (player.y + dy * player.speed)
end

--============================================================
function love.load()	
	love.graphics.setBackgroundColor(255, 255, 255);
end

function love.update(dt)

end

function love.draw()
	player.draw();
end

function love.mousepressed(x, y, button)
	if love.mouse.isDown(button) and button == "l" then
		player.move_to(x, y)
	end
end
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

Re: Mouse moved character

Post by Qcode »

You have the love.mouse.isDown part down correctly, but the thing is mousepressed is only triggered once for each individual click. For continuous movement, you want it checking every single frame. So to fix this you can completely remove love.mousepressed, and add this into love.update instead:

Code: Select all

function love.update(dt)
   if love.mouse.isDown("l") then --Check every frame if left mouse button is down
      local mousex, mousey = love.mouse.getPosition() --Get mousex and mousey because it's not given to us
      player.move_to(mousex, mousey)
   end
end
I'd also recommend using dt in your player movement calculations, if you weren't considering that already. Just multiply any speeds you use by dt to make sure that calculations are in pixels/second instead of pixels/frame (because variable frame rate would alter speed/second).
dominicus
Prole
Posts: 4
Joined: Tue Sep 08, 2015 8:21 pm

Re: Mouse moved character

Post by dominicus »

Thanks works like a charm... btw now I can use dt
permadeth
Prole
Posts: 9
Joined: Thu Apr 17, 2014 10:17 pm

Re: Mouse moved character

Post by permadeth »

There is another way to get the player to move with a different technique. This one causes the player to move with a single click, rather than holding down the mouse button. You just have to add a couple variables (note: there may be a more elegant solution :P )

Code: Select all

player = {}
player.image = "@";
player.x = 0
player.y = 0

player.newX = 0 -- ADDED A TARGET FOR THE PLAYER
player.newY = 0 -- X AND Y (THESE GET CHANGED BY THE love.mousepressed (x,y, button) FUNCTION)

player.speed = 100/20

player.moving = false -- A BOOLEAN TO CHECK IF THE PLAYER SHOULD BE MOVING OR NOT. ALSO SET BY ...mousepressed ()... AND USED IN player.move_to ()

function player.draw()
   love.graphics.print(player.image, player.x, player.y);
end
function player.move_to(_x, _y, dt) -- NOTICE YOU CAN ADD dt HERE, SINCE WE'LL CALL THIS IN love.update(dt) NOW
   dx = _x - player.x
   dy = _y - player.y
   length = math.sqrt(dx*dx+dy*dy);

   dx = (dx/length)
   dy = (dy/length)

   if player.moving == true then -- IF THE PLAYER SHOULD BE MOVING THEN...
     player.x = (player.x + dx * player.speed)
     player.y = (player.y + dy * player.speed)
   else -- IF IT SHOULD STOP (REACHED NEW DESTINATION ALREADY)
    player.x = player.x
    player.y = player.y
  end
end

--============================================================
function love.load()
   --love.graphics.setBackgroundColor(0, 255, 255);
end

function love.update(dt)

  player.move_to(player.newX, player.newY, dt) -- INCLUDE THIS IN UPDATE, TO CHECK EVERY TICK

   --[[if love.mouse.isDown("l") then --Check every frame if left mouse button is down
      local mousex, mousey = love.mouse.getPosition() --Get mousex and mousey because it's not given to us
      player.move_to(mousex, mousey)
   end]]
end

function love.draw()
   player.draw();
end

function love.mousepressed(x, y, button)
   if love.mouse.isDown(button) and button == "l" then

      player.moving = true -- INSTEAD OF ACTUALLY MOVING THE PLAYER, WE CAN TELL move_to THAT THE PLAYER SHOULD MOVE NOW
      player.newX = x        -- GIVE THE X
      player.newY = y       -- AND Y DESTINATIONS
    else
      player.moving = false -- OR JUST DO NOTHING IF THE MOUSEBUTTON IS NOT PRESSED
    end
end
PS. I didn't have a player image handy, so I changed the image to an @ symbol, and turned off the background color! Thanks for the initial code, I was coming at it in a confusing way before!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Bing [Bot] and 7 guests