So, in my game, when the player clicks, I want an image to spawn somewhere, and then move towards the point where the player clicked. I have a player image set up, and my plan is to spawn a bullet near him when the player clicks. The bullet should then move towards where the mouse clicked. I have not had any luck doing this. I have attempted some methods, but none seem to work.
Any ideas?
Moving an image towards a Point
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Moving an image towards a Point
Please post a .love file with a method you've tried.
-
- Prole
- Posts: 9
- Joined: Thu Apr 26, 2012 9:07 pm
Re: Moving an image towards a Point
The method has errors, but I will summarize what I am trying to do.
Basically, when the user clicks, the x and y position of the click are stored in variables. The origin of the bullet is the player location, also stored in variables. The game subtracts the origin x value from the destination x value to find the x rate (That is, how much the x value should increase or descrease) and the same thing for the y values. It then adds the x rate*dt to the bullet x, and the y rate*dt to the bullet y. This should cause the bullet to move towards the cursor. But it doesnt work. The code is attached, but i get an error.
Basically, when the user clicks, the x and y position of the click are stored in variables. The origin of the bullet is the player location, also stored in variables. The game subtracts the origin x value from the destination x value to find the x rate (That is, how much the x value should increase or descrease) and the same thing for the y values. It then adds the x rate*dt to the bullet x, and the y rate*dt to the bullet y. This should cause the bullet to move towards the cursor. But it doesnt work. The code is attached, but i get an error.
- Attachments
-
- move.love
- (1.96 KiB) Downloaded 227 times
Re: Moving an image towards a Point
xrate and yrate are being made within love.keypressed, at the top just put
It shouldn't error now
Code: Select all
local xrate,yrate = 0
Your screen is very zoomed in...
-
- Prole
- Posts: 9
- Joined: Thu Apr 26, 2012 9:07 pm
Re: Moving an image towards a Point
Still doesn't work, also, there is not keyPressed function, only a mousePressed on, is that what you meant?
Re: Moving an image towards a Point
Here, did that for you:
You had to set the globals in the beginning of the code (altough I prefer setting them in love.load() ), also the bullet resetted its position everytime in love.update since you used originx and add the velocity of the bullet.
Hope you'll understand it, I commented all my changes in the code <3
You had to set the globals in the beginning of the code (altough I prefer setting them in love.load() ), also the bullet resetted its position everytime in love.update since you used originx and add the velocity of the bullet.
Hope you'll understand it, I commented all my changes in the code <3
- Attachments
-
- pewpewpew.love
- (2.19 KiB) Downloaded 237 times
Re: Moving an image towards a Point
woops typoblackout_alex wrote:Still doesn't work, also, there is not keyPressed function, only a mousePressed on, is that what you meant?
Your screen is very zoomed in...
-
- Prole
- Posts: 9
- Joined: Thu Apr 26, 2012 9:07 pm
Re: Moving an image towards a Point
Alex22 wrote:Here, did that for you:
You had to set the globals in the beginning of the code (altough I prefer setting them in love.load() ), also the bullet resetted its position everytime in love.update since you used originx and add the velocity of the bullet.
Hope you'll understand it, I commented all my changes in the code <3
Thanks so much!
It works. and I understand it. Thanks!
Who is online
Users browsing this forum: Bing [Bot] and 4 guests