Using KeyPressed to increase a counter [Fixed]
in
Programming Questions
•
7 months ago
Hi,
I have the following code and I am trying to use KeyPressed to increase a counter. I would then like to use the counter within an array.
The code has random spheres moving around the screen and I am working on creating 'attrators' which I can place using my mouse one after the other.
The problem:
When I run the code, as I press 'CONTROL' to increase my counter, that works fine and using print I can see the counter increase. However, as soon as I right click to active mousePressed and hence use the counter, clicking 'CONTROL' does not increase my counter anymore.
Can anyone help or point me in the right direction.
This is part of an experiment using Daniel Shiffman's Nature of Code.
Code:
- Mover[] m = new Mover[10];
- Attractor[] a = new Attractor[2];
- int x = 0; //counter
- void setup() {
- size(600, 600, P3D);
- sphereDetail(10);
- background(255);
- for (int i = 0;i < m.length; i++) {
- m[i] = new Mover();
- }
- for (int i = 0;i < a.length; i++) {
- a[i] = new Attractor();
- }
- }
- void draw() {
- lights();
- background(255);
- if (mousePressed) {
- a[x].update();
- print(x);
- }
- for (int i =0;i < m.length; i++) {
- m[i].update();
- m[i].checkEdges();
- m[i].display();
- }
- for (int i =0;i < a.length; i++) {
- if(a[i].location.mag() != 0) {
- a[i].display(); for (int j = 0; j < m.length; j++) {
- a[i].attract(m[j]);}
- }
- }
- }
- void keyPressed() {
- if( key == CODED ) {if (keyCode == CONTROL ) {x+=1;print(x);}}
- if(x>10) {x=0;}
- loop();}
1