2017-06-15 2 views
-2
function Childclass(){ 
    this.printX = function(){ 
    print(this.vector.y); 
    } 
} 

function Superclass(){ 
    this.vector = createVector(1,2); 
} 

Childclass.prototype = new Superclass(); 

Wenn printX() ich die folgende Fehlermeldung ausgeführt wird:Wie kann ich createVector in einem Prototyp verwenden?

Uncaught ReferenceError: createVector is not defined at new Superclass

Ist das möglich createVector() in Prototypen?

+0

Können Sie bitte Posten [MCVE] zeigt genau das, was Sie zu tun versuchen? Besser noch, können Sie einen JSFiddle oder einen CodePen veröffentlichen? –

Antwort

1

Vom p5 FAQ:

Why can't I assign variables using p5 functions and variables before setup()?

Well, technically, you can by using on-demand global mode. But that's a less common use of p5, so we'll explain that later and talk about the more common case first. In regular global mode, p5 variable and function names are not available outside setup(), draw(), mousePressed(), etc. (Except in the case where they are placed inside functions that are called by one of these methods.) What this means is that when declaring variables before setup(), you will need to assign them values inside setup() if you wish to use p5 functions.

Die Lösung selbst ist auch in der FAQ:

We mentioned on-demand global mode earlier. This mode is most useful when you're building a program that uses other libraries and you want to control how p5 is loaded on the page with the others. You can read more about it here . But another interesting use of on-demand global mode is the ability to call p5 explicitly and then use p5 functions outside of setup(). Here's an example:

new p5(); 

var boop = random(100); 

function setup() { 
    createCanvas(100, 100); 
} 

function draw() { 
    background(255, 0, boop); 
} 
Verwandte Themen