2017-03-14 6 views
1

Okay, so versuche ich, einige Übung mit Konstruktorfunktionen zu gewinnen, aber ich bin mit einem Fehler in Bezug auf die Funktion random() kommen:p5.js Bibliothek die 'random()' Funktion?

12: Uncaught Reference: zufällig nicht definiert Sie „% CDID nur versuchen Sie, p5.js die random() -Funktion zu verwenden? Wenn dies der Fall ist, können Sie es in die setup() -Funktion Ihrer Skizze verschieben. \ n \ nWeitere Informationen finden Sie unter: github.com/processing/p5.js/wiki/Frequently -Akeds-Fragen # why-cant-ich-assign-variables-using-p5-Funktionen-und-Variablen-vor-Setup "

Kann ich diese Funktion nur in der setup() Funktion verwenden? Ich glaube nicht, dass ich schon einmal auf dieses Problem gestoßen bin. Gibt es eine Lösung mit ähnlicher Logik?

var circles = function(x,y,d,xSpeed,ySpeed) { 
this.x = x; 
this.y = y; 
this.d = d; 
this.xSpeed = xSpeed; 
this.ySpeed = ySpeed; 
} 

var circle1 = new circles(random(width),random(height), random(30,55),random(3,19),random(3,19)); 
var circle2 = new circles(random(width),random(height), random(30,55), random(3,19),random(3,19)); 
var circle3 = new circles(random(width),random(height), random(30,55), random(3,19),random(3,19)); 
var circle4 = new circles(random(width),random(height), random(30,55), random(3,19),random(3,19)); 
var circle5 = new circles(random(width),random(height), random(30,55), random(3,19),random(3,19)); 
var circle6 = new circles(random(width),random(height), random(30,55), random(3,19),random(3,19)); 


function setup() { 
createCanvas(900,900); 

} 

function draw() { 

background(0); 
fill(255,255,0); 
ellipse(circle1.x,circle1.y,circle1.d,circle1.d); 
} 

function moveCircles() { 

} 

Antwort

0

Nun, technisch gesehen können Sie diese Funktion außerhalb der setup() Funktion von On-Demand globalen Modus, wie die Dokumentation sagt. Es wird jedoch empfohlen, diese Funktion nur innerhalb der setup()-Funktion zu verwenden, wenn Sie keine andere Bibliothek von Drittanbietern verwenden.

p5 Funktionen vor der Inbetriebnahme

new p5(); // you need to call this fn first 

var circle1 = new circles(random(width),random(height), random(30,55),random(3,19),random(3,19)); 
var circle2 = new circles(random(width),random(height), random(30,55), random(3,19),random(3,19)); 
var circle3 = new circles(random(width),random(height), random(30,55), random(3,19),random(3,19)); 
var circle4 = new circles(random(width),random(height), random(30,55), random(3,19),random(3,19)); 
var circle5 = new circles(random(width),random(height), random(30,55), random(3,19),random(3,19)); 
var circle6 = new circles(random(width),random(height), random(30,55), random(3,19),random(3,19)); 

p5 Funktionen insde Setup ja

var circle1, circle2, circle3, circle4, circle5, circle6; 

function setup() { 
    createCanvas(900, 900); 
    circle1 = new circles(random(width), random(height), random(30, 55), random(3, 19), random(3, 19)); 
    circle2 = new circles(random(width), random(height), random(30, 55), random(3, 19), random(3, 19)); 
    circle3 = new circles(random(width), random(height), random(30, 55), random(3, 19), random(3, 19)); 
    circle4 = new circles(random(width), random(height), random(30, 55), random(3, 19), random(3, 19)); 
    circle6 = new circles(random(width), random(height), random(30, 55), random(3, 19), random(3, 19)); 
} 
+0

Okay, das funktionierte, cool danke. –