Ok, I’m trying to do something rather tricky here in p5.
Take a look at my shudder class inside my Trees class:
function draw() {
background(0, 111, 10);
trees.trunk();
trees.leaves();
trees.shudder();
}
class Trees {
constructor(x, y) {
this.x = x,
this.y = y,
this.l = this.x - 2,
this.r = this.y + 2,
this.a = 0
}
trunk() {
noStroke();
fill(126, 60, 1);
rect(this.x + 3, this.y, 14, 40);
triangle(this.x + 10, this.y + 30, this.x - 4, this.y + 40, this.x + 24, this.y + 40);
}
leaves() {
fill(71, 215, 45);
ellipse(this.x + 3, this.y - 5, 25);
ellipse(this.x + 10, this.y - 15, 25);
ellipse(this.x + 17, this.y - 5, 25);
}
shudder() {
let d = dist(mouseX, mouseY, this.x, this.y);
if (d < 50) {
this.a = 4;
if (this.x < this.l || this.x > this.r) {
this.a = this.a * -1;
}
} else {
this.a = 0;
}
this.x = this.x + this.a;
}
}
As you can see I’m making this tree shudder back and forth when I click on it which is working! However, I’m trying to simulate it shaking as if it’s being stuck with an axe three times. i.e I’d like it to start juddering half a second, then stop, start, stop, start and then permanently stop.
I’ve tried my best as you can see here:
shudder() {
let d = dist(mouseX, mouseY, this.x, this.y);
let counter = 0;
if (d < 50) {
const intervals = setInterval(function() {
counter++;
this.a = 4;
console.log(counter);
if (this.x < this.l || this.x > this.r) {
this.a = this.a * -1;
}
if (counter === 3) {
clearInterval(intervals)
}
}, 1000)
} else {
this.a = 0;
}
this.x = this.x + this.a;
}
But no dice. I get this in the console:
I realise it might be the fact that the class is called inside a loop might be screwing it up.
the counter is just so I can see what’s going on inside the setInterval function.
Do I have to make another function and call it in a non-looping function to make setInterval work nicely?
I should mention I’m using the p5 Library but I don’t think it would affect this too much apart from the mouseX and mouseY pre-defined functions.
Can anybody solve this puzzle?
Many Thanks!