Hi everyone, hopefully someone can lend a hand with an issue I am having.
I am writing a little program that displays different color bouncing balls on a 32x32 pixel RGB LED matrix. I wanted to use a structure array for the balls. The code to bounce the balls around the screen works just fine as long as I copy and paste each section of code and individually write in the number of the ball in the structure array.
To start off, heres the contents of the structure:
Code: Select all
Structure Ball
Color As Byte
X As Byte
Y As Byte
XDelay As Byte
XDelay2 As Byte
YDelay As Byte
YDelay2 As Byte
GoingUp As Boolean
GoingLeft As Boolean
MaxJumpHeight As Byte
CurrentJumpHeight As Byte
End Structure
Code: Select all
Dim Multiball(4) As Ball
Code: Select all
Sub BounceTheBalls()
for Index = 0 to bound(multiball)
' First move the balls left / right
If Multiball(Index).XDelay <> 0 Then
Dec(Multiball(Index).XDelay)
Else
Multiball(Index).XDelay = Multiball(Index).XDelay2
If Multiball(Index).GoingLeft = false Then
Inc(Multiball(Index).X)
If Multiball(Index).X = 31 Then
Multiball(Index).GoingLeft = true
EndIf
Else
Dec(Multiball(Index).X)
If Multiball(Index).X = 0 Then
Multiball(Index).GoingLeft = false
EndIf
EndIf
EndIf
' then move the balls up / down
If Multiball(Index).YDelay <> 0 Then
Dec(Multiball(Index).YDelay)
Else
If Multiball(Index).GoingUp = false Then ' if the ball is going down
If Multiball(Index).YDelay2 > 0 Then ' only take one away if we are still greater than 0 (0 is the fastest we can get because it's the lowest delay)
Dec(Multiball(Index).YDelay2)
EndIf
Multiball(Index).YDelay = Multiball(Index).YDelay2 ' now make the ball move down 1 space
Inc(Multiball(Index).Y)
Else ' if the ball is not going down, then it must be going up - so we need to slow the ball down
Inc(Multiball(Index).YDelay2)
Multiball(Index).YDelay = Multiball(Index).YDelay2
Dec(Multiball(Index).Y)
Inc(Multiball(Index).CurrentJumpHeight)
EndIf
If Multiball(Index).Y = 31 And Multiball(Index).GoingUp = false Then
Multiball(Index).YDelay = 0
Multiball(Index).YDelay2 = 0
Multiball(Index).GoingUp = true
Multiball(Index).CurrentJumpHeight = 0
End If
If Multiball(Index).GoingUp = true And Multiball(Index).CurrentJumpHeight >= Multiball(Index).MaxJumpHeight Then
Multiball(Index).GoingUp = false
EndIf
EndIf
next
End Sub
However if I use exactly the same code - but replace the 'Index' in multiball(Index) with a number such as 0, 1, 2 or 3, the code works perfectly and that particular ball will then bounce around the screen. If I copy and paste this code four times (one time for each ball) and then replace the 'Index' with an actual number, all balls then bounce around the screen.
So I guess the big issue here is that as soon as I try and use a variable to access the array, it doesn't work. But if I use an actual integer - it does work.
Any help or suggestions would be much appreciated!