Moving an image towards a Point

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
blackout_alex
Prole
Posts: 9
Joined: Thu Apr 26, 2012 9:07 pm

Moving an image towards a Point

Post by blackout_alex »

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?
iemfi
Citizen
Posts: 52
Joined: Fri Mar 30, 2012 11:03 am

Re: Moving an image towards a Point

Post by iemfi »

Please post a .love file with a method you've tried.
blackout_alex
Prole
Posts: 9
Joined: Thu Apr 26, 2012 9:07 pm

Re: Moving an image towards a Point

Post by blackout_alex »

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.
Attachments
move.love
(1.96 KiB) Downloaded 228 times
User avatar
mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

Re: Moving an image towards a Point

Post by mickeyjm »

xrate and yrate are being made within love.keypressed, at the top just put

Code: Select all

local xrate,yrate = 0 
It shouldn't error now
Your screen is very zoomed in...
blackout_alex
Prole
Posts: 9
Joined: Thu Apr 26, 2012 9:07 pm

Re: Moving an image towards a Point

Post by blackout_alex »

Still doesn't work, also, there is not keyPressed function, only a mousePressed on, is that what you meant?
Alex22
Prole
Posts: 26
Joined: Wed Jun 02, 2010 5:42 pm

Re: Moving an image towards a Point

Post by Alex22 »

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
Attachments
pewpewpew.love
(2.19 KiB) Downloaded 237 times
User avatar
mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

Re: Moving an image towards a Point

Post by mickeyjm »

blackout_alex wrote:Still doesn't work, also, there is not keyPressed function, only a mousePressed on, is that what you meant?
woops typo
Your screen is very zoomed in...
blackout_alex
Prole
Posts: 9
Joined: Thu Apr 26, 2012 9:07 pm

Re: Moving an image towards a Point

Post by blackout_alex »

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!
:awesome:
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 11 guests