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.
Trouble with move to mouse when clicked
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 2
- Joined: Sat Sep 14, 2024 5:49 pm
Trouble with move to mouse when clicked
- Attachments
-
- main.lua
- (969 Bytes) Downloaded 2497 times
- BrotSagtMist
- Party member
- Posts: 657
- Joined: Fri Aug 06, 2021 10:30 pm
Re: Trouble with move to mouse when clicked
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.
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
Re: Trouble with move to mouse when clicked
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.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]
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
Re: Trouble with move to mouse when clicked
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
https://gamedevelopertips.com/finite-st ... evelopers/
https://www.love2d.org/forums/viewtopic ... 35#p222935
Re: Trouble with move to mouse when clicked
Or they can use available tweening libraries that take care of that:
https://github.com/love2d-community/awe ... d#tweening
https://github.com/love2d-community/awe ... d#tweening
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
-
- Prole
- Posts: 2
- Joined: Sat Sep 14, 2024 5:49 pm
Re: Trouble with move to mouse when clicked
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
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.
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
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.
Who is online
Users browsing this forum: Google [Bot], Majestic-12 [Bot], Semrush [Bot] and 2 guests