• Advertisement
Sign in to follow this  

Projectile moves according to ship moves even when shot

This topic is 1841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

 

 

 
Edited by warnexus

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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! 

Edited by warnexus

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement