[RESOLVED] Exclude visible=false objects from a For loop
I am creating a basic shooting game, one shape the player moves around. You press space and create another shape as a bullet trying to hit the enemies. I cannot figure out how to make it so when the bullet (Shape2) isnt visible, it will not check to see if the enemy (Shape3) is hitting it.
Code:
Dim x As Integer
Dim y As Integer
Dim score As Integer
Dim a As Integer
Dim k As Integer
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyUp
Shape1.Top = Shape1.Top - 50
Case vbKeyDown
Shape1.Top = Shape1.Top + 50
Case vbKeyRight
Shape1.Left = Shape1.Left + 50
Case vbKeyLeft
Shape1.Left = Shape1.Left - 50
Case vbKeySpace
x = x + 1
Load Shape2(x)
'make it visible and set it in position
Shape2(x).Visible = True
'put it at the top of the shape 1
Shape2(x).Top = Shape1.Top
'centre it over the shape 1
Shape2(x).Left = Shape1.Left + (Shape1.Width / 2) - (Shape2(x).Width / 2)
'fire the shape 2
Do While Shape2(x).Top > -300
Shape2(x).Top = Shape2(x).Top - 1
Loop
If Shape2(x).Top <= -300 Then
Shape2(x).Visible = False
End If
End Select
End Sub
Private Sub Timer1_Timer()
For a = 0 To x
For k = 0 To 2
If Shape3(k).Visible = True Then
If Shape2(a).Visible = True Then
If Shape2(a).Left > Shape3(k).Left - Shape2(a).Width And Shape2(a).Left < Shape3(k).Left + Shape3(k).Width Then
If Shape2(a).Top < Shape3(k).Top + Shape3(k).Height Then 'And Shape2(a).Top > Shape3(k).Top - Shape2(a).Height Then
Shape2(a).Visible = False
Shape3(k).Visible = False
score = score + 10
End If
End If
End If
End If
Next k
Next a
Label1.Caption = "Score: " & score
End Sub
Private Sub Timer2_Timer()
Randomize
Shape3(y).Top = Shape3(y).Top + 100
If Shape3(y).Top >= 7680 Then
Shape3(y).Top = -120
Shape3(y).Left = Rnd(100) * 10000
End If
y = y + 1
If y = 3 Then y = 0
End Sub
With what I have there it does not detect any of them being hit, where as if i remove the If Shape2(a).visible = true then statement it does, but the bullet will still hit an enemy after it goes invisible. Hopefully I explained this well enough, thanks to anyone who can help!:)
Re: Exclude visible=false objects from a For loop
http://www.vbforums.com/attachment.p...id=47243&stc=1
I moved your thread to here from the Visual Basic 6 and Earlier forum.
Re: Exclude visible=false objects from a For loop
Oh thanks, I thought I put it there... lol guess not. And glad to be here (hopefully I can get this to work too! lol)
Re: Exclude visible=false objects from a For loop
Wait, don't I want it in the VB6 and earlier because I am in fact coding this in Vb6? :P
Re: Exclude visible=false objects from a For loop
This is the official forum for game programming in any language so I moved your thread here. You may however find some game programming questions in the VB6 forum.
Re: Exclude visible=false objects from a For loop
I see some things that should be improved.
1) I don't think you can rely on an infinite number of loaded shape2 (bullets). If someone played that game for awhile and held down the spacebar, you'd eventually get an error trying to load additional bullets.
2) Because you are never removing bullets, only making them invisible, your loop of checking against ships will get longer and longer, slowing down the game quite a bit
3) What scalemode is your game set to? If it is twips, you are moving your bullet by 1. A twip is ~15 not 1. It should be moved by Screen.TwipsPerPixelY in that case.
The reason your invisible bullet keeps hitting ships is because once it hits a ship and becomes hidden, you do not exit your loop; thereby keeps hitting other ships. Add Exit For after you increment your score
Re: Exclude visible=false objects from a For loop
Thanks, and yeah I know it could do to be improved, it's really just a basic thing to get across the point that I have learned what we've been being taught. I got it done and working, thank you.