Trouble with move to mouse when clicked

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
MaxedOutNodes
Prole
Posts: 2
Joined: Sat Sep 14, 2024 5:49 pm

Trouble with move to mouse when clicked

Post by MaxedOutNodes »

Hi guys,

I've got this simple program and the idea is that when I left click, the player (square in this case) moves to where the cursor is. I have managed to get the square to move one pixel every time I click but I want it so that when I click, the square moves in a straight line towards that point until it reaches it or I click somewhere else.

I've tried while loops but it just crashes. Any ideas? Thank you for any help.
Attachments
main.lua
(969 Bytes) Downloaded 2539 times
User avatar
BrotSagtMist
Party member
Posts: 659
Joined: Fri Aug 06, 2021 10:30 pm

Re: Trouble with move to mouse when clicked

Post by BrotSagtMist »

Ohgosh please learn the basics first.
You are not calling anything whithin love.update, you are creating stuff instead.
You recreate the same thing 60 times a second.
This is a call: movePlayer()
This is a creation: function movePlayer()
And creating alone does not run the code so essentially your love.update is empty aside from eating ram by creating unused data.
Now the reason that at least anything happens is that once love.mousepressed is created anywhere in the code it calls itself whenever the mouse is pressed.

Now if you want to have a continous move you gotta have mouspressed a bolean flag true and mousereleased turn this to false. Then in update, whenever this flag is set to true, call move.
obey
User avatar
dusoft
Party member
Posts: 654
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Trouble with move to mouse when clicked

Post by dusoft »

BrotSagtMist wrote: Sat Sep 14, 2024 8:59 pm Ohgosh please learn the basics first.
You are not calling anything whithin love.update, you are creating stuff instead.
You recreate the same thing 60 times a second.
[snip]
Thanks for writing this. I started to write an answer, but then decided not to solve basic issues, when somebody didn't do their homework.
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: Trouble with move to mouse when clicked

Post by RNavega »

The use of a flag variable to keep track of what the box is doing, like BrotSagtMist suggested, is a tiny version of a finite state machine (a way of organizing your code to allow some continuous action over time, like moving towards the mouse click, patrolling a dungeon, planting crops, helping grandma cross the street etc)
https://gamedevelopertips.com/finite-st ... evelopers/

https://www.love2d.org/forums/viewtopic ... 35#p222935
User avatar
dusoft
Party member
Posts: 654
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Trouble with move to mouse when clicked

Post by dusoft »

Or they can use available tweening libraries that take care of that:
https://github.com/love2d-community/awe ... d#tweening
MaxedOutNodes
Prole
Posts: 2
Joined: Sat Sep 14, 2024 5:49 pm

Re: Trouble with move to mouse when clicked

Post by MaxedOutNodes »

Thanks guys, I've come from web dev in Javascript still learning Lua and graphics, I'll use the tween library that's already done the mathematics.
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: Trouble with move to mouse when clicked

Post by RNavega »

Hey. So, in this particular case --using constant-velocity movement, as well as finite state machines-- if you don't know these subjects already, I think skipping on learning them by using a library is gonna have a huge 'negative' impact on your games programming journey, because they are used everywhere.

We learn constant-velocity motion in high school, with the classic kinematics formulas, and also linear algebra:

Code: Select all

s = v * t
P = P₀ + V * t
That takes care of calculating the positions of things.

Then, using FSMs you can produce the same behaviors seen in all 2D arcade and early console games -- like Castlevania, Mario, Galaga, Street Fighter, Metal Slug etc.

These are some great articles with the theory behind FSMs: Good luck.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 8 guests