Results 1 to 7 of 7

Thread: [RESOLVED] Exclude visible=false objects from a For loop

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    4

    Resolved [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!

  2. #2

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    4

    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)

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    4

    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

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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.

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    4

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width