【问题标题】:Cannot Figure out Why Variable is Undefined in p5.js无法弄清楚为什么在 p5.js 中未定义变量
【发布时间】:2016-04-03 23:06:18
【问题描述】:

我确信这是有史以来最简单的问题,但是我和我的朋友在 p5.js 中配对编码,无法弄清楚为什么下面的代码会抛出错误:Uncaught ReferenceError: RiLexicon is not defined

我们已经完成了一个类似的项目,所有代码基本上都在同一个地方,而且它可以工作。也许我们已经盯着这个太久了,要疯了?请帮忙!谢谢!

var mermaid = ("Fishes, both large and small, glide between the branches, as birds fly among the trees here upon land. In the deepest spot of all, stands the castle of the Sea King. Its walls are built of coral, and the long, gothic windows are of the clearest amber. The roof is formed of shells, that open and close as the water flows over them. Their appearance is very beautiful, for in each lies a glittering pearl, which would be fit for the diadem of a queen.");
var story = []; 
var lexicon;


function setup() {

    createCanvas(650,400);
    lexicon = new RiLexicon();
    story = RiTa.tokenize(mermaid);
    //partsOfSpeech = RiTa.getPosTags(story);
    textSize(15);
    fill(255, 100, 100);


function draw(){
 // background(255);

 var wordPosX = 10;
var wordPosY = width/8;


    for(var i=0; i < story.length; i++){
      text(story[i], wordPosX, wordPosY)
      textWidth(story[i]+5,30);

      //we check whether each parts of speech exists
      //in our array
      //if(partsOfSpeech[i] != null){
      //  text(partsOfSpeech[i], wordPosX, wordPosY+20, textWidth(story[i]), 20);
     // fill(100,175,175);  


      wordPosX += textWidth(story[i])+ 3;

      //if wordPosX goes beyond our width,
      //move the text down to a new line
      if(wordPosX +30 > width){
        wordPosX = 10;
        wordPosY += 50;
      }
    }
}
      function mousePressed(){
        changingWords();
        textSize(15);

        function changingWords (){
        var story = "Fishes," + 
          lexicon.randomWord("nn") + "" + 
          lexicon.randomWord("jj") + 
          "and" + lexicon.randomWord("jj") + 
          ", glide between the branches, as birds fly among the trees here upon" + 
          lexicon.randomWord("nn") + 
          ". In the deepest" + lexicon.randomWord("nn") + 
          "of all, stands the" + lexicon.randomWord("nn") + 
          "of the Sea King. Its walls are built of" + 
          lexicon.randomWord("jj") + 
          ", and the" + lexicon.randomWord("jj") +
          "," + lexicon.randomWord("jj") + 
          "windows are of the clearest" + 
          lexicon.randomWord("jj") + 
          ". The" + lexicon.randomWord("nn") + 
          "is formed of shells, that" + 
          lexicon.randomWord("jj") + 
          "and close as the" + 
          lexicon.randomWord("nn") + 
          "flows over them. Their" + 
          lexicon.randomWord("nn") + 
          "is very" + lexicon.randomWord("jj") + 
          ", for in each lies a glittering" + 
          lexicon.randomWord("nn") + 
          ", which would be fit for the" + 
          lexicon.randomWord("nn") + 
          "of a queen."
        }  
    }
 }

【问题讨论】:

  • “RiLexicon”在哪里定义?我看不到代码,你已经显示了。
  • 哦,我以为我们不必这样做?这是 RiTa 的一个功能。我们应该怎么做?
  • 我对RiTa一无所知,但它必须以某种方式加载。
  • 您在代码中使用了表达式new RiLexicon();,这意味着它期望在某处定义RiLexicon() 函数。但它没有在您的代码中定义。如果它是在外部库中定义的,那么您需要将该库的 .js 文件与您的 .js 文件一起包含,以便它可以找到该函数定义。

标签: javascript variables processing p5.js


【解决方案1】:

就像 cmets 所说,您需要确保正在加载 RiTa 库。

听起来你以前的草图可能已经加载了库,然后你从那个草图中复制了一些代码到一个新的中。问题是,这还不足以让你的新草图生效。您必须将库加载到新草图中才能访问它。

Here 是一个关于如何加载库的非常棒的教程,但我会尝试在此处复制基础知识:

第一步:here下载RiTa。

第二步:找到一个名为rita-full.min.js的文件(你也可以直接从here下载)。

第 3 步:将该文件复制到您的草图目录中。它应该可以通过您的 html 文件访问。

第 4 步:编辑您的 html 文件以加载库,方法是将此行放在 &lt;head&gt; 部分:

<script src="your/path/to/rita-full.min.js" type="text/javascript"></script>

第 5 步:现在,当您加载 html 页面时,库将被加载,您的代码将能够访问它。

您可以在 RiTaJS 的 GitHub 页面 herethis RiTaJS tutorial 中找到更多信息。

【讨论】:

  • 谢谢!我正在和我的朋友一起工作,你是对的,她没有正确加载库。感谢您的帮助!
猜你喜欢
  • 2015-05-18
  • 2016-10-28
  • 1970-01-01
  • 2019-05-05
  • 1970-01-01
  • 2022-09-15
  • 1970-01-01
  • 2014-07-22
  • 2015-04-06
相关资源
最近更新 更多