Page 1 of 1
Trouble with move to mouse when clicked
Posted: Sat Sep 14, 2024 6:12 pm
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.
Re: Trouble with move to mouse when clicked
Posted: Sat Sep 14, 2024 8:59 pm
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.
Re: Trouble with move to mouse when clicked
Posted: Sun Sep 15, 2024 9:05 am
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.
Re: Trouble with move to mouse when clicked
Posted: Mon Sep 16, 2024 1:08 am
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
Re: Trouble with move to mouse when clicked
Posted: Mon Sep 16, 2024 1:50 pm
by dusoft
Or they can use available tweening libraries that take care of that:
https://github.com/love2d-community/awe ... d#tweening
Re: Trouble with move to mouse when clicked
Posted: Mon Sep 16, 2024 9:34 pm
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.
Re: Trouble with move to mouse when clicked
Posted: Wed Sep 18, 2024 5:15 am
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:
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.