How to make something shooting by his self at certain moment

edited July 2014 in How To...

Hi there, i'm making an animation where something comes across the screen, from one side to the other, and it shoots something whenevr you press the Space bar... What i'd like to do is that this thing shoots something by his self at a random moment and i'm really stuck with it... Here is what i got so far:

    Fantasma fantasma;
    Bala bala;
    Timer timer1;
    void setup() {
      size(700, 500);
      noFill();
      smooth();
      noStroke();
      ellipseMode(CENTER);
      timer1 = new Timer(int(random(30000, 35000)));
      timer1.start();
      PImage f = loadImage("fantasma3.png");
      bala =new Bala(-1000,-2000);
      fantasma = new Fantasma(-200, 40, f);
      fantasma.active=true;
    }

    void draw() {
      background(#07283E);
      bala.display();
      bala.update();
      fantasma.update();
      fantasma.display();
      if (timer1.isFinished()) {
        fantasma.fantasmaX=random(-1800, -1000);
        fantasma.active=true; 
        timer1.start();
      }

    }

    void keyPressed() {
      if (fantasma.fantasmaX>0 && fantasma.fantasmaX<width) {  
        if (key==' ') {
          bala = new Bala(fantasma.fantasmaX+70, fantasma.fantasmaY+80);
        }
      }
    }

    class Bala {
      float x, y, speedX, speedY;
      boolean active;

      //constructor vacío
      public Bala() {
        this.speedX=2;
        this.speedY=2;
        this.active=true;
      }
      public Bala(float nx, float ny) {
        this.x=nx;
        this.y=ny;
        this.speedX=2;
        this.speedY=2;
        this.active=true;
      }

      public void update() {
        if (active) {
          x+=speedX;
          y+=speedY;
        }
        if (x>width) {
          speedX=speedX*-1;
        }
        if (y>height) {
          speedY=speedY*-1;
        }
        if(y<0){
          speedY=speedY*-1;
        }
      }
      public void display() {
        fill(255);
        ellipse(x, y, 20,20);
      }

    }

    class Fantasma {
      PImage fantasma;
      float fantasmaX, fantasmaY, noiseY=0;
      float velocidad=2.6;
      boolean active;

      public Fantasma(float x, float y, PImage f) {
        this.fantasmaX=x;
        this.fantasmaY=y;
        this.fantasma=f;
        this.active=true;
      }

      public void update() {
        if (active) {
          fantasmaX+=velocidad;
          fantasmaY=noise(noiseY)*40;
            noiseY+=0.02;
          if (fantasmaX>width+20) {
            active=false;
          }
        }
      }
      public void display() {
        image(fantasma,fantasmaX,fantasmaY);
      }
    }

I dunno if this problem was already asked or even solved in this forum so i apologize if it's so, anyway i didn't find any similar topic... Any help or idea would be much appreciated.

Thx!

Answers

  • Any suggestion or reference to other code could also help... :-/

  • edited July 2014 Answer ✓

    this prints hit in 70 %

    void draw() {  
      shootAtRandom();
    }
    
    void shootAtRandom() {
      if (int(random(100)) > 70) {
        println("Hit "+millis());
      }
    }
    

    insetad of println you could say

    bala = new Bala(fantasma.fantasmaX+70, fantasma.fantasmaY+80);

  • Hi Chris, thx so much for taking your time in replaying...i also tried that and found out that if ,let's say, it shoots two times the first bullet disappears when the second comes out and as the original idea was having only one bullet per shot i discarded that option... Anyway, i'll create an ArrayList to store the bullets so i could have a few of them on screen at the same time and in the meantime i'll try to work out something to have only one shoot at a time...

    Thx dude, apreciate your help on this kinda silly beguinner problem.

  • edited July 2014 Answer ✓

    I see.

    I couldn't run your code because I don't have your Timer class

    Anyway, without using an ArrayList

    you just have Bala bala;

    when bala leaves the screen ( <10 || > width+10 ) you could have a flag

    bala.isDead = true; 
    

    so we only want to start a new bullet when the old one is dead, right?

    in the class (in the constructor of the class say isDead = false; in the class have boolean isDead; ).

    and then

    void draw() { 
      shootAtRandom();
    }
    
    void shootAtRandom() {
      if (bala.isDead && int(random(100)) > 70) {
        println("Hit "+millis());
        bala = new Bala(fantasma.fantasmaX+70, fantasma.fantasmaY+80);
      }
    }
    

    ;-)

  • Ah! my friend... That was a clever one...tracking the bullet's x coordinate to see if it's on the screen ... Once again thx so much for your time and help!... and sorry for missing the Timer class i completely forgot about that one... : - /

  • Answer ✓

    You're welcome!

  • When you would use an ArrayList, you could use add and remove

    ArrayList is of flexible length

Sign In or Register to comment.