我的代码出现问题,每次点击鼠标时我都会尝试让我的脚本产生不同的错误,但由于我使用随机选择每次不同的号码。 我每次尝试运行脚本时都会收到此错误,但找不到该错误。Float无法使用开关
Cannot switch on a value of type float. Only convertible int values, strings or enum variables are permitted
某些标签和变量名称是丹麦语,因为我是丹麦语。
int posX = 100;
int posX2 = 1820;
int dirX = 1;
int dirX2 = 1;
boolean drawforest = true;
int height = 1080;
int width = 1920;
//img = loadImage("BlueMorpho.png");
PImage img;
int storrelse = 15;
int storrelse2 = storrelse + 2;
float skift = random(0,2);
//setup()-metoden køres én gang, når sketchen startes
void drawBackground()
{
color e = color(135, 206, 235);
fill(e);
rect(960, 270, 1920, 560);
}
void setup()
{
size(1920, 1080);
rectMode(CENTER);
drawBackground();
color f = color(1, 142, 14);
fill(f);
rect(960, 820, 1920, 540);
//noLoop();
}
//draw()-metoden kører i et loop, indtil den stoppes
void draw()
{
switch(skift)
{
case 0:
img = loadImage("BlueMorpho.png");
break;
case 1:
img = loadImage("BlueMorpho2.png");
break;
default:
img = loadImage("BlueMorpho3.png");
break;
}
if (drawforest)
{
drawForest();
drawforest = false;
}
drawBackground();
drawCloud();
drawCloud2();
posX = posX+1;
posX2 = posX2-1;
if (posX>width-20 || posX<20)
{
dirX=-dirX;
}
if (posX2>width-20 || posX<20)
{
dirX2=-dirX2;
}
}
void drawForest()
{
for (int i = 0; i < 12; i++)
{
drawTree(random(55, 1865), 635 + 30*i);
}
}
void drawTree(float xx, float yy)
{
color c = color(139, 69, 19);
fill(c);
//noStroke();
rect(xx, yy+30, 50, 100);
color d = color(000, random(25, 100), 000);
fill(d);
//noStroke();
ellipse(xx, yy-30, 125, 100);
color g = color(100, 0, 0);
fill(g);
ellipse(random(xx-50, xx+50), random(yy-50, yy+18), 10, 10);
}
void drawCloud()
{
color h = color(255, 255, 255);
fill(h);
ellipse(posX, 150, 100, 50);
//posX += random(50,1920);
noStroke();
}
void drawCloud2()
{
color h = color(0, 0, 0);
fill(h);
ellipse(posX2, 200, 100, 50);
//posX += random(50,1920);
}
void tegnSommerfugl(float xx, float yy)
{
image(img, xx, yy, width/storrelse2, height/storrelse);
}
void mouseClicked()
{
tegnSommerfugl(mouseX,mouseY);
}
您正在尝试在变量“skift”,这是浮动式的切换。也许把'skift'改为int? – khriskooper
基础问题是声明float skift,但很难想象你想在设计级别。也许应该是int,但不确定 –
编译器是正确的,你不能基于float进行切换,它需要是int,string或enum。 –