2017-06-15 20 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

ソリューション自体はFAQにもある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.

から:

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); 
} 
関連する問題