How to create (Spawn) multiple rectangles?

Asked

Viewed 133 times

2

I’m creating a little game Jframe where I need to create several rectangles that will be the projectiles/bullets of my character(main/main) then I created the following class:

Bullet Class

package dannark;

import java.awt.Color;
import java.awt.Graphics;

public class balas {
    int x,y,lar,alt;
    String dir;

    public balas(int x, int y, int lar, int alt, String dir){
        this.x = x;
        this.y = y;
        this.lar = lar;
        this.alt = alt;
        this.dir = dir;
    }

    public void draw(Graphics bbg){
        if(dir.equals("right")){
            x += 10;
        }else{
            x -= 10;
        }
        bbg.setColor(Color.yellow);
        bbg.fillRect(main.camX+x,  main.camY+y, lar, alt);
    }
}

More like I can call it in my main class so that when I press the button C She always fires a new ball?

Main class

public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == e.VK_C){
            //o que fazer aqui para spawnar varios novos retangulos?
        }

    }
  • In which package is the Main class? You managed to overwrite the method keyPressed() correctly?

  • in the dannark package, but actually Voce doesn’t need to be based on my code, I would just need a luz how to do this so that I can adapt myself later :) The codes I showed above were supposed to be examples but I think I messed up a little bit.

  • OBS The keyPressed() It works perfectly the problem is that the way I did I can shoot only one projectile at a time...

  • How many projectiles should I shoot? Wouldn’t it be the case to make a for?

  • as much as it is necessary, every time I shoot I shoot 1 time, but if the user prices 100 times in a second for example, I wanted 100 projectiles to come out. If the case were as it would be?

  • Now I understand, but I find it strange not to shoot several. Can you tell what is the "limit" of balls that shoots? Example: if press 2 times in a second is ok, more than that gives dick. O for I suggested when I hadn’t quite understood your question, forget it.

  • Do not see well, for now I would not like to impose limits.. the problem is that I do not know how to draw more than one rectangle... In other words I instanceio the bullets class: bullet = new bullet(x,y-25,6,3,direction); and then somewhere I draw her bullet.draw(main.bbg); and when I Preciono C eu Seto the position for mega. x and mega. y that is my main character.

Show 3 more comments

1 answer

1

Hey guys I was able to find a solution, just use an arraylist to store my various instances of my same class. This site here helped me: http://www.guj.com.br/java/75460-instanciar-objetos-dinamicamente

In other words, I first created the array:

   ArrayList<balas> bala = new ArrayList();  

2nd Then I saved the instances inside the array

  if(shoot){
      bala.add(new balas(x,y-25,6,3,direcao)); // instancia um novo Objeto e salvo na array
  }

And to get the value of each one was just to do so:

  for(int i = 0; i < bala.size(); i++){
      bala.get(i).draw(main.bbg);
  }

The result was:

Multiplos shoots

Browser other questions tagged

You are not signed in. Login or sign up in order to post.