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

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

Childclass.prototype = new Superclass(); 

當運行printX()我得到以下錯誤:如何在原型中使用createVector?

Uncaught ReferenceError: createVector is not defined at new Superclass

這可能在原型中使用createVector()

+0

能否請您發表[MCVE]顯示正是你想做什麼?更好的是,你可以發佈JSFiddle或CodePen嗎? –

回答

1

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.

的解決方案本身也是在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); 
}