Well this more or less works, though still needs a fair amount of work:
- ArrayList fighters;
- int iR = 255;
- int iG = 0;
- int iB = 0;
- int mR = 0;
- int mG = 255;
- int mB = 0;
- int sR = 0;
- int sG = 0;
- int sB = 255;
- int fR = 255;
- int fG = 255;
- int fB = 0;
- void setup() {
- size(600,400);
- fighters= new ArrayList();
- for (int i=0; i < 20; i++){
- fighters.add(new fFighter());
- }
- }
- void draw() {
- for (int i=0; i < fighters.size(); i++){
- fFighter fighter = (fFighter) fighters.get(i);
- fighter.tick();
- }
-
- mouseOver();
-
-
- }
- // This doesn't belong in the fFighter class!!!
- void mouseOver() {
- for(int i = 0; i < fighters.size(); i++) {
- fFighter ff = (fFighter)fighters.get(i);
- if(mouseX > ff.x-5 && mouseX < ff.x+5 && mouseY > ff.y-5 && mouseY < ff.y+5) {
- ff.mouseselected = true;
- println("over!");
- }
- else {
- ff.mouseselected = false;
- }
- }
- }
- 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(iR,iG,iB);
- ellipse(x,y+5,51,51);
- }
- else {
- }
-
- if(mrangeon) {
- noFill();//(sR,sG,sB);
- stroke(mR,mG,mB);
- ellipseMode(CENTER);
- ellipse(x,y+5,50,50);
- }
- else {
- }
-
- if(mouseselected) {
- stroke(0);
- fill(sR,sG,sB);
- triangle(x,y,x+5,y+12.5,x-5,y+12.5);
- }
- // ELSE!!!!
- else if(selected) { //If selected...
- stroke(0);
- fill(sR,sG,sB);
- triangle(x,y,x+5,y+12.5,x-5,y+12.5);
- noFill();//(sR,sG,sB);
- stroke(mR,mG,mB);
- ellipseMode(CENTER);
- ellipse(x,y+5,50,50);
- stroke(iR,iG,iB);
- ellipse(x,y+5,51,51);
- }
- // In its original state this overrode all previous colour settings...
- else { //If not selected...
- stroke(0);
- fill(fR,fG,fB);
- }
- 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;
- }
- }
First off not seeing how you go about calling that mouseOver code didn't help; but in its original state it didn't make a great deal of sense: you iterated over the whole collection of fighters; which an individual fighter doesn't really know about. Better to have a mouseOver function that checks against itself then it's just a matter of calling it from within tick() which presumably you're calling on all fighters from a loop in draw... I'll leave that to you.
Next up:
- if(irangeon) {
- //snip
- }
- else {
- }
-
- if(mrangeon) {
- //snip
- }
- else {
- }
-
- if(mouseselected) {
- //snip
- }
- else {
- }
-
- if(selected) { //If selected...
- // snip
- }
- // This overrides all previous settings except if selected == true
- else { //If not selected...
- stroke(0);
- fill(fR,fG,fB);
- }
Since all those conditions affect the fill colour you need to chain them together with else ifs in an order of priority. Otherwise the last condition overrides all the others except when selected == true...
Even then, and as you'll see from my 'working' code above, your mouseOver test doesn't quite correspond with the position of the ships...