Opposite of random(); ?

Hi, I have an array which I want to pull single letters from and continuously return to display them on each frame. The random function does this, except that I want them to displayed chronological. Heres a sample of my code:

let streamLetters = ['S', 'O', 'U', 'L']; this.value = random(streamLetters);

Is there any other built in function in P5 I could use? If not, how can this be done in plain JS?

Answers

  • Can't you just use a variable that keeps track of which index you're looking at, and increment that index whenever you want to go to the next letter?

  • I assume you're talking about a for loop, or am I wrong? I need to start the sequence from the first letter after it has gone through all the letters in the array.

  • I'm not really talking about a for loop, but I suppose the idea is similar. Instead of a for loop though, you could change the index every 10 seconds.

  • Thank you for the help so far. I have a function with an if statement that runs the code on a set interval (based on framecount and another parameter). So I just need to return the letters in the sequence they are set, and then start from the beginning. Here's the function with return in the block code:

    this.sequencedLetters = function() { if (frameCount % this.switchInterval == 0) { let letters = ['S', 'O', 'U', 'L']; this.value = random(letters); }

  • Okay cool. So next I would create a variable outside of any functions, call it index. Use that as an index into your array, and then increment it whenever you want to change the letter.

  • Thank you again. I'll try that tomorrow, time for sleep now.

Sign In or Register to comment.