我希望代码在第一次点击时说出第一个单词,在第二次点击时说出第二个单词这是代码: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); }}
2 回答

开满天机
TA贡献1786条经验 获得超13个赞
您可以使用数组作为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
}
}

胡子哥哥
TA贡献1825条经验 获得超6个赞
只需用于plus访问数组中的每个元素:
var plus = 0;
var words = ['hola', 'como', 'esta'];
document.body.addEventListener("click", () => {
plus++;
document.write(words[plus % words.length]);
});
body {
height: 100vh;
width: 100vw;
}
<body></body>
添加回答
举报
0/150
提交
取消