Hello guys, so I’m trying to make this counter sort of thing where a function takes in an input and then it shows it back in reverse like a countdown. But I’m unable to get it working properly as the numbers tend to just get drawn on top of one another.
I’ve tried setting the background but it isn’t working. The approach I’ve taken is that my function populates the array and then sets the text. I’m not sure if its a problem with the nesting loop or something else
var range = [];
function setup() {
createCanvas(400, 400);
textAlign(CENTER, CENTER);
textSize(24);
sequence(10);
}
function draw() {
}
function sequence(number) {
for (i = number; i >= 0; i--) {
for(j = 0; j < range.length; j++) {
text(range[j], width / 2, height / 2);
}
range.push(i);
}
}
Your issue is that you are looping through a set amount of times and calling text(...) each iteration causing each text() call to draw on top of each other.
You can use the frameCount and modulo to get a close representation of a timer, though this is not an accurate account of time … but it may help to get you started. Essentially this is assuming that every 60 frames equals 1 second, in theory p5 runs 60 frames per second, but that is not always true.
Hi, thank you for the response but I really wanna see it done using arrays if that’s possible… And I’m sorry if I didn’t mention this in the question but I’m not really looking at it as a timer but rather as a function that takes in a value, counts it down to 0 and that’s all!
I have managed to work around and get it to just go through the array and display the last entry of the array, so now I’ve to find a way to make the loop not run instantaneously?
var range = [];
var indexCounter;
function setup() {
createCanvas(400, 400);
textAlign(CENTER, CENTER);
textSize(24);
sequence(10);
}
function draw() {
}
function sequence(number) {
for (i = number; i >= 0; i--) {
range.push(i);
}
for (j = range.length; j >= 0; j--) {
indexCounter = range[j];
}
text(range[indexCounter], width / 2, height / 2);
}
Oh wow, thank you and here I was just about to hit the bed all defeated…
So, I was just sticking to using arrays cause it’s like this learning thing where I must absolutely work within the constraints given, or it’s just plain stupidity sorry!
Although I’ve got the result, I’ve also managed to confuse myself a bit and I’m not entirely sure if it works exactly in the way I wanted, but along the way, I’ve also learned about intervals.
I’m sure it’d all clear up once I get more experience with all that intervals stuff I guess. Anyway, here’s the abomination I came up with
var range = [];
function setup() {
createCanvas(400, 400);
textAlign(CENTER, CENTER);
textSize(24);
populateArray(10);
sequence();
}
function populateArray(limit) {
for (i = limit; i > 0; i--) {
range.push(i);
}
}
var sequence = function() {
for (var j = 20; j >= 0; j--) { // j = 20 is arbitrary, just wanted to knw if the loop was running in relation to the array and not something else!
setTimeout(j => {
background(204),
text(range.length--, 200, 200)
}, 1000 * j);
}
}