Firing Bullets in certain direction

hi i was just wondering if anybody can help me with my problem? if so great and thanks :slight_smile:

i am currently working on my college project i am making a top down shooter game i have it coded so that when the player presses WASD keys the sprites image changes to face the current direction the problem i am running into is that i have variables that are tied to the the methods that change the image to different directions to tell what position is currently in so i know which was to fire the bullets.

the problem is when i fire the bullet it goes the way i want it to go but when i change direction using the keyboard the bullet goes in the same direction as the player is facing like it should but thats a problem.

is there a way to when the user fires the bullets it can no longer be control by the keyboard i.e it just fire the way its facing and when the player changes direction the bullet still travels that way.

Any help would be greatly appreciated thank you.

Easiest way is to create a class called bullet and pass it something like:
startX, startY, deltaX, deltaY (or just direction if you don’t want to allow 360 degrees)

You can put the bullet object into a collection (or whatever you want to use to decide what should be drawn each frame) and then it won’t change direction anymore :slight_smile:

Mike

code

Give things good names, no need in keeping them really short (m, ms? meters and milliseconds? :P) :slight_smile:

What is up with m.move(); I do see a function for it that doesn’t do anything (but that has some parameters), so where is the real one inherited from? I see a moveLeft and a moveRight function and I guess they are called from move. What decides whether moveLeft or moveRight is called? mainPlayer.getDirectionX()? If so you should store the direction when the bullet is created and reuse that.

Both move left and move right has this btw:
if (getPositionX() > BOARD_WIDTH)
setVisible(false);
I am guessing moveLeft should have:
if (getPositionX() < 0)
setVisible(false);

Mike