Hi
Here’s my situation :
I have 3 lines of text, displayed like this :
line1. Hello
line2. Hello
line3. Hello
I want the first line to move towards the right direction, the second to the left, the third to the right …
So I set a if statement in my for loop in order to sort the pair / impair line :
let interval = 20;
let offset = 0;
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
for(let i = 0; i<3; i++){
text('hello', width/2+offset, 10+i*interval);
if(i%2){
offset++;
}else{
offset--;
}
}
}
But the if statement doesn’t work cause all lines move to the same direction. I truly don’t know why…
Thank you for the help !
that’s the same variable for left and right moving text
try (this gives an animation)
for(let i = 0; i<3; i++){
//
if(i%2 == 0 ){ // maybe say == 0 here?
// using offset2 for right
offset2++;
text('hello', width/2+offset2, 10+i*interval); // moves right very fast
}else{
// left
offset--;
text('hello', width/2+offset, 10+i*interval); // moves left
}
}
Another Sketch
for static left/right offset :
for(let i = 0; i<3; i++){
//
offset = 0; // reset
if(i%2 == 0 ){ // maybe say == 0 here or !=0 ?
// for right
offset +=6;
}else{
// left
offset -=6;
}
text('hello', width/2+offset, 10+i*interval); // text out (left or right)
}
On the “I don’t know why” part, if you come from basic or another language where code is executed outside of a loop, beware that the animation does not upgrade each time your for loop loops, but every time draw() is executed. And it is executed many times for second.