猿问

我希望代码在第一次点击时说出第一个单词,在第二次点击时说出第二个单词

我希望代码在第一次点击时说出第一个单词,在第二次点击时说出第二个单词


这是代码:


var hola = false;

var plus = 0;

var words = ['hola', 'como', 'esta']


function draw() {

  class mouse {

    click() {

      if (mouseIsPressed) {

        plus = plus + 1

      }

    }

  }

  if (mouseIsPressed) {

    textSize(50);

    text(words[0], mouseX, mouseY, 50);

  }

}


MMMHUHU
浏览 128回答 2
2回答

开满天机

您可以使用数组作为FIFO数据结构(在这里我称之为队列)。通过使用.shift(),您可以弹出集合中的第一个元素以进行打印,然后继续执行直到集合为空。(您将需要处理尝试弹出一个空对象的情况)。var hola = false;var plus = 0;var wordsQueue = ['hola', 'como', 'esta']function draw() {  class mouse {    click() {      if (mouseIsPressed) {        plus = plus + 1      }    }  }  if (mouseIsPressed) {    textSize(50);    alert(wordsQueue.shift()); //removes head of queue  }}

胡子哥哥

只需用于plus访问数组中的每个元素:var plus = 0;var words = ['hola', 'como', 'esta'];document.body.addEventListener("click", () => {&nbsp; plus++;&nbsp; document.write(words[plus % words.length]);});body {&nbsp; height: 100vh;&nbsp; width: 100vw;}<body></body>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答