if else dilemma
in
Programming Questions
•
3 years ago
Right, below is a very short version of the whole code that i'm using:
- PImage background;
PImage opening;
boolean bStop; - color f = color(15,128,223);
color e = color(246,2,19);
color i = color(16,119,233);
color s = color(42,164,99);
color m = color(227,35,39);
color ms = color(196,174,4); - //Classes & goTo
- //Friendly Fighter Class
- class fFighter {
float x; //Current x posn
float y; //Current y posn
float destx; //Destination x posn
float desty; //Destination y posn
float speed; //Speed of movement
float movingAngle; //Angle of movement
boolean selected; //Selected?
boolean moving; //Moving?
boolean irangeon; //Intel Range
boolean mrangeon; //Mil Range
boolean mouseselected; //mouseover change
public fFighter() {
x = random(width);
y = random(height);
selected = moving = false;
}- public fFighter(float startx,float starty) {
x = startx;
y = starty;
speed = 0.02; //Speed of movement
selected = moving = false;
} - public void tick() { //Definition of 'moving'
if(moving) {
x += cos(movingAngle)*speed;
y += sin(movingAngle)*speed;
if(sqrt(sq(destx-x)+sq(desty-y)) <= speed) {
moving = false;
}
} - if(irangeon) {
ellipseMode(CENTER);
noFill();
stroke(i);
ellipse(x,y+5,51,51);
}
else {
} - if(mrangeon) {
noFill();//(s);
stroke(m);
ellipseMode(CENTER);
ellipse(x,y+5,50,50);
}
else {
} - if(selected) { //If selected...
mouseselected = false;
stroke(0);
fill(s);
triangle(x,y,x+5,y+12.5,x-5,y+12.5);
noFill();//(s);
stroke(m);
ellipseMode(CENTER);
ellipse(x,y+5,50,50);
stroke(i);
ellipse(x,y+5,51,51);
} - else if(mouseselected) {
fill(255);
stroke(0);
rectMode(CORNER);
rect(width-(width),height-100,200,100);
fill(0);
text("Fighter",width-(width-15),height-75);
text("Max Speed=0.02",width-(width-15),height-60);
text("IntelRange=50",width-(width-15),height-45);
text("MilRange=50",width-(width-15),height-30);
stroke(0);
fill(ms);
triangle(x,y,x+5,y+12.5,x-5,y+12.5);
}
// ELSE!!!! - // In its original state this overrode all previous colour settings...
else { //If not selected...
stroke(0);
fill(f);
}
triangle(x,y,x+5,y+12.5,x-5,y+12.5); //Shape of unit
} - public void goTo(float dx, float dy) { //Movement code
destx = dx;
desty = dy;
movingAngle = atan2(dy-y,dx-x);
moving = true;
}
} - ArrayList ffighters;
- void setup() {
size(400,400);
ffighters = new ArrayList();
for(int i=0;i<0;i++) {
ffighters.add(new fFighter());
}
} - void draw() {
background(255);
for(int i=0;i< ffighters.size();i++) {
fFighter ff = (fFighter)ffighters.get(i);
ff.tick();
}
} - void keyPressed() {
if (key == '1') { //New unit
ffighters.add(new fFighter(mouseX,mouseY));
}
if(key == 'i') {
for(int i = 0; i < ffighters.size(); i++) {
fFighter ff = (fFighter)ffighters.get(i);
if(ff.irangeon) {
ff.irangeon = false;
}
else {
ff.irangeon = true;
}
}
}
} - void mousePressed() {
boolean ffighterPressed = false;
for(int i = ffighters.size()-1; i>=0;i--) {
fFighter ff = (fFighter)ffighters.get(i);
if(mouseX > ff.x-10 && mouseX < ff.x+10 &&mouseY > ff.y && mouseY < ff.y+10) {
ffighterPressed = true;
if(ff.selected) {
ff.selected = false;
}
else {
ff.selected = true;
}
break;
}
}
}
To set up the problem...
1. Press '1' twice in two different locations in the window. some blue triangles should appear
2. press 'i' and blue circles should appear around the triangles
3. press 1 again in another location and a triangle will appear
4. keep pressing 'i' and the circles toggle between the two and single triangles.
My question is: is there a way of making it so that the circles appear all at once?
I tried the following:
- if(key == 'i') {
for(int i = 0; i < ffighters.size(); i++) {
fFighter ff = (fFighter)ffighters.get(i);
if(!ff.irangeon) {
ff.irangeon = true;
break;
}
else if(ff.irangeon) {
ff.irangeon = false;
}
else {
ff.irangeon = true;
}
}
but sadly it doesn't work. it's getting closer though...
apologies if my question and the details surrounding it aren't very clear. please ask if you need to know something more specific
1