Trigger/Bullet

edited March 2014 in Programming Questions

I am trying to make a trigger like ENTER or spacebar to shoot a bullet at the other side of the screen. How can i do this?

Answers

  • edited March 2014

    I did an online example. Check that out: (*)

    http://studio.processingtogether.com/sp/pad/export/ro.9FioSBFuQzD8V/latest

    /**
     * Bullet Fire Example (v1.02)
     * by GoToLoop (2014/Mar)
     *
     * forum.processing.org/two/discussion/3470/triggerbullet
     *
     * studio.processingtogether.com/sp/pad/export/ro.9FioSBFuQzD8V/latest
     */
    
    static final int BW = 030, BH = 010, BS = 010;
    int bx, by;
    boolean isShooting;
    
    void setup() {
      size(600, 150, JAVA2D);
      frameRate(60);
      smooth(4);
      rectMode(CORNER);
    
      fill(#FF0000);
      stroke(0);
      strokeWeight(2);
    
      by = height-BH >> 1;
    }
    
    void draw() {
      background(0300);
      if (isShooting)  animateBullet();
    }
    
    void keyPressed() {
      final int k = keyCode;
      if (!isShooting && k == ENTER | k == RETURN | k == ' ')  startBullet();
    }
    
    void mousePressed() {
      if (!isShooting & mouseButton == LEFT)  startBullet();
    }
    
    void startBullet() {
      isShooting = true;
      bx = -BW;
    }
    
    void animateBullet() {
      rect(bx += BS, by, BW, BH);
      if (bx > width)  isShooting = false;
    }
    
  • Closing this thread, as it is duplicate of the other one.

This discussion has been closed.