How can I make an object remain in the x position it was fired from?

I have a character moving back and forth across the screen that fires a projectile, and I want the projectile to stay on screen.

The Y position part of my code is working, but I cannot get my projectiles to stay in the X position that they were launched from using a mousePress.

Does anybody have any tips on how to achieve this?

1 Like


The idea is to have separate variables for the x position of the character and of the projectile - because they are not the same but express different things!

Therefore you need float characterX AND float projectileX

upon firing, make sure to say projectileX=characterX; // copy the current character pos X

  • when displaying the projectile use projectileX

  • when displaying the character use characterX

Hey, and welcome to the forum, great to have you here!


1 Like