Results 1 to 5 of 5

Thread: Loops

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800

    Cool

    How do I make a loop? specifically I want this to loop:

    Public Sub Shoot()
    If ShotEnabled = True Then
    Shot.y = Shot.y - 2

    If Shot.y = Enemy.y Then
    If Shot.x = Enemy.x Then Enemy.w = Enemy.w + 1
    End If
    If Shot.x = Enemy.x Then
    If Shot.y = Enemy.y Then Enemy.w = Enemy.w + 1
    End If
    End If
    End Sub


  2. #2
    Addicted Member jcouture100's Avatar
    Join Date
    Aug 1999
    Posts
    141
    Hi Steve,

    You have a couple of options...

    If you want the loop to happen a specific number of times then use a FOR ... NEXT Loop.

    Code:
    For intCnt = 0 to 3
       Call Shoot
    Next intCnt
    If you want the loop to continue looping until a certain criteria is met then use a DO [While/Until] LOOP Loop

    Code:
    blnFlag = False
    Do Until blnFlag = True
        Call Shoot
        'Somewhere in your Shoot sub you will
        'need to set the value
        'of blnFlag to True if you don't want an
        'infinite loop. blnFlag will need to be
        'Public in this case to be able to be set
        'from your Shoot sub.
    Loop
    Hope this helps.
    JC

  3. #3
    Hyperactive Member
    Join Date
    Sep 1999
    Posts
    305
    Well, you could put in a timer with a refresh rate of about 100 milliseconds. in it, put

    Call Shoot()

    That will call your sub every 100 milliseconds. To stop it, put

    Timer1.Enabled = False

    or to start the looping

    Timer1.Enabled = True

    Another thing you can consider is condensing your code to:

    Code:
    Public Sub Shoot()
    If ShotEnabled = True AND Shot.y-2 = Enemy.y AND Shot.x = Enemy.x Then
    Enemy.w = Enemy.w + 1
    End If
    End Sub
    That should do the same thing, I think.

    bob

  4. #4
    Guest

    Cool Just a thought

    In the posting Do...Loop example. If there are a number of lines of code following the test you conduct use "Exit Do" after the exact exit criteria is meet. vb will pop you out of the loop without executing unwaited sequences of code.

  5. #5
    Hyperactive Member
    Join Date
    Sep 1999
    Posts
    305
    The problem with jcouture100s answer is that it will suspend everything until this happens. I'm assuming you're making a game that has a bullet colliding with the enemy. In this case, you won't want everything frozen until the collision occurs. Instead, you'll need the separate function that tests for a collision(you named it Shoot already), repeated often, like with a timer.

    bob

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