Actually the question is to make a bullet fly in two directions in width relative to the player himself. For example, if a player shoots to the right, the bullet flies to the right; if to the left, it flies to the left ... but I don’t write the condition itself correctly! it turns out that he shoots everything correctly, but if I turn the player in the other direction and shoot, then those bullets that were flying in the other side turn and fly in the direction the player is shooting at the moment and vice versa the same thing happens! There is such a code ...

Bullet Bullet Class:

 public class Bullet { private Rectangle rect; private float velocity; public Bullet(Rectangle rect, float velocity) { this.rect = rect; this.velocity = velocity; } public void updateRight() { rect.x += velocity; } public void updateLeft() { rect.x -= velocity; } public Rectangle getRect() { return rect; } public float getVelocity() { return velocity; } } 

Player class where I try to write a condition (this code is in the render method):

 if (arrayBullet.size != 0) { for (num = 0; num < arrayBullet.size; num++) { if (isShot) { arrayBullet.get(num).updateRight(); } if (!isShot) { arrayBullet.get(num).updateLeft(); } 

And the actual shot itself is played in the method when the button is pressed:

  byte i = 0; if(facesRight){ isShot=true; } else { isShot=false; } arrayBullet.add(new Bullet(new Rectangle(position.x, position.y + 0.3f, 0.7f, 0.7f), 0.3f)); 

All this is done using the ArrayList<Bullet> collection ArrayList<Bullet>

  • If you have a variable that is responsible for the direction of the shot is called isFire , then the unfolding bullets are not your main problem :). - Igor
  • bullet speed can be negative, then when adding speed to the coordinate the bullet will fly to the left. two different update is not needed. - zRrr
  • @Igor I'm new to programming I think forgivable. - dev3java
  • @zRrr Could you write how it should look like? reading your comment does not quite understand. I'm sorry that it’s so ... I’m just learning to program more ... I need the bullets to fly along their coordinates and don’t know about each other ... - dev3java

1 answer 1

The movement of the bullet after the shot should not depend on the variables belonging to the class player.

 public class Bullet { private Rectangle rect; private float velocity; public Bullet(Rectangle rect, float velocity) { this.rect = rect; this.velocity = velocity; } public void move() { rect.x += velocity; } public Rectangle getRect() { return rect; } public float getVelocity() { return velocity; } } for (num = 0; num < arrayBullet.size; num++) { arrayBullet.get(num).move(); } arrayBullet.add(new Bullet( new Rectangle(position.x, position.y + 0.3f, 0.7f, 0.7f), facesRight? 0.3f : -0.3f )); 

or

 Rectangle bulletRectangle = new Rectangle(position.x, position.y + 0.3f, 0.7f, 0.7f); float bulletVelocity = facesRight? 0.3f : -0.3f; arrayBullet.add(new Bullet(bulletRectangle, bulletVelocity)); 
  • nightmare! everything is working!!! Thanks you! Igor, how many years have you been programming? How did you know that this is the way to write code ?? - dev3java
  • and another question about this code ... I didn’t even know that such a condition could be written inside the constructor! facesRight? 0.3f: -0.3f) I broke my head for a week over the condition of how to write and here it is !!! - dev3java
  • @AndreyKonstantinovich - Andrey (nothing without middle name?), Defining the parameters of a function (in this case, the designer) in-line or in advance is a matter of style. Do not worry, try to write the code as easy as possible and remember the physics of the process. Successes in programming. - Igor
  • middle name here itself stuck like that)) well ... thanks again for the help! You too have all the best ... Happy :) - dev3java