Variables in p5.js functions

moomoo
edited April 16 in p5.js Library Questions

Ok, So I am struggling with finding any information on variables.

the code that works looks like this: Basically its moving the triangle to the left.

  var a, b, c
  var counter

  p.setup = () => {
    p.createCanvas(1600, 200);
    b = 800; a = 800; c = 800
    counter = 0
  }

  function rotation(degrees) {
    p.rotate(p.radians(degrees))
  }

  function triangleLeft() {
      a < 0 ? a = 800 :a = a-2;
      console.log(a)
      p.fill(209, 238, 255);
      p.translate(a, 100);
        p.triangle(0, -50, -50, 0, 0, 50)
  }

  p.draw = () => {
    counterTimer()
    p.background(89, 113, 255);
    p.noStroke();
      triangleLeft()
}

but I want to use the tringleLeft() function a bunch of times, if I try and do:

  function triangleLeft(num) {
      num < 0 ? num = 800 :num = num-2;
      console.log(num)
      p.fill(209, 238, 255);
      p.translate(num, 100);
        p.triangle(0, -50, -50, 0, 0, 50)
  }

  p.draw = () => {
    counterTimer()
    p.background(89, 113, 255);
    p.noStroke();
      triangleLeft(a)
}

it doesn't increase the variable, it just stays at 800.. any ideas?

Answers

  • I am also using the react wrapper hense the p. infront of the p5 functions

  • We don't have the full picture here. What does your counterTimer() function do?

    Also your assignment construction looks odd to me. It would more typically be written as:

    num = num < 0 ? 800 : num-2;

Sign In or Register to comment.