Action on mousepressed
in
Programming Questions
•
1 year ago
I've been going through the beginner tutorials and was hoping someone could set me in the right direction with the problem I am having.
I would like to 'shoot' a rectangle across the screen when the mouse is pressed (kind of like the car example, if anyone has gone throught the tutorials). Right now, I can get the rectangle across the screen while the mouse is pressed the entire time, but I'd like to just click once and have it go across...
I wasn't using the void mousePressed() method until I started looking through the forums. I've moved stuff around and at this point the rectangle isn't moving at all.
- Bullet myBullet1;
- Bullet myBullet2;
- void setup() {
- size(200,200);
- myBullet1 = new Bullet(color(255,0,0),100,0,2);
- myBullet2 = new Bullet(color(200,12,20),50,0,25);
- }
- void draw() {
- background(255);
- myBullet1.display();
- }
- class Bullet {
- color c;
- float xpos;
- float ypos;
- float yspeed;
- //constructor
- Bullet(color tempC, float tempXpos, float tempYpos, float tempYspeed) {
- c = tempC;
- xpos = tempXpos;
- ypos = tempYpos;
- yspeed = tempYspeed;
- }
- void display() {
- stroke(0);
- fill(c);
- rectMode(CENTER);
- rect(xpos,ypos,40,10); //20,10 give width and height
- }
- void mousePressed() {
- shoot();
- }
- void shoot() {
- ypos = ypos + yspeed;
- if (ypos > height) {
- ypos = 0;
- }
- }
- }
- void draw() {
- background(255);
- myBullet.display();
- if (mousePressed) {
- myBullet.shoot();
- }
- }
- .
- .
- .
- void shoot() {
- ypos = ypos + yspeed;
- if (ypos > height) {
- ypos = 0;
- }
- }
1