[英]JavaScript, how do I make fill() a variable (that I can use in a custom function)?
I'm new to JS and have not found an answer to this question (or any question like it).我是 JS 新手,还没有找到这个问题的答案(或任何类似的问题)。 I'm making a relatively simple program: create fish in tank and have them be of different lengths and colour.
我正在制作一个相对简单的程序:在坦克中创建鱼,并让它们具有不同的长度和颜色。
I can make them different lengths through having a length variable in the parameters and editing the length argument within the function, but I do not think that same logic can be applied to custom adjustable colours in the same way*.我可以通过在参数中使用长度变量并在 function 中编辑长度参数来使它们具有不同的长度,但我认为相同的逻辑不能以相同的方式应用于自定义可调节颜色*。
So in essence, how would I structure my code so that I can create the command fill() , a variable such that it can be edited to make different colours from the function call?所以本质上,我将如何构造我的代码,以便我可以创建命令fill() ,一个变量,以便可以编辑它以从 function 调用中生成不同的颜色?
*EDIT: Apologies, I forgot to add code: *编辑:抱歉,我忘了添加代码:
background(147, 226, 250);
var colour = fill(158, 52, 158); //Failed attempt at custom fill()
var drawFish = function(posX, posY, x) {
ellipse(posX, posY, 166, 31);
triangle(posX + 81, posY, posX + 125, posY - 16, posX + 125, posY + 16);
ellipse(posX - 60, posY - 1, 10, 10);
};
drawFish(90, 241);
drawFish(121, 174);
drawFish(175, 356);
drawFish(238, 300);
drawFish(263, 22);
ANSWERED.回答。 I couldn't make fill() a variable of course but instead I created variables that would generate random numbers from 0 to 256(exclusive) and placed them inside the fill() function.
我当然不能让 fill() 成为变量,而是创建了可以生成 0 到 256(不包括)随机数的变量,并将它们放在 fill() 函数中。 This allowed me to generate different colours for my fish!
这让我可以为我的鱼生成不同的颜色!
Thanks for the help.谢谢您的帮助。
Sample code:示例代码:
var drawFish = function (posX, posY, tailSize) {
stroke(74, 74, 74);
var randomNum = random(0,256);
var randomNum1 = random(0,256);
var randomNum2 = random(0, 256);
fill(randomNum, randomNum1, randomNum2);
var bodySize = random(150, 200);
ellipse (posX, posY, 166,31); //body
fill(randomNum1, randomNum, randomNum2);
triangle (posX + 81, posY, posX + 125/20*tailSize, posY - 16/20*tailSize, posX + 125/20*tailSize, posY+16/20*tailSize); //tail
//...(Removed code)
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.