Ich habe kürzlich versucht, in p5.js zu bekommen und auf dem ersten Projekt, das ich arbeite, erlebe ich ein völlig perverses Problem, das aus irgendeinem Grund nirgendwo sonst online erwähnt wird von dem, was ich finden kann.Ich kann nicht auf Variablen verweisen, die in setup()
Jede Variable, die ich außerhalb von Setup() definiere, ist undefiniert, aber das ist eine bekannte Tatsache. Das Problem ist, dass jede Variable, die ich innerhalb von setup() definiere, ebenfalls undefiniert ist, das bedeutet, dass ich überhaupt keine Variablen verwenden kann. Hier
ist der 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);
}
Hier ist die Rückverfolgung von Chrom in vscode:
ReferenceError: plyPos is not defined
at drawPly (https://magicgonads.github.io/p5test/sketch.js:18:23)
at draw (https://magicgonads.github.io/p5test/sketch.js:14:3)
at p5.redraw (https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.8/p5.js:16560:7)
at p5.<anonymous> (https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.8/p5.js:11593:12)
at p5.<anonymous> (https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.8/p5.js:11489:12)
at new p5 (https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.8/p5.js:11769:12)
at _globalInit (https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.8/p5.js:8048:7)
Dies ist die grundlegende Art und Weise Variablen scoped sind in fast alle Sprachen programmation –