Issues with accessing a structure array

Post here to discuss programming with swordfish basic

Moderators: Chuckt, Garth, bitfogav

Post Reply
User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm

Issues with accessing a structure array

Post by brad » Sun Jun 02, 2013 6:48 pm

I posted this over at the swordfish forum (sfcompiler.co.uk) but no bites as yet. Anywho...

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
I then created an array of these so I can get multiple balls:

Code: Select all

Dim Multiball(4) As Ball
And then I have a sub routine to bounce the balls around the screen (within a 32 pixel wide area):

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
In the above code, it has a for next loop using the variable Index (which has been declared previously as a byte), which should cycle through each ball in multiball and move it accordingly. Unfortunately with this code, the balls never move - they just appear in their starting locations (which are pre-set) and that's it - they just don't move anywhere.

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!

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: Issues with accessing a structure array

Post by bitfogav » Sun Jun 02, 2013 8:18 pm

Just posted on SF forum with this issue Brad :)
http://www.sfcompiler.co.uk/forum/viewt ... =9530#9530
If you don't know what Voltage your country is using, you shouldn't be doing electronics ;-)

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm

Re: Issues with accessing a structure array

Post by brad » Sun Jun 02, 2013 9:01 pm

Just replied!

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: Issues with accessing a structure array

Post by bitfogav » Sun Jun 02, 2013 9:07 pm

It might be something that David will have to look into? :)
If you don't know what Voltage your country is using, you shouldn't be doing electronics ;-)

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm

Re: Issues with accessing a structure array

Post by brad » Sun Jun 02, 2013 9:14 pm

I think he's busy with firewing :)

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests