Player moving to a object

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
rfowler1
Prole
Posts: 1
Joined: Thu Feb 14, 2019 11:04 pm

Player moving to a object

Post by rfowler1 »

Hey all, I have been working on a prototype for a couple weeks, its a adventure game that involves clicking to where you want to go. I have run into an issue with having the character move to a point that is created on the screen when the mouse clicks. Currently the character moves 1 pixel towards the object upon creation and stops. I have been trying to figure out maybe some kind of loop that will just add to the (X,Y) of the player until it equals the markers (X,Y) unfortunately I have found no solution.

Code: Select all

function love.load()
	playerX = 1
	playerY = 1
	playerDifference = playerX/playerY

	markerX = 1
	markerY = 1
	markerDraw = false
	markerDifference = 1

	playerCooldown = 50
end

function love.update(dt)
	if love.keyboard.isDown("d") then
		playerX = playerX + 4
	end
	if love.keyboard.isDown("a") then
		playerX = playerX - 4
	end
	if love.keyboard.isDown("w") then
		playerY = playerY - 4
	end
	if love.keyboard.isDown("s") then
		playerY = playerY + 4
	end
end

function love.mousepressed(x, y, button, istouch)
	if button == 1 then
		markerDraw = true
		markerX = x
		markerY = y
		markerDifference = x/y	

		if playerDifference ~= markerDifference then
			playerX = playerX + 1
			playerY = playerY + 1
		end
	end	
end
 
function love.draw()
	if markerDraw == true then
		love.graphics.circle("fill", markerX, markerY, 5,5)
	end
	love.graphics.rectangle("fill", playerX, playerY, 20, 20)
end
I have seen people use math.tan or sin, cos ect, to move their character towards the mouse but im not sure how to apply it to a object. Thanks in advance!
MightyPancake
Prole
Posts: 26
Joined: Thu Feb 14, 2019 7:45 pm

Re: Player moving to a object

Post by MightyPancake »

Hi, I'm new to LÖVE engine, however, I think I got Your issue here! The thing is, the love.mousepressed function is called ONLY while pressing button, which means it runs only once (well, that is if You're not literally smashing right-click on Your mouse of course :P )
My point is, You should insert
**
If PlayerDiffrence~=MarkerDiffrence
--code goes here
end
**
In the love.update instead of love.mousepressed
Note: Don't forget to utilize dt!!!
I hope I've helped You with that one ;)
Post Reply

Who is online

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