Arkanoid problem

Sta

Member
Hi I'm new here, i've tried to create simple game - arkanoid. I wrote the code but if the ball contacts with bricks, sometimes the error is shown. I don't know where's the problem, please :sign0085:, thanks.
 

Attachments

  • Arkanoid.zip
    13.4 KB · Views: 223

specci48

Well-Known Member
Licensed User
Longtime User
Hi sta,

I'm not really sure if I'm able to explain this error so you can understand it :sign0013:
This sub is buggy:
B4X:
Sub CollisionCheck
   For a=0 To AlBricksX.Count-1
      If rect.X>AlBricksX.Item(a)-15 AND rect.X<AlBricksX.Item(a)+30 AND rect.Y>AlBricksY.Item(a)-15 AND rect.Y<AlBricksY.Item(a)+17 Then 
         Control("Brick" & a).Dispose
         AlBricksY.RemoveAt(a)
         AlBricksX.RemoveAt(a)
         Speed.Y=-Speed.Y
                        [COLOR="Red"]Return[/COLOR]
      End If
   Next
End Sub
If you add the return statement, one of your errors will vanish. This is because you are altering the array within a loop around this array. E.g. AlBricksX.Count = 4 your loop goes from 0 to 3. If a brick is hit, you delete values of each array so only 3 entries are left (from 0 to 2). After that your loop tries to access these arrays with the last value of the loop definition (here 3) but this entry is not available any more.

The second error I came up is because of this:
You added 4 bricks naming brick0, brick1, brick2 and brick3. If the ball hits first e.g. brick2, you dispose brick2 so brick0, brick1 and brick3 are left. Then you remove the position 2 in the x and y array. Now your brick data is messed up! The values AlBricksX.ItemAt(2) and AlBricksY.ItemAt(2) belong to brick3 because the positions in the arrays changed but not the image names.


specci48
 

Sta

Member
Thanks

Thank you very much for a quick reply, now i understand. I solve it this way:

AlBricksY.RemoveAt(a)
AlBricksY.Insert(a,1000)
AlBricksX.RemoveAt(a)
AlBricksX.Insert(a,1000)

I know this isn't very nice way to solve it but it works for now, so i will fix it later...
 
Top