P5.js实例模式不能处理矩阵字母雨

问题描述 投票:2回答:2

我是一个非常初学者,在P5.js上课,:(

试图将P5.js实例模式与这个神话般的教程结合起来:与Emily Xie在p5.js中的Matrix Digital Rain

在运行代码时,我在第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 );
p5.js
2个回答
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(...

我希望这有帮助


0
投票

generateSymbols()被称为全局函数,但尚未在全球定义,因此您得到错误的原因。

generateSymbols()Stream的函数,因此必须从Stream的实例访问它。您已经在之前的行上创建了此实例,因此您只需要添加steam实例变量。

stream.generateSymbols();
© www.soinside.com 2019 - 2024. All rights reserved.