Projectile moves according to ship moves even when shot

Started by
1 comment, last by Nicholas Kong 11 years, 3 months ago

I am working on making a Top-Down Shooter Clone in Java. After an hour or so, I was able to figure how to position the ship's projectile which is a laser relative to the direction of the ship's sprite. Problem is, if I move the ship, the projectile follows relative to the ship direction when I move the ship. As you can see this is a problem because the laser should be moving independently regardless of where the ship is when it is fired.

Here's the code which presents my problem:

gameProblem1_zpsb4ee2df9.png

Here's the picture mainly to prove I have a working game(the ship can move with left and right arrows keys and fire a projectile with z button!):

gameProblem_zps60f87d95.png

Advertisement

Your laser coordinates should not be set to anything initially. Once you press the fire button, you then assign the X and Y location of the laser to where the nose of the ship is. Then, every loop through, you simply increment the Y location based on a velocity. There is no need to keep setting the X location relative to the ship after it has been fired.

Also, you will want to store a list of lasers, assuming you can have mroe than 1 laser on screen at a time, and you'll do the same thing, except loop through the lasers every frame, check if it hits something, or if it's gone off the screen, and remove it from the list if so.

Good luck!

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Your laser coordinates should not be set to anything initially. Once you press the fire button, you then assign the X and Y location of the laser to where the nose of the ship is. Then, every loop through, you simply increment the Y location based on a velocity. There is no need to keep setting the X location relative to the ship after it has been fired.

Also, you will want to store a list of lasers, assuming you can have mroe than 1 laser on screen at a time, and you'll do the same thing, except loop through the lasers every frame, check if it hits something, or if it's gone off the screen, and remove it from the list if so.

Good luck!

Initially I wanted to do a while loop through to decrease the laser_dx so the laser can move up but the paintComponent in Java is like a loop in itself. So if I chosen to do a while loop, I would actually get an infinite loop.

Thanks for the suggestion for more than one laser. I am going to take your feedback and come up with the solution right now. Thanks for the reply! =]

On a side note, Nice AttackAndDefend Video on youtube. I like the collision detection and lighting!

This topic is closed to new replies.

Advertisement