We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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;
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 ;-)
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/
you wrote:
hm,
int z = int (random(1));
Oh, thank you i figure it out!
here...
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.
For pure
boolean
comparisons like:if (check == true) {}
andif (check == false) {}
A more elegant (and possibly better performance) coding is:
if (check) {}
andif (!check) {}
This will always be 0 (so check will always be false). Run the following code:
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: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.