Page 1 of 1

Moving an image towards a Point

Posted: Thu Apr 26, 2012 11:45 pm
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?

Re: Moving an image towards a Point

Posted: Fri Apr 27, 2012 7:49 am
by iemfi
Please post a .love file with a method you've tried.

Re: Moving an image towards a Point

Posted: Fri Apr 27, 2012 6:32 pm
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.

Re: Moving an image towards a Point

Posted: Fri Apr 27, 2012 6:39 pm
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

Re: Moving an image towards a Point

Posted: Sat Apr 28, 2012 8:56 pm
by blackout_alex
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

Posted: Sun Apr 29, 2012 1:09 am
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

Re: Moving an image towards a Point

Posted: Sun Apr 29, 2012 10:18 am
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

Re: Moving an image towards a Point

Posted: Sun Apr 29, 2012 3:00 pm
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: