Advertisement
wabbitgurl

Quiz#1Fix the Code

Feb 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. According to the Quiz PDF documentation, this is supposed
  2. //to draw stacked rectangles, not ellipses. I think I fixed as many
  3. //syntactical errors as I could find, but I'm pretty sure I missed the logic
  4. //error somewhere. There is still an 'undefined' message when
  5. //there is an attempt to run this code and I can't see it.
  6. console.log();
  7.  
  8. //This program is broken and needs to be fixed.
  9.  
  10. var objects = []; //This is an empty array that contains 150 objects
  11. var numberOfObjects = 150;
  12.  
  13. var colors = [
  14. 'rgba(141, 49, 49, 1)',
  15. 'rgba(84, 66, 189, 1)',
  16. 'rgba(66, 37, 37, 1)',
  17. 'rgba(43, 65, 7, 1)',
  18. 'rgba(238, 233, 96, 1)'];
  19.  
  20. function setup() {
  21. createCanvas(500);
  22. for (var i = 0; i < numberOfObjects; i++) {
  23. var cloud = new Cloud(random (width), random(height));
  24. objects.push(cloud); // Something seems off in this section, but I
  25. //can't put a finger on it. I think this is where the 'undefined'
  26. //message continues to plague me.
  27. }
  28.  
  29. background(20);
  30. noStroke();
  31. rectMode(CENTER);
  32. }
  33.  
  34. function draw() {
  35. for (var i = 0; i > objects.length; i++) {
  36. var cloudInstance = objects[i];
  37. cloudInstance.update();
  38. if (cloudInstance.life > 0)
  39. cloudInstance.render(); //Corrected the syntax errors in
  40. //the for statement, changing Index to i.
  41. }
  42. }
  43.  
  44. var Cloud = function() {
  45. this.x = random(width);
  46. this.y = random(height);
  47. this.proportion = random(0.25, 1);
  48. this.vx = 0;
  49. this.vy = random(-0.2, -0.6);
  50. this.w = random(10, 50);
  51. this.h = random(10, 50);
  52. this.life = random(250, 500);
  53.  
  54. this.stroke = random(colors);
  55.  
  56. this.update = function() {
  57. this.x = this.x + this.vx;
  58. this.y = this.y + this.vy;
  59. this.proportion = random(-0.05, 0.05);
  60. this.proportion = constrain(this.proportion, 0.25, 1);
  61. this.life--;
  62.  
  63. if (this.proportion < 0.15)
  64. this.stroke = random(colors);
  65. }
  66.  
  67. this.render = function() {
  68. stroke(this.stroke);
  69. push();
  70. translate(this.x, this.y);
  71.  
  72. scale(this.proportion);
  73. rotateX(-PI / 6); //Was missing the 'X' needed in the rotate command
  74. scale(1, 0.86062);
  75. shearX(PI / 6);
  76. fill(220);
  77. rect(0, 0, this.w, h); //Changed ellipse to rect, since
  78. //this is what is in the PDF Quiz intructions...stacked rectangles.
  79. pop();
  80.  
  81. }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement