我是一个非常初学者,并且在P5.js中有类,:(
试图将P5.js实例模式与这个神话般的教程结合起来:{4}在p5.js中与Emily Xie合作。
在运行代码时,我在第59行中有和未捕获的ReferenceError(“generateSymbols未定义”),但是this.generateSymbols已在第155行中定义...
这里发生了什么?
/* Poem Matrix
I am gonna risk, I am gonna risk the value of every single chance. Decent needs on time, part silence, part judgment.
I am gonna face, I am gonna face about ten people in a white hall praying for maintain the differences in a short conversation.
He stands up and draws a few steps across the living room. Then, he looks forward and she’s gone again. He stands up and draws a few steps across the living room. Then, he looks forward and she’s gone again.
*/
var letterRain = function ( l ) {
var myFont;
preload = function() {
myFont = l.loadFont('fonts/CutiveMono-Regular.ttf');
console.log("loaded");
};
var canvas;
var streams = [];
var symbolSize = 30;
windowResized = function() {
l.resizeCanvas(l.windowWidth, l.windowHeight);
console.log("window resized");
};
l.setup = function() {
canvas = l.createCanvas(l.windowWidth, l.windowHeight);
canvas.position(0, 0);
canvas.style("z-index", "-1");
//background(255);
var x = 0;
console.log("canvas creado y posicionado")
for (var i = 0; i < l.width / symbolSize; i++) {
stream = new Stream();
generateSymbols(x, l.random(-2000, 0));
streams.push(stream);
x += symbolSize
};
textSize(symbolSize);
textFont(myFont);
console.log("he pasado por el for");
};
l.draw = function() {
l.background(255);
streams.forEach(function(stream){
stream.render();
});
console.log("he pasado por el draw");
};
/* Symbol Class*/
function Symbol (x, y, speed){
this.x = x;
this.y = y;
this.value;
this.speed = speed;
this.switchInterval = l.round (l.random(90, 200));//el anterior era: 50, 100
this.setToRandomSymbol = function() {
if(frameCount % this.switchInterval == 0){
var lyric = ["I am", "gonna", "risk", "the", "value", "of", "every", "single chance", "Decent", "needs", "on", "time", "part", "silence", "part judgment", "she’s gone again", "short conversation","looks forward", "draws", "conversation"]
this.value =
lyric[l.floor(l.random(lyric.length))];
};
//console.log("he pasado");
};
this.rain = function (){
this.y = (this.y >= height) ? 0 : this.y += this.speed;
};
console.log("rain");
};
/* Stream Classs*/
function Stream(){
this.symbols = [];
this.totalSymbols = l.round(l.noise (2, 8));//la anterior era random en vez de noise
this.speed = l.random (1, 2);//controla la velocidad el anterior: 1 5 y random en veez de noise
this.generateSymbols = function (x, y){
for (var i = 0; i <= this.totalSymbols; i++){
symbol = new Symbol(x, y, this.speed);
symbol.setToRandomSymbol();
this.symbols.push(symbol);
y -= symbolSize;
};
console.log("final");
};
this.render = function(){
this.symbols.forEach(function(symbol){
l.fill(0, 0, 0);
text(symbol.value, symbol.x, symbol.y);
symbol.rain();
symbol.setToRandomSymbol();
});
};
};
}; //final de la funcion l
new p5( letterRain );
答案 0 :(得分:0)
你的代码没有在我的机器上运行,但我想我知道它为什么不起作用。
for (var i = 0; i < l.width / symbolSize; i++) {
stream = new Stream();
stream.generateSymbols(x, l.random(-2000, 0));
streams.push(stream);
x += symbolSize
};
我认为你必须添加stearm.generateSybols(...
我希望这有帮助
答案 1 :(得分:0)
generateSymbols()
被称为全局函数,但尚未在全局定义,因此您会收到错误。
generateSymbols()
是Stream
的函数,因此必须从Stream
的实例访问它。您已经在前面的行中创建了此实例,因此您只需要添加steam实例变量。
stream.generateSymbols();