java - Float not working with switch -
i have problem code, i'm trying make script spawn different every time click mouse, error switch because use random choose different number each time. error every time try run script , can't find error.
cannot switch on value of type float. convertible int values, strings or enum variables permitted
some of tags , names of variables in danish i'm danish.
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 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() { (int = 0; < 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); }
as comments suggest: expected behavior. can't switch on variable of type float.
the answer is: bad idea anyway. keep in mind floating point numbers "inaccurate" design (see here example). whereas switch
has notion of exactly matching n different cases. technically not possible floating point numbers.
in sense: step back, , change type of variable int example. should int skift
- not float.
and ask: "but how random int number?" - see here.
Comments
Post a Comment