javascript - Can't reference variables declared in setup() -


i have tried p5.js , on first project working on experiencing perverse issue whatever reason isn't mentioned anywhere else online can find.

any variable define outside of setup() undefined, that's known fact. issue variable define inside of setup() undefined, means can't use variables @ all.

here code:

function setup() {   const canvasratio = 0.975;   createcanvas(int(windowwidth*canvasratio), int(windowheight*canvasratio));   fill(0);   strokeweight(2);   var plymov = createvector(0,0,0);   var plypos = createvector(0,0,0);   const plyspd = 1;   const plyenable = 0; }  function draw() {   background(0);   drawply(); }  function drawply(){   plypos=createvector(plypos.x+plymov.x,plypos.y+plymov.y,0)   plymov=createvector(0,0,0);   stroke(255,0,0);   ellipse(int(width/2),int(height/2),80, 80); } 

here traceback chrome in vscode:

referenceerror: plypos not defined     @ drawply (https://magicgonads.github.io/p5test/sketch.js:18:23)     @ draw (https://magicgonads.github.io/p5test/sketch.js:14:3)     @ p5.redraw (https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.8/p5.js:16560:7)     @ p5.<anonymous> (https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.8/p5.js:11593:12)     @ p5.<anonymous> (https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.8/p5.js:11489:12)     @ new p5 (https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.8/p5.js:11769:12)     @ _globalinit (https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.8/p5.js:8048:7) 

any variable define outside of setup() undefined, that's known fact.

umm, what? not well-known fact. might mean can't use processing's functions before setup, doesn't stop defining variable @ top of sketch.

if define variable in setup(), it's available in setup(). that's true of variable defined inside function.

if want use variable in multiple functions, should define @ sketch level. can still initialize (give value) in setup() function:

var plypos; var plymov;  function setup() {   const canvasratio = 0.975;   createcanvas(int(windowwidth*canvasratio), int(windowheight*canvasratio));   fill(0);   strokeweight(2);   plymov = createvector(0,0,0);   plypos = createvector(0,0,0); }  function draw() {   background(0);   drawply(); }  function drawply(){   plypos=createvector(plypos.x+plymov.x,plypos.y+plymov.y,0)   plymov=createvector(0,0,0);   stroke(255,0,0);   ellipse(int(width/2),int(height/2),80, 80); } 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -