Function, This and Array

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

Sign In or Register to comment.