2

I have an array of objects Letter, and if keyboard key A is pressed it adds Letter to an array.

 static Letter[] letters = new Letter[50]; // all letters to be shown
 static int letterCounter = 0; // last array element
 public static Vector2 Cursor = new Vector2(10, 10); 

 public static void Update()
    {
        keyState = Keyboard.GetState();

        Checker();

        oldKeyState = keyState;
    }

 static void Checker()
    {
        if(IsKeyPressed(Keys.A))
        {
            characters[letterCounter] = 'a';
            letters[letterCounter] = new Letter('a');
            Cursor.X += 20;
            letterCounter++;
        }
    }

All letters in array should be drawn, but aren't, only the last element in array is drawn.

public static void Draw(SpriteBatch spriteBatch)
    {
        for(int i = 0; i < letterCounter; i++)
        {
              letters[i].Draw(spriteBatch);
        }

        spriteBatch.DrawString(font, ("Letter 0: " + (letters[0] == null)), new Vector2(10, 50), Color.Black);
        spriteBatch.DrawString(font, ("Letter 1: " + (letters[1] == null)), new Vector2(10, 70), Color.Black);
    }

As you can see I also tested if previous elements are maybe null for some reason, but they aren't, they are all valid objects I think but just the last one in array gets drawn :/

EDIT:

A pressed first time

A pressed second time

Fildor
  • 14,510
  • 4
  • 35
  • 67
  • Why are you looping through `letters.Length` and then checking for null instead of just looping to `letterCounter`? And what's with `IsKeyPressed()` instead of using `keyState.IsKeyDown()`? I mean, you're getting the keyboard state and then ignoring it. – itsme86 Aug 31 '17 at 14:41
  • Well letters[0] would then be drawn without checking if its null and it should be null before pressing A key – Bruno Filip Aug 31 '17 at 14:43
  • Nope. `letterCounter` starts at 0, so `i < letterCounter` will break right away since `i` (0) is not less than `letterCounter` (0). – itsme86 Aug 31 '17 at 14:44
  • Oh thanks for pointing that out, I changed it but it doesn't fix the problem, still the last element is only drawn – Bruno Filip Aug 31 '17 at 14:45
  • Are they writing over each other? What does your `Letter.Draw()` implementation look like? I'm guessing it draws the character at `Cursor`. But `Cursor` is going to be the same value for each character in the array. – itsme86 Aug 31 '17 at 14:46
  • How are you calling Update()? It may be a case the class level variables are being reinitialized each time – Richard Boyce Aug 31 '17 at 14:52
  • Well this all is in static class called InputSystem, and I use InputSystem.Update in Game1 Update and InputSystem.Draw in Game1 Draw, Look again at Checker method Cursor.X += 20; , so Cursor is different always, and game 1 letter all the time, if I press A more, the only thing thats happening is letter moving for 20, but as I said, I check if previous letters are null, and they are not. – Bruno Filip Aug 31 '17 at 14:59
  • "so Cursor is different always" - No it isn't. Cursor is static. You *change* it with each key press, but when drawing, the position is the same for all. You'd have to save the current Cursor for each index if you wanted them to be different. – Fildor Aug 31 '17 at 15:23
  • 2
    Ooooooh I should make a position variable in Letter Class and just forward the Cursor to it, yeah that might work EDIT: Yeah thanks a lot can't upvote anyone tho, its just comments :/ – Bruno Filip Aug 31 '17 at 15:30

0 Answers0