如何在 p5.js 中将整数转换为字符串
How to convert Ints to Strings in p5.js
我查看了 JS 和 P5.js 参考资料,但我无法弄清楚这段代码有什么问题。我将 p5.js 与 DOM 和声音库
一起使用
var year = 0
var yearST
function setup() {
createCanvas(1000, 750);
}
function draw() {
background(210)
textAlign(RIGHT,TOP)
yearST = str(year)
textSize(15)
console.log(yearST) //This is the var that matters
text(("Years survived: " + yearST), 990, 10)
}
记录年ST的console.log()returns
function () { [native code] }
求助,这段代码有什么问题。 (加分号时没有变化,我试过了)
这是 运行 在浏览器中的样子:
1, 应该是字符串末尾的 0
问题是 year
已经是 P5.js 库中的一个函数。您可以在参考文献 here.
中找到它
所以当您调用 str(year)
时,您实际上是在传递 year()
函数,而不是您的 year
变量。
只需将您的 year
变量重命名为不与现有函数冲突的名称,或使用 instance mode 来防止 P5.js 像这样弄乱全局命名空间。
我查看了 JS 和 P5.js 参考资料,但我无法弄清楚这段代码有什么问题。我将 p5.js 与 DOM 和声音库
一起使用var year = 0
var yearST
function setup() {
createCanvas(1000, 750);
}
function draw() {
background(210)
textAlign(RIGHT,TOP)
yearST = str(year)
textSize(15)
console.log(yearST) //This is the var that matters
text(("Years survived: " + yearST), 990, 10)
}
记录年ST的console.log()returns
function () { [native code] }
求助,这段代码有什么问题。 (加分号时没有变化,我试过了)
这是 运行 在浏览器中的样子: 1, 应该是字符串末尾的 0
问题是 year
已经是 P5.js 库中的一个函数。您可以在参考文献 here.
所以当您调用 str(year)
时,您实际上是在传递 year()
函数,而不是您的 year
变量。
只需将您的 year
变量重命名为不与现有函数冲突的名称,或使用 instance mode 来防止 P5.js 像这样弄乱全局命名空间。