Class doesn't want to pass on boolean

I'm new to Processing, and I have a problem inside the following code. A made two classes, which I called 'antagonist' and 'protagonist', and I want them to react on each other. They do, or at least the protagonist responds to the antagonist, but when they come in contact, I want the protagonist to move away, based on a boolean stored inside the antagonist (the reason is that I eventually want to multiply the protagonist object, and I want them al to be attracted/distracted a the same time). I tried out a lot, and I found out that the problem is not in the communication from the protagonist to the antagonist, but back from the antagonist to the protagonist. Inside the console you can see this happening: when the 'curdis' (current distance) gets below 10, the a.getappeal() switches from true to false, but my 'appeal' variable within the protagonist class (which is directly related to the a.getappeal()) stays on true. I don't understand why.


Protagonist p;
Antagonist a;

void setup(){
  size(1000,600);
  stroke(0);
  fill(0);
  
  a = new Antagonist();
  p = new Protagonist();

  a.exist();
  p.exist();
}

void draw(){
  //background(255);
  a.display();
  p.think();
  p.move();
  p.display();
  
}



class Antagonist {
  int xpos;
  int ypos;
  
  boolean appeal = true;

  void exist(){
    xpos = (int)random(width);
    ypos = (int)random(height);
  }
  
  void think(){
  }
  
  void move(){
  }
  
  void display(){
    point(xpos, ypos);
  }
  
  int xpos(){return xpos;}
  int ypos(){return ypos;}
  
  boolean getappeal(){
    return appeal;
  }
  
  void setappeal(boolean _appeal){
    appeal = _appeal;
  }
}



class Protagonist {
  int xpos;
  int ypos;
  
  int xintention, yintention;
  
  boolean appeal;
  
  int curdis, newdis;
  
  long storedtime, passedtime;
  
  void exist(){
    xpos = (int)random(width);
    ypos = (int)random(height);
  }
  
  void think(){
    xintention = (int)random(-2,2);
    yintention = (int)random(-2,2);
    newdis = (int)dist(xpos+xintention, ypos+yintention, a.xpos, a.ypos);
    curdis = (int)dist(xpos, ypos, a.xpos, a.ypos);
    passedtime = frameCount - storedtime;
    
    if (curdis < 10){
      a.setappeal(false);  
      storedtime = frameCount;
    }
    
    appeal = a.getappeal();

    if (appeal = true){  
      if(newdis < curdis){
        xintention = xintention * 3;
        yintention = yintention * 3;
      }
    } 
    else if(appeal = false){
      if(newdis > curdis){
        xintention = xintention * 3;
        yintention = yintention * 3;
      }
    }
    
      println("curdis: " + curdis + ", a.getappeal(): " + a.getappeal() + ", appeal: " + appeal);

  }
  
  void move(){
    xpos = xpos + xintention;
    ypos = ypos + yintention;
  }
  
  void display (){
    point(xpos, ypos); 
  }

}

Tagged:

Answers

  • edited March 2014 Answer ✓

    this is wrong

    if (appeal = true){

    you want

    if (appeal == true){

    in fact

    if (appeal){

    is enough

    same for

    same for

    else if(appeal = false){

    must be

    else if(appeal == false){

    or just

    else if( !appeal ){

    or even

    else {

    because a boolean can only have two states.

    Greetings!

  • Answer ✓

    You are not checking appeal correctly it should be '==' instead '='.

    Protagonist p;
    Antagonist a;
    
    void setup(){
      size(1000,600);
      stroke(0);
      fill(0);
    
      a = new Antagonist();
      p = new Protagonist();
    
      a.exist();
      p.exist();
    }
    
    void draw(){
      //background(255);
      a.display();
      p.think();
      p.move();
      p.display();
    }
    
    
    class Antagonist {
      int xpos;
      int ypos;
      boolean appeal;
    
      Antagonist() {
      }
    
      void exist(){
        xpos = (int)random(width);
        ypos = (int)random(height);
      }
    
      void think(){
      }
    
      void move(){
      }
    
      void display(){
        point(xpos, ypos);
      }
    
      int xpos(){return xpos;}
      int ypos(){return ypos;}
    
      boolean getappeal(){
        return appeal;
      }
    
      void setappeal(boolean _appeal){
        appeal = _appeal;
      }
    }
    
    class Protagonist {
      int xpos;
      int ypos;
    
      int xintention, yintention;
    
      boolean appeal;
    
      int curdis, newdis;
    
      long storedtime, passedtime;
    
      void exist(){
        xpos = (int)random(width);
        ypos = (int)random(height);
      }
    
      void think(){
        xintention = (int)random(-2,2);
        yintention = (int)random(-2,2);
        newdis = (int)dist(xpos+xintention, ypos+yintention, a.xpos, a.ypos);
        curdis = (int)dist(xpos, ypos, a.xpos, a.ypos);
        passedtime = frameCount - storedtime;
    
        appeal = a.getappeal();
    
        if (curdis < 100){
          a.setappeal(false);
          storedtime = frameCount;
        } else {
          a.setappeal(true);
        }
    
        if (appeal){  
          if(newdis < curdis){
            xintention = xintention * 3;
            yintention = yintention * 3;
          }
        }
    
        else if(appeal){
          if(newdis > curdis){
            xintention = xintention * 3;
            yintention = yintention * 3;
          }
        }
        println("curdis: " + curdis + ", a.getappeal(): " + a.getappeal() + ", appeal: " + appeal);
      }
    
      void move(){
        xpos = xpos + xintention;
        ypos = ypos + yintention;
      }
    
      void display (){
        point(xpos, ypos); 
      }
    
    }
    
  • to the overall layout / architecture

    maybe you can achieve the same with only one class.

    then a and p would both be of type Protagonist

    that's the idea of a class: one abstract idea and several concrete objects derived from that.

    But I haven't read your code thorougly....

  • @ Ace

    else if(appeal){
    

    must be

    else if(!appeal){
    
  • You are correct Chrisir didn't noticed that :). Cheers.

  • Thanks a lot for all your help, it works fine now. Why is it that a wrong piece of code influences a previous line? To me it would be more logic that the wrong '=' would only effect the 'if' part of the code, but strangely enough that part still did function (otherwise the protagonist won't move towards the antagonist).

  • When you do '=' it assings true to the boolean. So if you do if(appeal = true) it sets appeal to true, then checks if appeal is true (obviously it is) so the code gets executed. There's no syntax error. The result is just not the one you want.

  • edited March 2014

    I love to use the = operator and its siblings inside branch conditions: :ar!

    if ((x += vx) > width)  x = 0;
    

    What happens is that all operators evaluate to a resulting value.
    So the x += vx expression returns the value assigned to x.
    Then that result is used as an operand for the next > operator! ;)

    Another neat example which pauses/resumes a sketch:

    boolean isLooping;
    
    void mousePressed() {
      if (isLooping = !isLooping)   loop();
      else                        noLoop();
    }
    
Sign In or Register to comment.