Results 1 to 8 of 8

Thread: Space Invaders

  1. #1

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727

    Angry Space Invaders

    Hi, im trying to make a space invaders game and am stuck moving my gun. Ive put a loop in that moves the ship to the right when a button is pressed and then should stop when released. the stop part doesnt work. can anyone help! here is the code.

    Private Sub CmdRight_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    Do Until LblBullet.Left = 324
    For i = 1 To 5000
    DoEvents
    Next i
    Dim MyVar As Variant
    MyVar = f
    If MyVar = t Then
    ImgGun.Left = ImgGun.Left + 1
    LblBullet.Left = LblBullet.Left + 1
    Else: Exit Do
    End If
    Loop

    End Sub

    Private Sub CmdRight_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    MyVar = f
    End Sub

    have i just got my variable or if statements in the wrong place

  2. #2

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727
    sorry, in the mousedown sub MyVar is initially set to t, i set it to f to test if it was seeing the variable at all

  3. #3
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    and what is t

  4. #4
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    You don't declear t in your code, so what is it....

    BTW..use [Highlight=VB] and [ /vbcode] tags whenposting code...

  5. #5
    Addicted Member
    Join Date
    Aug 2002
    Location
    Baltimore, MD
    Posts
    230

    Re: Space Invaders

    Couple of no-no's that pop right out:
    1)
    Originally posted by davebat
    For i = 1 To 5000
    DoEvents
    Next i
    NEVER, I repeat, NEVER do this. A loop like this will run as quickly as the PC will let it, guaranteeing different results on different machines.

    2)
    Originally posted by davebat
    Dim MyVar As Variant
    MyVar = f
    If MyVar = t Then
    ALWAYS, I repeat, ALWAYS use descriptive variable names. When you go back to look at code like this, you probably won't even remember what you were trying to do or what the variables are.

    Are you using Option Explicit in all of your modules and forms? If not, DO IT NOW!

  6. #6
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    well

    Dim Keys(0 to 255) as boolean
    ------------------------------------------
    Sub Keydown (lalalalala)
    Keys(keycode) = true
    end sub
    ------------------------------------------
    Sub keyUp(lallalala)
    Keys(KeyCode) = false
    End sub


    So as long as you know which key uses which code you always know when one is down
    Don't pay attention to this signature, it's contradictory.

  7. #7

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727
    thanks guys, Ive taken note of your advice and will change the code when i get in from work. what did you mean by open explicit? Im new to this you see.

  8. #8
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    From the MSDN:

    When Option Explicit appears in a module, you must explicitly declare all variables using the Dim, Private, Public, ReDim, or Static statements. If you attempt to use an undeclared variable name, an error occurs atcompile time.

    If you don't use the Option Explicit statement, all undeclared variables are of Variant type unless the default type is otherwise specified with a Deftype statement.
    On the filemenu click Tools, then Options and check the check box "Require Variable Delaration"....

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