View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Projectile moves according to ship moves even when shot

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1warnexus  Prime Members

Posted 06 January 2013 - 09:36 PM

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:

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!):

Edited by warnexus, 06 January 2013 - 09:40 PM.

### #2BeerNutts  Members

Posted 06 January 2013 - 09:59 PM

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)

### #3warnexus  Prime Members

Posted 06 January 2013 - 10:12 PM

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, 06 January 2013 - 10:17 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.