Syntax error on token "==", invalid Assignment Operator

Hello, I am new to processing. And I am really stuck I'm currently try to make a object move left to right or right to left randomly.

 int z = random(1);

    if (z == 1) {
      check == true;
    }

    if (z == 0) {
      check == false;
    }



  if (check == true) {

  x = 0;

    }

    if (check == false) {

      x = 500;

    }

    y = random(410, 440);

    x1 = 25;

    y1 = 50;

    if (check == true) {

  dx = SPEED2 * random(0.5, 0.8);
    }

    if (check == false) {

  dx = -dx;
    }

however the line (check == true) has a error on it. Any idea how i can fix this?

Answers

  • must be
    check = false;

  • edited March 2014

    Welcome to processing! It's great!

    • the double == is for comparison (as in if)

    • the single = is for assignment (give the variable on the left the result of what's on the right)

    you mostly have that right anyway.

    Greetings, Chrisir ;-)

  • edited March 2014

    Hmm, then it says cannot convert float to int.. for (int z = random(1))

  • you might want to use setup() and draw()

    pls read the section "hello mouse" here:

    http://www.processing.org/tutorials/overview/

  • edited March 2014 Answer ✓

    you wrote:

    Hmm, then it says cannot convert float to int.. for (int z = random(1))

    hm,

    int z = int (random(1));

  • Oh, thank you i figure it out!

  • here...

    int z = int (random(1)); 
    boolean check = false ; 
    
    int x;
    int y;
    
    int x1;
    int y1;
    
    float  dx=0;
    float  dy;
    
    int SPEED2 = 0;
    
    
    if (z == 1) { 
      check = true;
    }
    
    
    if (z == 0) {
      check = false;
    }
    
    if (check == true) { 
      x = 0;
    } 
    if (check == false) { 
      x = 500;
    } 
    y = int (random(410, 440)); 
    x1 = 25; 
    y1 = 50;
    
    
    if (check == true) {
      dx = SPEED2 * random(0.5, 0.8);
    }
    
    if (check == false) {
      dx = -dx;
    }
    
    //
    
  • How to post code

    when you post your code:

    • in the editor hit ctrl-t to auto format

    • copy it

    • paste it in the browser

    • leave 2 empty lines before it

    • mark the code (without the 2 empty lines)

    • press C in the small command bar.

  • edited March 2014

    For pure boolean comparisons like:
    if (check == true) {} and if (check == false) {}

    A more elegant (and possibly better performance) coding is:
    if (check) {} and if (!check) {}

  • int z = int (random(1));

    This will always be 0 (so check will always be false). Run the following code:

    for(int i = 0; i < 50; i++)
    {
      println(random(1) + "  |  " + int(random(1)));
    }
    

    The reason is that int() always rounds down to an integer (it removes the part that comes after the decimal point entirely). The solution is to take a random number between 0 and 2:

    int z = int (random(2));
    boolean check = false ;
    
    if (z == 1) {
      check = true;
    }
    

    You don't need the second if statement to check if z is equal to 0, because check is false by default so you're not changing anything. Also in the improbable case z would be equal to 2 one day (not sure if this is even possible), it will be taken care of.

Sign In or Register to comment.