Objects

If i have an array of object from this

class Shot {
  PVector loc;
  PVector vel;  
  int r,g,b; 
  Shot (PVector loc, PVector direction, int r, int g, int b) {
    this.loc=new PVector ();
    vel=new PVector();
    this.loc = loc.get();
    vel= direction.get();
    vel.normalize();
    vel.mult(9);
    this.r=r;
    this.g=g;
    this.b=b;
  }
  void displayShot() {    
    loc.add(vel);
    noStroke();
    fill(r,g,b);
    ellipse(loc.x, loc.y, 4, 4);    
  }  
}

and i want to shoot them by ckicking. How could i make it work without shooting too many of those(by to many i mean one each draw). because my game works perfect but it shoots a lot, its nice but THERE ARE TO MANY GOING OUT.

Answers

  • edited August 2015

    well i dont know how to show that class nicely. here is the method that shoots

         void shoot(boolean d) { 
            if (!d) { 
              if (ind<tamT) {
                shots[ind]=new Shot (shoter.loc, shoter.dif, 230, 150, 30);
                ind++;
              } else {
                ind=0;
              }
            } else {
              antiDif = shoter.dif.get();
              antiDif.mult(-1);   
              if (ind<tamT) {
                shots[ind]=new Shot (shoter.loc, antiDif, 30, 150, 230);
                ind++;
              } else {
                ind=0;
              }
            }
          }
    
  • the d boolean goes for the click (left or right)

  • Could you post the entire code nicely formatted (like your second post)?

  • i dont even know how the second happend

  • edited July 2015
    class Shot {
      PVector loc;
      PVector vel;  
      int r,g,b; 
      Shot (PVector loc, PVector direction, int r, int g, int b) {
        this.loc=new PVector ();
        vel=new PVector();
        this.loc = loc.get();
        vel= direction.get();
        vel.normalize();
        vel.mult(9);
        this.r=r;
        this.g=g;
        this.b=b;
      }
      void displayShot() {    
        loc.add(vel);
        noStroke();
        fill(r,g,b);
        ellipse(loc.x, loc.y, 4, 4);    
      }  
    }
    
  • that was the best i could

  • edited August 2015

    i forgot to put where the methot is used,

     if (lM && !rM) {    
          mButton=false;    
          shoot(mButton);    
        } else if (rM && !lM) {    
          mButton=true;    
          shoot(mButton);    
        }
    

    lM && rM=true when mousePressed()

    =false when mouseReleased()

  • edited July 2015 Answer ✓

    please read the sticky post at the top of the forum about formatting code. then go back and fix your comments. i've done the first one for you.

    http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text

  • For shots you may use an ArrayList instead of simple array, as it helps to maintain its size dynamically. You may add a shot to the ArrayList with a key press and delete it from list when it goes off-screen.

  • i ve just solved my problem by doing this

    t++;
    if (t==preT*8) {
    preT++;
    }
    

    for each draw it sums one and each time it sums up to 8 it shoots

  • nice work

  • also this works better i think.

         t--;
              if (t==0) {
                t=8;
    create=new Shots();
        }
    
  • Also Ater, i dont really know how to use well the ArrayList. I made my entire game using simple arrays :/

  • edited July 2015 Answer ✓

    So why don't you copy the "Fireworks"'s example.
    It uses an array and doesn't launch another Firework when it doesn't find 1 isInactive among them.

  • its better true. this way works but i will try to improve it to that way because i just put a shotgun that creates 5 objects an thats really messy.

    thanks

  • Answer ✓

    I think the initial over-shooting is caused by the use of the variable mousePressed instead of the function mousePressed()

    The latter is counted only one every time whereas the first is ounted again and again - look at the reference

    Chrisir

  • i ve used a boolean that s true when mouse pressed and false when mouse released. with the void mousePressed() anda void mouseReleased()

  • Answer ✓

    Ok, this is different from what I thought

    But still wrong. Just use shoot in mousePressed.

    The way you do it now generates multiple shoots because it's multiple cycles of draw till you release the mouse

  • Yeah i know, i want to let the user have his mouse pressed and shot automaticaly, but not so fast as it was before. ive fixed anyways. thanks :D i will share my game later when its "finished"...

  • Answer ✓

    you could also use a timer with millis()

  • I DID NOT KNOW THAT :D thanks a lot :D:D:D:D:D:D:D (i really didn t know, i ve started with processing 2 months ago :/)

Sign In or Register to comment.