为什么在修改值之前将变量赋值给变量
Why is a variable being assigned to a value AFTER the value has been modified when I've assigned it before it's been modified
let Text = [];
let textsize = 10;
let Total_Character = 0;
let Ypos = 100;
function setup() {
createCanvas(400, 400);
}
function keyPressed() {
//All the characters which I DON'T want in the text, and their keycode
// 8 = BackSpace, 17 = Control, 13 = Enter(NotDone), 16 = SHIFT, 18 = ALT, 20 = CAPS LOCK, 9 = TAB, 27 = ESCAPE, 45 = INSERT, 46 = DELETE, 33 = PAGE UP, 34 = PAGE DOWN, 36 = HOME, 35 = END, 144 = NUM LOCK, 91 = WINDOWS, 173 = F1, 174 = F2, 175 = F3, 177 = F4, 179 = F5, 176 = F6, 118 = F7, 91 = F8, 38 = UP ARROW, 39 = RIGHT ARROW, 40 = DOWN ARROW, 37 = LEFT ARROW
//All the Values I don't want in my text writer (Such as "Media Volume Down")
if (keyCode != 8 && keyCode != 17 && keyCode != 16 && keyCode != 18 && keyCode != 20 && keyCode != 9 && keyCode != 27 && keyCode != 45 && keyCode != 46 && keyCode != 33 && keyCode != 34 && keyCode != 36 && keyCode != 35 && keyCode != 144 && keyCode != 91 && keyCode != 173 && keyCode != 174 && keyCode != 175 && keyCode != 177 && keyCode != 179 && keyCode != 176 && keyCode != 118 && keyCode != 91 && keyCode != 38 && keyCode != 39 && keyCode != 40 && keyCode != 37) {
//Only true if you press a key that is NOT one of the keycodes listed above, it pushes the key into the Text array (Not to be confused with `text` function)
Text.push(key)
}
}
function draw() {
background(220);
Total_Character = Text.length;
//Changes all the different one character strings in the array to one large string
for (var i = 0; i < Text.length - 1; i++) {
Text[0] += Text[1];
Text.splice(1)
}
for (var i = 0; i < Text.length; i++) {
//Displays the text
textSize(textsize)
text(Text[i], textWidth(Text[i]) / 2, Ypos);
}
}
<script src="https://cdn.jsdelivr.net/npm/p5@1.3.1/lib/p5.js"></script>
除非未键入任何内容,否则 Total_Character
变量始终为“1”。这部分是有道理的,因为 Total_Character
是文本的长度,我使文本的长度始终为 1 个大字符串。我需要帮助理解的部分是为什么它是“1”我将 Total_Character
分配给文本的长度 BEFORE 我更改文本的长度,所以不应该不是总字数吗?为什么修改text数组后才定义?
顺便说一句,我在程序中加入了很大一部分,让人们了解我在这里要完成的工作。我本来可以不包含 keyPressed()
下面的代码,但我觉得这样更容易理解我的整个程序,当你 运行 使用代码片段工具时,它会在片段工具。你可以完全忽略下面的所有内容,因为它不会特别影响这个问题。
您得到的是数组的长度,而不是数组中字符串的长度。
将 Text
的所有元素组合成一个字符串后,就不需要循环遍历它了。只显示 Text[0]
.
let Text = [];
let textsize = 10;
let Total_Character = 0;
let Ypos = 100;
function setup() {
createCanvas(400, 400);
}
function keyPressed() {
//All the characters which I DON'T want in the text, and their keycode
// 8 = BackSpace, 17 = Control, 13 = Enter(NotDone), 16 = SHIFT, 18 = ALT, 20 = CAPS LOCK, 9 = TAB, 27 = ESCAPE, 45 = INSERT, 46 = DELETE, 33 = PAGE UP, 34 = PAGE DOWN, 36 = HOME, 35 = END, 144 = NUM LOCK, 91 = WINDOWS, 173 = F1, 174 = F2, 175 = F3, 177 = F4, 179 = F5, 176 = F6, 118 = F7, 91 = F8, 38 = UP ARROW, 39 = RIGHT ARROW, 40 = DOWN ARROW, 37 = LEFT ARROW
//All the Values I don't want in my text writer (Such as "Media Volume Down")
if (keyCode != 8 && keyCode != 17 && keyCode != 16 && keyCode != 18 && keyCode != 20 && keyCode != 9 && keyCode != 27 && keyCode != 45 && keyCode != 46 && keyCode != 33 && keyCode != 34 && keyCode != 36 && keyCode != 35 && keyCode != 144 && keyCode != 91 && keyCode != 173 && keyCode != 174 && keyCode != 175 && keyCode != 177 && keyCode != 179 && keyCode != 176 && keyCode != 118 && keyCode != 91 && keyCode != 38 && keyCode != 39 && keyCode != 40 && keyCode != 37) {
//Only true if you press a key that is NOT one of the keycodes listed above, it pushes the key into the Text array (Not to be confused with `text` function)
Text.push(key)
}
}
function draw() {
background(220);
Total_Character = Text.length;
//Changes all the different one character strings in the array to one large string
Text[0] = Text[0].join('');
Text.splice(1)
//Displays the text
textSize(textsize)
text(Text[0], textWidth(Text[0]) / 2, Ypos);
}
<script src="https://cdn.jsdelivr.net/npm/p5@1.3.1/lib/p5.js"></script>
let Text = [];
let textsize = 10;
let Total_Character = 0;
let Ypos = 100;
function setup() {
createCanvas(400, 400);
}
function keyPressed() {
//All the characters which I DON'T want in the text, and their keycode
// 8 = BackSpace, 17 = Control, 13 = Enter(NotDone), 16 = SHIFT, 18 = ALT, 20 = CAPS LOCK, 9 = TAB, 27 = ESCAPE, 45 = INSERT, 46 = DELETE, 33 = PAGE UP, 34 = PAGE DOWN, 36 = HOME, 35 = END, 144 = NUM LOCK, 91 = WINDOWS, 173 = F1, 174 = F2, 175 = F3, 177 = F4, 179 = F5, 176 = F6, 118 = F7, 91 = F8, 38 = UP ARROW, 39 = RIGHT ARROW, 40 = DOWN ARROW, 37 = LEFT ARROW
//All the Values I don't want in my text writer (Such as "Media Volume Down")
if (keyCode != 8 && keyCode != 17 && keyCode != 16 && keyCode != 18 && keyCode != 20 && keyCode != 9 && keyCode != 27 && keyCode != 45 && keyCode != 46 && keyCode != 33 && keyCode != 34 && keyCode != 36 && keyCode != 35 && keyCode != 144 && keyCode != 91 && keyCode != 173 && keyCode != 174 && keyCode != 175 && keyCode != 177 && keyCode != 179 && keyCode != 176 && keyCode != 118 && keyCode != 91 && keyCode != 38 && keyCode != 39 && keyCode != 40 && keyCode != 37) {
//Only true if you press a key that is NOT one of the keycodes listed above, it pushes the key into the Text array (Not to be confused with `text` function)
Text.push(key)
}
}
function draw() {
background(220);
Total_Character = Text.length;
//Changes all the different one character strings in the array to one large string
for (var i = 0; i < Text.length - 1; i++) {
Text[0] += Text[1];
Text.splice(1)
}
for (var i = 0; i < Text.length; i++) {
//Displays the text
textSize(textsize)
text(Text[i], textWidth(Text[i]) / 2, Ypos);
}
}
<script src="https://cdn.jsdelivr.net/npm/p5@1.3.1/lib/p5.js"></script>
除非未键入任何内容,否则 Total_Character
变量始终为“1”。这部分是有道理的,因为 Total_Character
是文本的长度,我使文本的长度始终为 1 个大字符串。我需要帮助理解的部分是为什么它是“1”我将 Total_Character
分配给文本的长度 BEFORE 我更改文本的长度,所以不应该不是总字数吗?为什么修改text数组后才定义?
顺便说一句,我在程序中加入了很大一部分,让人们了解我在这里要完成的工作。我本来可以不包含 keyPressed()
下面的代码,但我觉得这样更容易理解我的整个程序,当你 运行 使用代码片段工具时,它会在片段工具。你可以完全忽略下面的所有内容,因为它不会特别影响这个问题。
您得到的是数组的长度,而不是数组中字符串的长度。
将 Text
的所有元素组合成一个字符串后,就不需要循环遍历它了。只显示 Text[0]
.
let Text = [];
let textsize = 10;
let Total_Character = 0;
let Ypos = 100;
function setup() {
createCanvas(400, 400);
}
function keyPressed() {
//All the characters which I DON'T want in the text, and their keycode
// 8 = BackSpace, 17 = Control, 13 = Enter(NotDone), 16 = SHIFT, 18 = ALT, 20 = CAPS LOCK, 9 = TAB, 27 = ESCAPE, 45 = INSERT, 46 = DELETE, 33 = PAGE UP, 34 = PAGE DOWN, 36 = HOME, 35 = END, 144 = NUM LOCK, 91 = WINDOWS, 173 = F1, 174 = F2, 175 = F3, 177 = F4, 179 = F5, 176 = F6, 118 = F7, 91 = F8, 38 = UP ARROW, 39 = RIGHT ARROW, 40 = DOWN ARROW, 37 = LEFT ARROW
//All the Values I don't want in my text writer (Such as "Media Volume Down")
if (keyCode != 8 && keyCode != 17 && keyCode != 16 && keyCode != 18 && keyCode != 20 && keyCode != 9 && keyCode != 27 && keyCode != 45 && keyCode != 46 && keyCode != 33 && keyCode != 34 && keyCode != 36 && keyCode != 35 && keyCode != 144 && keyCode != 91 && keyCode != 173 && keyCode != 174 && keyCode != 175 && keyCode != 177 && keyCode != 179 && keyCode != 176 && keyCode != 118 && keyCode != 91 && keyCode != 38 && keyCode != 39 && keyCode != 40 && keyCode != 37) {
//Only true if you press a key that is NOT one of the keycodes listed above, it pushes the key into the Text array (Not to be confused with `text` function)
Text.push(key)
}
}
function draw() {
background(220);
Total_Character = Text.length;
//Changes all the different one character strings in the array to one large string
Text[0] = Text[0].join('');
Text.splice(1)
//Displays the text
textSize(textsize)
text(Text[0], textWidth(Text[0]) / 2, Ypos);
}
<script src="https://cdn.jsdelivr.net/npm/p5@1.3.1/lib/p5.js"></script>