2017-06-24 6 views
1

私はC#プログラミングの初心者です。私は絵がbox.thisクリックすると、色の変化に私がしたいような場合.IN C#でゲームを作るつもりだ、私は配列にピクチャボックスの色のchanged.Accordingをクリックしworked.when配列を使用してピクチャボックスの背景色を変更します。

  private void pictureBox1_MouseClick(object sender, MouseEventArgs e) 
    { 
     int[] R = { 0, 255, 255, 34, 249,255 }; 
     int[] G = { 0, 255, 0, 235, 255 ,153}; 
     int[] B= { 255, 255, 0, 27, 40,51 }; 
     pictureBox1.BackColor = Color.FromArgb(R[this.index], G[this.index], B[this.index]); 
     this.index++; 
     } 

このコード私のコードですR、G、Bは6色を得ることができます。最後の色の後に私はこのメッセージを受け取ります。 インデックスは配列の境界外でした。

誰かが、私は方法を見つけた...私に

+1

一つの解決策は、わずか5回をクリックすることですか、それは6当たったとき、あなたはすなわち、インデックスを包み込む0にリセットする必要があります: '指数=(インデックス+ 1)%を6; ''% 'は[モジュロ演算子]です(https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/modulus-operator) – TaW

+0

私はそれをチェックしますTaw .thanks –

答えて

1

をthis.Thankあなたのためのソリューションを教えてください。

 int[] R = { 0, 255, 255, 34, 249, 255 }; 
     int[] G = { 0, 255, 0, 235, 255, 153 }; 
     int[] B = { 255, 255, 0, 27, 40, 51 }; 
     pictureBox2.BackColor = Color.FromArgb(R[this.index], G[this.index], B[this.index]); 
     this.index++; 
     if (this.index == 6) 
     { 
      this.index -= 6; 

     } 

第六color.againた後、私たちは

+0

'this.index - = 6;'単に 'this.index = 0; 'と書いた方が分かりやすいので、誰でもあなたがインデックスをリセットしているのを見ることができます。 – t3chb0t

0

配列のインデックス0から開始し、それの長さは、したがって、単にあなたのコードの周りにいる場合は、この

private void pictureBox1_MouseClick(object sender, MouseEventArgs e) 
{ 
    if(this.index <= 5){ 
    int[] R = { 0, 255, 255, 34, 249,255 }; 
    int[] G = { 0, 255, 0, 235, 255 ,153}; 
    int[] B= { 255, 255, 0, 27, 40,51 }; 
    pictureBox1.BackColor = Color.FromArgb(R[this.index], G[this.index], B[this.index]); 
    this.index++; 
    } 
} 
+0

ありがとうWahidSaeed –

0

のようなものを包む、6である第一の色を得ることができますこれで試してみてください。 配列の長さについて考慮する必要があります。 ) - -

private void pictureBox1_MouseClick(object sender, MouseEventArgs e) 
{ 
    int[] R = { 0, 255, 255, 34, 249,255 }; 
    int[] G = { 0, 255, 0, 235, 255 ,153}; 
    int[] B= { 255, 255, 0, 27, 40,51 }; 
    if(this.index <= (R.Length - 1) ||this.index <= (G.Length - 1) || this.index <= (B.Length - 1)) 
    { 
     pictureBox1.BackColor = Color.FromArgb(R[this.index], G[this.index], B[this.index]); 
     this.index++; 
    } 
    else 
    { 
     // Do nothing since you don't have colors 
    } 

}

+0

ありがとうEranga Sandaruwan –

関連する問題