We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, i've a few problems, I want to create a sketch which generate randomly falling triangle. I've already achieve to make one falling triangle, but i don't really understand how array/table works in js, to generate more than one triangle. All my triangles are generate at the same position.
Oh and I'm in instance mode.
var sketch = function (p) {
var t1,t2,t3,t4,t5,t6,time;
p.setup = function () {
p.createCanvas(p.windowWidth, p.windowHeight);
for(var i=0;i<10;i++){
generate();
}
};
generate = function () {
this.t1 = p.random(0,p.windowWidth);
this.t3 = this.t1+p.random(-150,120);
this.t5 = this.t1+p.random(-120,150);
this.t2 = 300;
this.t4 = p.random(-150,120);
this.t6 = p.random(-120,150);
this.time = p.random(0.5,2.5);
p.print(this.t1);
}
p.draw = function () {
p.background(150);
for(var i=0;i<10;i++){
animate();
}
};
animate = function (){
p.triangle(this.t1,this.t2,this.t3,this.t2+this.t4,this.t5,this.t2+this.t6);
this.t2+=this.time;
p.print(this.t1);
if(this.t2>p.windowHeight){
generate();
}
}
}
new p5(sketch, pong);
Answers
You should implement a
class
to represent 1 Triangle w/ all its properties & methods:https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class
Although not in instance mode, here's an online example of using a
class
to represent 1 Ball:http://CodePen.io/GoSubRoutine/pen/KaerGb/right?editors=101
And at the link below, its corresponding instance mode source code: :bz
https://forum.Processing.org/two/discussion/17306/multiple-canvases-on-one-page#Item_13
Thanks, i take a lot of time to understand how that work, but your example help me :) !
You can see a premiere version on my unfinished portfolio (in french) part "Mes Creations" : http://peyheramouno.mmi-angouleme.fr/Portfolio/