We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I made 4 different sets of keys for 4 max ellipses but they wouldn't move at the same time. I don't know why. Please help me. Thank you.
final static int N1= 1; //players can move at the same time
final static int S1 = 2;
final static int E1 = 4;
final static int W1 = 8; // Speed limit. Round out edges for field (silhouette)
// if time Collision detection
final static int N2 = 16;
final static int S2 = 32;
final static int E2 = 64;
final static int W2 = 128;
final static int N3 = 256;
final static int S3 = 512;
final static int E3 = 1024;
final static int W3 = 2048;
final static int N4 = 4096;
final static int S4 = 8192;
final static int E4 = 16384;
final static int W4 = 32768;
int result, k; //store key input and initial number of players on field
float x1, x2, x3, x4, y1, y2, y3, y4, n, r; //Decleare some useful variables
boolean sketchFullScreen() { //Make full screen
return true;
}
void setup() { //Basic setup
size(displayWidth, displayHeight); //Window size maximum
noCursor(); //Hide mouse cursor
frameRate(60); //set the frame rate, can lower the rate for lower performent pc
result = 0;
k = 1;
n = 8; //initial movement speed
r = 15; //initial radius of bubble
x1 = 2*width/3;
y1 = height/3;
x2 = 2*width/3; //light dot initial position
y2 = 2*height/3;
x3 = width/3;
y3 = height/3;
x4 = width/3;
y4 = 2*height/3;
}
void draw() { //Start drawing
background(0); //Black background(COMPLETELY BLACK)
switch(result) {
case N1:
y1=y1-n;
break;
case E1:
x1=x1+n;
break;
case S1:
y1=y1+n;
break;
case W1:
x1=x1-n;
break; //create movement to the dot by reassigning the coordinates
case N1|E1:
y1=y1-n;
x1=x1+n;
break;
case N1|W1:
y1=y1-n;
x1=x1-n;
break;
case S1|E1:
y1=y1+n;
x1=x1+n;
break;
case S1|W1:
y1=y1+n;
x1=x1-n;
break;
case N2:
y2=y2-n;
break;
case E2:
x2=x2+n;
break;
case S2:
y2=y2+n;
break;
case W2:
x2=x2-n;
break; //create movement to the dot by reassigning the coordinates
case N2|E2:
y2=y2-n;
x2=x2+n;
break;
case N2|W2:
y2=y2-n;
x2=x2-n;
break;
case S2|E2:
y2=y2+n;
x2=x2+n;
break;
case S2|W2:
y2=y2+n;
x2=x2-n;
break;
case N3:
y3=y3-n;
break;
case E3:
x3=x3+n;
break;
case S3:
y3=y3+n;
break;
case W3:
x3=x3-n;
break; //create movement to the dot by reassigning the coordinates
case N3|E3:
y3=y3-n;
x3=x3+n;
break;
case N3|W3:
y3=y3-n;
x3=x3-n;
break;
case S3|E3:
y3=y3+n;
x3=x3+n;
break;
case S3|W3:
y3=y3+n;
x3=x3-n;
break;
case N4:
y4=y4-n;
break;
case E4:
x4=x4+n;
break;
case S4:
y4=y4+n;
break;
case W4:
x4=x4-n;
break; //create movement to the dot by reassigning the coordinates
case N4|E4:
y4=y4-n;
x4=x4+n;
break;
case N4|W4:
y4=y4-n;
x4=x4-n;
break;
case S4|E4:
y4=y4+n;
x4=x4+n;
break;
case S4|W4:
y4=y4+n;
x4=x4-n;
break;
}
if (k>=5) {
k=4;
} //max bumber of players is 4
if (k<0) {
k=0;
} //least amount of players is 0
if (x1<0) {
x1=r/2;
}
else if (x1>displayWidth) {
x1=displayWidth-r/2;
}
if (x2<0) {
x2=r/2;
}
else if (x2>displayWidth) {
x2=displayWidth-r/2;
}
if (x3<0) {
x3=r/2;
}
else if (x3>displayWidth) {
x3=displayWidth-r/2;
}
if (x4<0) {
x4=r/2;
}
else if (x4>displayWidth) {
x4=displayWidth-r/2;
}
if (y1<0) {
y1=r/2;
}
else if (y1>displayHeight) {
y1=displayHeight-r/2;
}
if (y2<0) {
y2=r/2;
}
else if (y2>displayHeight) {
y2=displayHeight-r/2;
}
if (y3<0) {
y3=r/2;
}
else if (y3>displayHeight) {
y3=displayHeight-r/2;
}
if (y4<0) {
y4=r/2;
}
else if (y4>displayHeight) {
y4=displayHeight-r/2;
}
stroke(255); //White light stroke
if (k==1) {
ellipse(x1, y1, r, r);
} //Draw an ellipse at location (x,y) xwith radius of 15 pixels
if (k==2) {
ellipse(x1, y1, r, r);
ellipse(x2, y2, r, r);
}
if (k==3) {
ellipse(x1, y1, r, r);
ellipse(x2, y2, r, r);
ellipse(x3, y3, r, r);
}
if (k==4) {
ellipse(x1, y1, r, r);
ellipse(x2, y2, r, r);
ellipse(x3, y3, r, r);
ellipse(x4, y4, r, r);
}
}
void keyPressed() {
switch(key) {
case('w'):
case('W'):
result |=N1;
break;
case('d'):
case('D'):
result |=E1;
break;
case('s'):
case('S'):
result |=S1;
break; //classify the cases for the key pressed
case('a'):
case('A'):
result |=W1;
break;
}
switch(key) {
case('8'):
result |=N2;
break;
case('6'):
result |=E2;
break;
case('5'):
result |=S2;
break;
case('4'):
result |=W2;
break;
}
switch(key) {
case('t'):
case('T'):
result |=N3;
break;
case('h'):
case('H'):
result |=E3;
break;
case('g'):
case('G'):
result |=S3;
break;
case('f'):
case('F'):
result |=W3;
break;
}
switch(key) {
case('i'):
case('I'):
result |=N4;
break;
case('l'):
case('L'):
result |=E4;
break;
case('k'):
case('K'):
result |=S4;
break;
case('j'):
case('J'):
result |=W4;
break;
}
switch(key) {
case('+'):
case('='):
n++;
break;
case('_'):
case('-'):
n--;
break; //change movement speed
case('['):
case('{'):
r=r-1;
break; //change radius of bubble
case(']'):
case('}'):
r=r+1;
break;
case(':'):
k--;
break; //add and take away players
case('"'):
k++;
break;
}
}
void keyReleased() {
switch(key) {
case('w'):
case('W'):
result ^=N1;
break;
case('d'):
case('D'):
result ^=E1;
break;
case('s'):
case('S'):
result ^=S1;
break; //classify the cases for the key pressed
case('a'):
case('A'):
result ^=W1;
break;
}
switch(key) {
case('8'):
result ^=N2;
break;
case('6'):
result ^=E2;
break;
case('5'):
result ^=S2;
break;
case('4'):
result ^=W2;
break;
}
switch(key) {
case('t'):
case('T'):
result ^=N3;
break;
case('h'):
case('H'):
result ^=E3;
break;
case('g'):
case('G'):
result ^=S3;
break;
case('f'):
case('F'):
result ^=W3;
break;
}
switch(key) {
case('i'):
case('I'):
result ^=N4;
break;
case('l'):
case('L'):
result ^=E4;
break;
case('k'):
case('K'):
result ^=S4;
break;
case('j'):
case('J'):
result ^=W4;
break;
}
switch(key) {
case('+'):
case('='):
n++;
break;
case('_'):
case('-'):
n--;
break;
case('['):
case('{'):
r=r-1;
break;
case(']'):
case('}'):
r=r+1;
break;
}
}
}
Answers
Looking at the code is painful... you should start using arrays... please reference the article From several variables to arrays. Also, your code formatting is a bit funky. Attempting to fix that momentarily...
Used Processing formatting tool (Edit > Auto Format) and then re-pasted the code, selected all of it, and clicked the "code" button...
Ok I fixed it. But using arrays in this situation now is kind of confusing for me at least and wouldn't it mess me up even more?
Perhaps it isn't a good idea to make the switch for this particular project, but it would definitely be a good idea for your next project. Generally, it makes code cleaner and easier to read, both for yourself and for everyone else...
so do you know what I should do now to make 4 ellipses move at the same time?