Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - Foreach - problema
Forum - C# / VB.NET - Foreach - problema

Avatar
ht-never (Member)
Rookie


Messaggi: 49
Iscritto: 24/09/2011

Segnala al moderatore
Postato alle 13:26
Lunedì, 02/07/2012
sto implementando in un videogioco in C#, Framework XNA un sistema di custom levels basato su plain text. Esempio:
Codice sorgente - presumibilmente Plain Text

  1. 10
  2. 10
  3. 100
  4. 10
  5. 10
  6. 10
  7. 10
  8. 100


questo codice genera due rettangoli di qualche sorta, composti da quattro numeri, appunto che andranno ad essere gli argomenti del costruttore della classe Rectangle.

questo è il codice:
la funzione DrawWall disegna un  singolo rettangolo

Codice sorgente - presumibilmente C#

  1. private void DrawLevel(string file)
  2.         {
  3.             int[] buffer = new int[500];
  4.             int[] the_buffer = new int[4];
  5.             int count=0;
  6.  
  7.             string[] lines = System.IO.File.ReadAllLines(file);
  8.             foreach (string line in lines)
  9.             {
  10.                 buffer[count] = Convert.ToInt32(line);
  11.                 count++;
  12.             }
  13.             count = 0;
  14.             foreach (int i in buffer)
  15.             {
  16.                 if (count == 4)
  17.                 {
  18.                     count = 0;
  19.                     DrawWall(the_buffer[0],
  20.                              the_buffer[1],
  21.                              the_buffer[2],
  22.                              the_buffer[3]);
  23.                 }
  24.                 the_buffer[count] = i;
  25.             }
  26.         }


PM Quote
Avatar
ampeg (Normal User)
Pro


Messaggi: 124
Iscritto: 21/04/2011

Segnala al moderatore
Postato alle 13:45
Lunedì, 02/07/2012
mi sa che il problema è che non fai avanzare la variabile count nella seconda iterazione quindi non arriverà mai ad avere il valore uguale a 4

PM Quote
Avatar
ht-never (Member)
Rookie


Messaggi: 49
Iscritto: 24/09/2011

Segnala al moderatore
Postato alle 14:05
Lunedì, 02/07/2012
Grazie, era quello il problema

PM Quote