Tezak
Tezak

Reputation: 36

Small Basic Variables in Arrays?

In my hangman program, I'm trying to use an array to detect if a letter has been pressed before, and therefore cannot be pressed again.

In my attempt to do this, I have tried using:

If targetarray[LastLetter] = 1 Then

Where 'LastLetter' is my variable.

In another section of my code I also have the arrays stored as such:

 Sub LetterArrays
  If GraphicsWindow.LastKey = "a" Then
    targetarray["a"] = 1
  ElseIf GraphicsWindow.LastKey = "b" Then
    targetarray["b"] = 1

And so on...

The sub that the initial code checking if the letter = 1 is inside a sub that is called when a letter is pressed (GraphicsWindow.KeyDown) and before my if clause, I used: targetarray[LastLetter] = 1 as my way of setting the letter in the array to 1.

If this makes sense to you and you have a solution, thank you very much.

Upvotes: 0

Views: 303

Answers (2)

CodeAlong
CodeAlong

Reputation: 1

From what I understand, you don't want the same key pressed twice. Here is how you could do this:

GraphicsWindow.KeyDown = KeyDown
i = 0

Sub KeyDown
    If Array.ContainsValue(keysPressed, GraphicsWindow.LastKey) Then
        ' key already pressed before
    Else
        ' key not pressed before
        i = i + i
        keysPressed[i] = GraphicsWindow.LastKey
        ' add code here

Upvotes: 0

Zock77
Zock77

Reputation: 1017

Not 100% sure what it is you're asking, but if you are just looking to detect keypresses, then this code should do the trick for ya:

GraphicsWindow.KeyDown = KeyDown
GraphicsWindow.KeyUp = KeyUp

While 1 = 1
  Program.Delay(10)

  If Key["a"] Then
    TextWindow.WriteLine("Key 'a' was pressed")
  EndIf
EndWhile


Sub Keydown
  LastKeyDown = GraphicsWindow.LastKey
  Key[LastKeyDown] = "True"
EndSub

Sub KeyUp
  LastKeyUp = GraphicsWindow.LastKey
  Key[LastKeyUp] = "False"
EndSub

Upvotes: 0

Related Questions