how to do inetarcction

pppppp
edited November 2017 in Questions about Code
  ArrayList bales;
Bala bala1;

ArrayList soldats;
Soldat soldat1;
boolean matar= false;

void setup() {
size(600, 500);
stroke(255);
fill(255, 255, 255);
bales = new ArrayList();
bala1 = new Bala();

soldats= new ArrayList();
soldat1 = new Soldat();

bales.add( bala1 );
soldats.add (soldat1);
}

void draw() {
background(51);
line(300, 500, mouseX, 2 * mouseY);
strokeWeight(mouseY/10);

for ( int i = 0; i < bales.size(); i++) {
bala1 = (Bala)bales.get(i);
bala1.balesmoviment();

for ( int i = 0; i < bales.size(); i++) {
soldat1 = (Soldat)soldats.get(i);
soldat1.soldatsmoviment();
}
}

if (balapx = soldatpx) {
matar=true; // matar is kill, when bala collision rectangle (soldat=soldier) soldier die (dissapear)


class Bala {
float balapx, balapy;
float balavx, balavy;
Bala() {//creamos una bala
balapx = width/2.0;
balapy = height;
balavx = map(mouseX, 0, width, -10, 10);
balavy = -10;
}
void balesmoviment() {
balapx += balavx;
balapy += balavy;
ellipse( balapx, balapy, 10, 10);
}
//================================================================
class Soldat { // Here the problem, how to make rectangles that move and avance in the map
float soldatpx, soldatpy;
float i;
Soldat() {//creamos unsoldat
soldatpx = width+i;
soldatpy = 0;
}
void soldatsmoviment() {
soldatpx +=i;

rect( soldatpx, balapy, 10, 20);
}
}
}
}


void mousePressed() {
if ( bales.size() > 9 ) bales.remove(0);
bales.add( new Bala() );
}

Don't do pro things

«1

Answers

  • You'll have to be more specific with what you're asking.

  • if (balapx = soldatpx) {
      matar=true; // matar is kill, when bala collision rectangle (soldat=soldier) soldier die (dissapear)
    

    when 1 array touch another array the rect dissaper

  • i wanna to shoot rectangles and when the balls(bullets) impact on rectangles, the rect die

  • Loop over all the objects in one ArrayList.

    Within that loop, loop over all the objects in the other ArrayList.

    Within both of those loops, you have two objects that you are working with. Using the above links, determine if they collide. If they do, whatever happens when they collide can happen to both of the objects you are currently working with.

    If you're going to remove objects from their ArrayList, the trick is to loop over the objects in reverse.

  • please tf guy 44, is the final question.

  • pppppp
    edited November 2017

    How I can change to make it work

  • edited November 2017

    Your code is a mess. You need to learn how to pair up curly brackets ({}) properly. Right now you are defining your Bala class inside your draw() function. What worries me the most is that you don't realize this. It shows me you have a basic lack of understanding about code and functions. Do you really need a lesson in syntax?

  • Use command-T in PDE to auto-indent and clean up code -- this will make it obvious that, for example, your class is inside draw.

    Reading the error messages in the errors tab will also be helpful in many cases if your nesting is off....

  • Ok, im gonna to correct them, I wrote this so fast.

  • Li8ke this???

    ArrayList soldats;//I have to separe this???????????
    Soldat soldat1;
    boolean matar= false;
    
    ArrayList bales;//I have to separe this???????????
    Bala bala1;
    
    
    void setup() {
      size(600, 500);
      stroke(255);
      fill(255, 255, 255);
      bales = new ArrayList();
      bala1 = new Bala();
    
      soldats= new ArrayList();//there is the other class
      soldat1 = new Soldat();
    
      bales.add( bala1 );
      soldats.add (soldat1);
    }
    
    void draw() {
      background(51);
      line(300, 500, mouseX, 2 * mouseY);
      strokeWeight(mouseY/10);
    
      for ( int i = 0; i < bales.size(); i++) {
        bala1  = (Bala)bales.get(i);
        bala1.balesmoviment();
    
        for ( int i = 0; i < bales.size(); i++) {
          soldat1  = (Soldat)soldats.get(i);
          soldat1.soldatsmoviment();
        }
      }
    
      if (balapx = soldatpx) {
        matar=true; // matar is kill, when bala collision rectangle (soldat=soldier) soldier die (dissapear)
      }
    }
    
    class Bala {
      float balapx, balapy;
      float balavx, balavy;
      Bala() {//creamos una bala
        balapx = width/2.0;
        balapy = height;
        balavx = map(mouseX, 0, width, -10, 10);
        balavy = -10;
      }
      void balesmoviment() {
        balapx += balavx;
        balapy += balavy;
        ellipse( balapx, balapy, 10, 10);
      }
      //================================================================
      class Soldat { // Here the problem, how to make rectangles that move and avance in the map
        float soldatpx, soldatpy;
        float i;
        Soldat() {//creamos unsoldat
          soldatpx = width+i;
          soldatpy = 0;
        }
        void soldatsmoviment() {
          soldatpx +=i;
    
          rect( soldatpx, balapy, 10, 20);
        }
      }
    
    
    
    
      void mousePressed() {
        if ( bales.size() > 9 ) bales.remove(0);
        bales.add( new Bala() );
      }
    }
    
  • sorry, here

    ArrayList bales;
    Bala bala1;
    
    ArrayList soldats;
    Soldat soldat1;
    boolean matar= false;
    
    void setup() {
      size(600, 500);
      stroke(255);
      fill(255, 255, 255);
      bales = new ArrayList();
      bala1 = new Bala();
    
      soldats= new ArrayList();
      soldat1 = new Soldat();
    
      bales.add( bala1 );
      soldats.add (soldat1);
    }
    
    void draw() {
      background(51);
      line(300, 500, mouseX, 2 * mouseY);
      strokeWeight(mouseY/10);
    
      for ( int i = 0; i < bales.size(); i++) {
        bala1  = (Bala)bales.get(i);
        bala1.balesmoviment();
    
        for ( int i = 0; i < bales.size(); i++) {
          soldat1  = (Soldat)soldats.get(i);
          soldat1.soldatsmoviment();
        }
      }
    
      if (balapx = soldatpx) {
        matar=true; // matar is kill, when bala collision rectangle (soldat=soldier) soldier die (dissapear)
      }
    }
    
    class Bala {
      float balapx, balapy;
      float balavx, balavy;
      Bala() {//creamos una bala
        balapx = width/2.0;
        balapy = height;
        balavx = map(mouseX, 0, width, -10, 10);
        balavy = -10;
      }
      void balesmoviment() {
        balapx += balavx;
        balapy += balavy;
        ellipse( balapx, balapy, 10, 10);
      }
      //================================================================
      class Soldat { // Here the problem, how to make rectangles that move and avance in the map
        float soldatpx, soldatpy;
        float i;
        Soldat() {//creamos unsoldat
          soldatpx = width+i;
          soldatpy = 0;
        }
        void soldatsmoviment() {
          soldatpx +=i;
    
          rect( soldatpx, balapy, 10, 20);
        }
      }
    
    
    
      void mousePressed() {
        if ( bales.size() > 9 ) bales.remove(0);
        bales.add( new Bala() );
      }
    }
    
  • I can do it in this way

    ArrayList bales;
    Bala bala1;
    PImage img;
    
    void setup() {
      size(800, 450);
      stroke(255);
      fill(255, 255, 255);
      bales = new ArrayList();
      bala1 = new Bala();
      img = loadImage("pa.jpg");
      bales.add( bala1 );
    }
    
    void draw() {
      image(img, 0, 0);
    
      line(400, 450, mouseX, 2 * mouseY);
      strokeWeight(mouseY/10);
    
    
      for ( int i = 0; i < bales.size(); i++) {
        bala1  = (Bala)bales.get(i);
        bala1.balesmoviment();
      }
    }
    
    class Bala {
      float balapx, balapy;
      float baladx, balady;
      Bala() {//creamos una bala
        balapx = width/2.0;
        balapy = height;
        baladx = map(mouseX, 0, width, -10, 10);
        balady = -10;
      }
    
      void balesmoviment() {
        balapx += baladx;
        balapy += balady;
        ellipse( balapx, balapy, 2, 2);
      }
    }
    
    
    void mousePressed() {
      if ( bales.size() > 9 ) bales.remove(0);
      bales.add( new Bala() );
    }
    {
      ArrayList soldats;
      Soldat soldat1;
    
      void setup() {
        soldats= new ArrayList();
        soldat1 = new Soldat();
        soldats.add (soldat1);
      }
      void draw() {
        for ( int i = 0; i < bales.size(); i++) {
          soldat1  = (Soldat)soldats.get(i);
          soldat1.soldatsmoviment();
        }
    
        if (balapx = soldatpx) {
          matar=true; 
    
          class Soldat { // Here the problem, how to make rectangles that move and avance in the map
            float soldatpx, soldatpy;
            float i;
            Soldat() {//creamos unsoldat
              soldatpx = width+i;
              soldatpy = 0;
            }
            void soldatsmoviment() {
              soldatpx +=i;
    
              rect( soldatpx, balapy, 10, 20);
            }
          }
        }
      }
    }
    
  • edited November 2017

    Look at this code you have in draw():

      for ( int i = 0; i < bales.size(); i++) {
        bala1  = (Bala)bales.get(i);
        bala1.balesmoviment();
    
        for ( int i = 0; i < bales.size(); i++) {
          soldat1  = (Soldat)soldats.get(i);
          soldat1.soldatsmoviment();
        }
      }
    
      if (balapx = soldatpx) {
        matar=true;
      }
    

    It's getting close. Except there are three problems.

    1) You iterate over the Soldats inside the loop for the Balas - essentially, you move each Soldat as many times as you have Balas. For example, if you have ten Balaes, then each time this runs, each Soldat will move ten times! You need to have ONE loop that moves all the Balas. Then, AFTER that loop, you need a SECOND loop that moves all your Soldats.

    2) This is why you were close: You do have two loops where one is inside the other. But you don't need nestled loops for movement - you need them to check for collisions. The first loop starts, and you are looking at the first Bala. You know which Bala it is because you are get()ting it from the ArrayList of them and storing it in the bala1 variable. Next, you do need to loop over every Soldat. In side that second loop, you will (on the first time it loops) have the first Soldat stored in the soldat1 variable. So now, inside both loops, you need to see if bala1 and soldat1 collide...

    3) You have totally ignored all attempts to point you at examples and explanations that demonstrate rect-circle collision, and you still don't seem to understand how to access class member variables for objects.

    Here's what you should do:

    // Loop over every Bala. For each Bala you look at, do the following:
    ////// Move this balla.
    // That's all you need to do for every Bala.
    
    // Loop over every Soldat. For each Soldat you look at, do the following:
    ////// Move this Soldat.
    // That's all you need to do for every Soldat.
    
    // Loop over every Bala.  For each Bala you look at, do the following:
    ////// Loop over every Soldat. For each Soldat you look at, do the following:
    ////////// Compare the sizes and positions of this Bala with this Soldat.
    ////////////// If they are colliding, do whatever it is when they collide.
    ////// That's all you need to do for every Soldat.
    // That's all you need to do for every Bala.
    

    Remember: If you've put an object in my_object:

    Type my_object = my_objects.get(someNumber);
    

    Then you can access that object's class members directly:

    my_object.x_position = 90;
    
  • edited November 2017

    Also, checking that two objects have the same X position is not a super great way to know if they are colliding. Especially not if you use an assignment operator (=) instead of a equality comparison operator (==). Read the links provided about rect-circle collision. Your rect should have an x position, y position, width, and length. Your ellipse should have an x position, y position, and radius. You will have to check multiple conditions to see if they collide - not just one check that their x positions are EXACTLY the same (which is super unlikely anyway, since they're float variables!).


    This is all super good advice. Here's some more: No one is going to do it for you. You will get the grade you deserve. And this is coming from the person on these forums who is the most likely to give you a full-code solution (Teachers hate him! This one weird trick will amaze you!). You haven't convinced me you know your shit.

    And if you're removing items from your ArrayList, as I've said before, iterate over the objects in that ArrayList in reverse.

  • Vandalise any more of your questions and you will end up being banned. That's three now and I've warned you before.

    All the help all the people here have given you and you keep deleting your text.

  • Yea,yea, the code It s doid by me, under your instructions, and I don't do any copy paste, remember. Your explanation is perfect because you don't give me any example or solution, I liked and i can doid well under your instrictions and your knowledge.

  • "And if you're removing items from your ArrayList, as I've said before, iterate over the objects in that ArrayList in reverse." What Im removing?? where?

  • "You haven't convinced me you know your shit." What are you telling me?

  • Im not the only of the class that you help

  • I can do another way to do balavx ( the direction of the bullets) because I don't know why draw 2 ellipses, and when the mouse change the position, the bullet begins big. I know that is for the map

    map(value, start1, stop1, start2, stop2)

      float balapx, balapy;`
      float balavx, balavy;
      Bala() {//creamos una bala
        balapx = width/2.0;
        balapy = height;
        balavx = map(mouseX, 0, width, -10, 10);
        balavy = -10;
    
  • better

    class Bala {
      float balapx, balapy;
      float balavx, balavy;
      Bala() {//creamos una bala
        balapx = width/2.0;
        balapy = height;
        balavx = map(mouseX, 0, width, -20, 20);
        balavy = -10;
      }
    
  • but the bullet change of the posisition (I don't like map)

  • Better?

    void draw() { background(51); image(img, 0, 0); line(300, 500, mouseX, 2 * mouseY); strokeWeight(mouseY/10);

      for ( int i = 0; i < bales.size(); i++) { // Loop over every Bala. For each Bala you look at, do the following:
        bala1  = (Bala)bales.get(i);////// Move this balla.
        bala1.balesmoviment();// That's all you need to do for every Bala.
      }
      for ( int i = 0; i < bales.size(); i++) {// Loop over every Soldat. For each Soldat you look at, do the following:
        soldat1  = (Soldat)soldats.get(i);////// Move this Soldat.
        soldat1.soldatsmoviment();// That's all you need to do for every Soldat.
      }
    
    // Loop over every Bala.  For each Bala you look at, do the following:
    ////// Loop over every Soldat. For each Soldat you look at, do the following:
    ////////// Compare the sizes and positions of this Bala with this Soldat.
    ////////////// If they are colliding, do whatever it is when they collide.
    ////// That's all you need to do for every Soldat.
    // That's all you need to do for every Bala.
    
    
    if ((balapx = soldatpx) && (balapy = soldatpy)) {
      matar=true; // matar is kill, when bala collision rectangle (soldat=soldier) soldier die (dissapear)
    }
    
  • This is correct, I want soldiers to appear on the right side of the screen in different positions and you have to shoot them.

      class Soldat { // Here the problem, how to make rectangles that move and avance in the map
        float soldatpx, soldatpy;
        float i;
        Soldat() {//creamos unsoldat
          soldatpx = width+i;
          soldatpy = 0;
        }
        void soldatsmoviment() {
          soldatpx +=i;
    
          rect( soldatpx, balapy, 10, 20);
        }
    
  • Your thoughts, like your code, and a mess. They are all over the place. It is impossible to even follow along with what you are trying to do any more.

    Let's try this one last time.


    Software development is a process. Start simple.

    We can start with a blank sketch that defines two kinds of objects:

    class Brick{
    }
    
    class Ball{
    }
    
    void setup(){
      size(400,400);
    }
    
    void draw(){
      background(0);
    }
    

    Run this code. You get a blank sketch. While we have defined two new kinds of objects, we are not using them yet - and that's just fine. There are no details about them anyway.

  • edited November 2017

    We can add some details about our objects. Imagine that you are a Brick. What do you need to know about yourself to be a good brick?

    You might need to know where in the world you are. That is your position.

    You might need to know how big you are. That is your size.

    You might need to know what color you are.

    You might need to know if you exist at all!

    We can now add these details about what a brick is to the Brick class:

    class Brick{
      float x,y; // Position.
      float w,h; // Size.
      color c; // Color.
      boolean e; // Exists.
    }
    
    class Ball{
    }
    
    void setup(){
      size(400,400);
    }
    
    void draw(){
      background(0);
    }
    
  • edited November 2017

    Next, what do you need to know to be a good Ball? A position, yes. A color, sure. Size? A Ball's size is just one value - its radius. And again, a boolean to remember if it even exists.

    So we can add those details to the Ball class:

    class Brick{
      float x,y;
      float w,h;
      color c;
      boolean e;
    }
    
    class Ball{
      float x,y; // Position.
      float r; // Radius.
      color c;
      boolean e;
    }
    
    void setup(){
      size(400,400);
    }
    
    void draw(){
      background(0);
    }
    
  • edited November 2017

    Okay. Go back to thinking you are a Brick. Now that you know your position, size, color, and if you exist, what do you look like? Chances are good that you are some type of colored rectangle. So we should add a method to the Brick class that allows us to draw a Brick.

    class Brick{
      float x,y;
      float w,h;
      color c;
      boolean e;
      void draw(){
        if(e){
          fill(c);
          rect(x,y,w,h);
        }
      } 
    }
    
    class Ball{
      float x,y;
      float r;
      color c;
      boolean b;
    }
    
    void setup(){
      size(400,400);
    }
    
    void draw(){
      background(0);
    }
    

    Notice that I am keeping the indenting nice and making sure that my curly brackets all match up.

  • Back to the Ball. Imagine you are a ball. What do you look like? An ellipse, yes. So add a function that draws the Ball.

    class Brick {
      float x, y;
      float w, h;
      color c;
      boolean e;
      void draw() {
        if (e) {
          fill(c);
          rect(x, y, w, h);
        }
      }
    }
    
    class Ball {
      float x, y;
      float r;
      color c;
      boolean e;
      void draw() {
        if (e) {
          fill(c);
          ellipse(x, y, r, r);
        }
      }
    }
    
    void setup() {
      size(400, 400);
    }
    
    void draw() {
      background(0);
    }
    

    Notice that the sketch still doesn't show any Bricks or Balls. This is because the main draw() function is not telling any Bricks or Balls to be drawn. Also, do not get confused by the fact that there are three draw() function! One is the main draw() function, which draws the entire sketch. The other two are inside classes, and allow objects of those classes to be drawn.

  • edited November 2017

    Let's focus on the Bricks again now, and see if we can actually get one to be drawn.

    We should actually create a Brick.

    Then we should draw that Brick.

    Here goes:

    class Brick {
      float x, y;
      float w, h;
      color c;
      boolean e;
      void draw() {
        if (e) {
          fill(c);
          rect(x, y, w, h);
        }
      }
    }
    
    class Ball {
      float x, y;
      float r;
      color c;
      boolean e;
      void draw() {
        if (e) {
          fill(c);
          ellipse(x, y, r, r);
        }
      }
    }
    
    Brick a_brick;
    
    void setup() {
      size(400, 400);
      a_brick = new Brick();
    }
    
    void draw() {
      background(0);
      a_brick.draw();
    }
    

    Where is the brick? if you debug this sketch, you will discover that the brick doesn't appear because both it's width and length are 0 (and so it is not drawn). We should make sure we set the values for out brick a little better.

  • Behold:

    class Brick {
      float x, y;
      float w, h;
      color c;
      boolean e;
      void draw() {
        if (e) {
          fill(c);
          rect(x, y, w, h);
        }
      }
    }
    
    class Ball {
      float x, y;
      float r;
      color c;
      boolean e;
      void draw() {
        if (e) {
          fill(c);
          ellipse(x, y, r, r);
        }
      }
    }
    
    Brick a_brick;
    
    void setup() {
      size(400, 400);
      a_brick = new Brick();
      a_brick.x = 20;
      a_brick.y = 30;
      a_brick.w = 60;
      a_brick.h = 40;
      a_brick.e = true;
      a_brick.c = color(200,0,0);
    }
    
    void draw() {
      background(0);
      a_brick.draw();
    }
    

    Once we tell out Brick where it is, how big it is, that it exists, and is red, it appears!

  • You may have noticed that I had to type "a_brick" like six times to set all the values for the brick. Since I do no want to have to type the name of every brick I want to make, I can simply pass parameters to the Brick directly when I call it's constructor function (which we'll add to the Brick class now):

    class Brick {
      float x, y;
      float w, h;
      color c;
      boolean e;
      Brick(float ix, float iy, float iw, float ih, color ic){
        x = ix;
        y = iy;
        w = iw;
        h = ih;
        c = ic;
        e = true;
      }
      void draw() {
        if (e) {
          fill(c);
          rect(x, y, w, h);
        }
      }
    }
    
    class Ball {
      float x, y;
      float r;
      color c;
      boolean e;
      void draw() {
        if (e) {
          fill(c);
          ellipse(x, y, r, r);
        }
      }
    }
    
    Brick a_brick;
    
    void setup() {
      size(400, 400);
      a_brick = new Brick(20, 30, 60, 40, color(200,0,0));
    }
    
    void draw() {
      background(0);
      a_brick.draw();
    }
    

    Great, we still can see our brick!

  • It's just that I've become a mess, but I know all of thiiiisss!!! Why you are teeling me that .from the respect

  • AAAAA, I understand

  • Now let's have lots of Bricks. Since I may eventually want to add or remove many bricks, I will store my bricks in an ArrayList(). Let's switch from having one Brick to many Bricks now.

    class Brick {
      float x, y;
      float w, h;
      color c;
      boolean e;
      Brick(float ix, float iy, float iw, float ih, color ic){
        x = ix;
        y = iy;
        w = iw;
        h = ih;
        c = ic;
        e = true;
      }
      void draw() {
        if (e) {
          fill(c);
          rect(x, y, w, h);
        }
      }
    }
    
    class Ball {
      float x, y;
      float r;
      color c;
      boolean e;
      void draw() {
        if (e) {
          fill(c);
          ellipse(x, y, r, r);
        }
      }
    }
    
    ArrayList<Brick> bricks = new ArrayList();
    
    void setup() {
      size(400, 400);
      bricks.add(new Brick(20, 30, 60, 40, color(200,0,0)) );
      bricks.add(new Brick(20, 130, 40, 30, color(200,0,200)) );
    }
    
    void draw() {
      background(0);
      for(int i = bricks.size()-1; i>=0; i--) bricks.get(i).draw();
    }
    

    Super. As you can see, I can now have as many Bricks as I like.

    Now I take a break, and you bring the Ball class up to speed. Follow the same steps as we did above with the bricks, but for the Ball class. The goal is to be able to have and see many Balls. Post your attempt at doing this for more help.

  • why do you in my code??

  • And now I have to do the velocity of the ball, but we can do in my code please??

    class Brick {
      float x, y;
      float w, h;
      color c;
      boolean e;
      Brick(float ix, float iy, float iw, float ih, color ic) {
        x = ix;
        y = iy;
        w = iw;
        h = ih;
        c = ic;
        e = true;
      }
      void draw() {
        if (e) {
          fill(c);
          rect(x, y, w, h);
        }
      }
    }
    
    class Ball {
      float x, y;
      float r;
      color c;
      boolean e;
      Ball(float mx, float my, float mr, float mc, float me) {
        x = mx;
        y = my;
        r = mr;
        c = mc;
        e = me;
        e = true;
      }
      void draw() {
        if (e) {
          fill(c);
          ellipse(x, y, r, r);
        }
      }
    }
    
    ArrayList<Brick> bricks = new ArrayList();
    
    void setup() {
      size(400, 400);
      bricks.add(new Brick(20, 30, 60, 40, color(200, 0, 0)) );
      bricks.add(new Brick(20, 130, 40, 30, color(200, 0, 200)) );
    }
    
    void draw() {
      background(0);
      for (int i = bricks.size()-1; i>=0; i--) bricks.get(i).draw();
    }
    
    ArrayList<Ball> balls = new ArrayList();
    
    void setup() {
      size(400, 400);
      balls.add(new Brick(20, 30, 60, 40, color(200, 0, 0)) );
      balls.add(new Brick(20, 130, 40, 30, color(200, 0, 200)) );
    }
    
    void draw() {
      background(0);
      for (int i = balls.size()-1; i>=0; i--) balls.get(i).draw();
    }
    
  • edited November 2017

    You just posted this code:

    class Brick {
      float x, y;
      float w, h;
      color c;
      boolean e;
      Brick(float ix, float iy, float iw, float ih, color ic) {
        x = ix;
        y = iy;
        w = iw;
        h = ih;
        c = ic;
        e = true;
      }
      void draw() {
        if (e) {
          fill(c);
          rect(x, y, w, h);
        }
      }
    }
    
    class Ball {
      float x, y;
      float r;
      color c;
      boolean e;
      Ball(float mx, float my, float mr, float mc, float me) {
        x = mx;
        y = my;
        r = mr;
        c = mc;
        e = me;
        e = true;
      }
      void draw() {
        if (e) {
          fill(c);
          ellipse(x, y, r, r);
        }
      }
    }
    
    ArrayList<Brick> bricks = new ArrayList();
    
    void setup() {
      size(400, 400);
      bricks.add(new Brick(20, 30, 60, 40, color(200, 0, 0)) );
      bricks.add(new Brick(20, 130, 40, 30, color(200, 0, 200)) );
    }
    
    void draw() {
      background(0);
      for (int i = bricks.size()-1; i>=0; i--) bricks.get(i).draw();
    }
    
    ArrayList<Ball> balls = new ArrayList();
    
    void setup() {
      size(400, 400);
      balls.add(new Brick(20, 30, 60, 40, color(200, 0, 0)) );
      balls.add(new Brick(20, 130, 40, 30, color(200, 0, 200)) );
    }
    
    void draw() {
      background(0);
      for (int i = balls.size()-1; i>=0; i--) balls.get(i).draw();
    }
    

    Did you even try to run it? It has several serious errors. For a start, the parameters to the Ball() constructor aren't all floats, and you don't need that many of them. Then you have duplicate setup() and draw() functions. Plus you should be adding Balls to the balls ArrayList, not Bricks, and you need to pass a matching list of parameters.

    Always make sure that the code you have runs without errors! It's an important property to maintain in your code... that way you know things remain okay!

  • yea yea, but i have to do my game, so i cannot spend time with another stuff. No? please helps me, what can i do

  • edited November 2017

    Fine. Here, write this function:

    boolean collide_check( float balaX, float balaY, float balaR, float soldatX, float soldatY, float soldatW, float soldatH ) {
      /// Returns true if bala and soldat touch, false otherwise.
      // Your code goes here.
    }
    

    You need it.

  • ok I did

  • Let's see it then.

  • ok I m doing

  • Now I havee to change the boolean, but i can go in this direction. I can put inseide the draw?

    ArrayList bales;
    Bala bala1;
    PImage img;
    
    void setup() {
      size(800, 450);
      stroke(255);
      fill(255, 255, 255);
      bales = new ArrayList();
      bala1 = new Bala();
      img = loadImage("pa.jpg");
      bales.add( bala1 );
    }
    
    void draw() {
      image(img, 0, 0);
    
      line(400, 450, mouseX, 2 * mouseY);
      strokeWeight(mouseY/10);
    
    
      for ( int i = 0; i < bales.size(); i++) {
        bala1  = (Bala)bales.get(i);
        bala1.balesmoviment();
      }
    }
    
    class Bala {
      float balapx, balapy;
      float baladx, balady;
      Bala() {//creamos una bala
        balapx = width/2.0;
        balapy = height;
        baladx = map(mouseX, 0, width, -10, 10);
        balady = -10;
      }
    
      void balesmoviment() {
        balapx += baladx;
        balapy += balady;
        ellipse( balapx, balapy, 2, 2);
      }
    }
    
    
    void mousePressed() {
      if ( bales.size() > 9 ) bales.remove(0);
      bales.add( new Bala() );
    }
    {
      ArrayList soldats;
      Soldat soldat1;
    
      void setup() {
        soldats= new ArrayList();
        soldat1 = new Soldat();
        soldats.add (soldat1);
      }
      void draw() {
        for ( int i = 0; i < bales.size(); i++) {
          soldat1  = (Soldat)soldats.get(i);
          soldat1.soldatsmoviment();
        }
    
    
          boolean collide_check( float balaX, float balaY, float balaR, float soldatX, float soldatY, float soldatW, float soldatH ) {
         if (collide_check=1) {
         matar=true; 
    
          class Soldat { // Here the problem, how to make rectangles that move and avance in the map
            float soldatpx, soldatpy;
            float i;
            Soldat() {//creamos unsoldat
              soldatpx = width+i;
              soldatpy = 0;
            }
            void soldatsmoviment() {
              soldatpx +=i;
    
              rect( soldatpx, balapy, 10, 20);
            }
          }
        }
      }
    }
    
This discussion has been closed.