質問

I was recently trying to make a light "guess the color" game (my first one), where you get a hex code and you get some alternative to choose from. The way I've done it I need to collect information about these alternatives (which are just colored ovals). Anyway, I need to change the RGB values of a color since they are randomly generated, and it doesn't seem very efficient to create (10 in this case) separate colors and set them one at a time.

My problem is, I can not find a way to change the RGB values of my colors. Basically I created a color array (I noticed this problem exists without the array aswell) but trying to change values as you would with a normal variable doesn't work. It just gives me an error.

This is the specific method.

public void OvalData(int r, int b, int g, int x, int i){
    //Create two arrays containing the information
    Color OvalColor[] = new Color[difficulty];
    int[] posX = new int[difficulty];
    //Set the infromation
    Color[i] = (r, g, b);
    posX[i] = x;

Where this is the problem line:

   `Color[i] = (r, g, b);` 

Any help would be appreciated!

役に立ちましたか?

解決

Color[i]=... can't work at all, because Color is the name of the class, not your variable. What you want to change is OvalColor[i], not Color[i].

You could try OvalColor[i]=new Color(r, g, b) to assign to one of your colors, but i guess there's more than that to fix; OvalColor probably shouldn't be local to the OvalData() method.

Not related to your problem, but to your coding style: Please use initial Caps for class names only, and initial lowercase for variable and method names, like ovalColor or public void ovalData(...). This makes problems like the one you're having much easier to spot - you'll get used to "Assigning to something hat has an initial capital letter won't work" after a while.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top