locked
Prevent mouvement with arrow keys RRS feed

  • Question

  • So I have a timer that checks collisions, but I can't get it so that when the timer finds a collision, the player can't move with the arrow keys anymore.
    Saturday, December 28, 2013 3:09 PM

Answers

  • Here is beauty of using flags. Let's call the boolean flag "isCollision" for example

    Dim isCollision as Boolean

    . . . . . . .  then in the event code which detects the collision - also set the flag isCollision = True

    . . .  . .  . then in the keypress events which dectect key presses - add the following early in the code - like

        Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            If isCollision Then Exit Sub


    Leon C Stanley - - A dinky di VBer - - Code is like fresh baked bread - it goes stale after a while - - so why try to immortalize it ? and turn it into a Class? ^^ :-)

    Sunday, December 29, 2013 5:45 AM

All replies

  • So I have a timer that checks collisions, but I can't get it so that when the timer finds a collision, the player can't move with the arrow keys anymore.

    This must be one of those guessing game posts. Where no code is provided. Therefore it's a big guess. So attach pieces of tin foil to the left and right side of each arrow key. Seperated by a hair. Then hook those up to a 120 volt USB controlled switch plugged into a wall outlet. With the switch sides hot on the left and neutral on the right piece of each tin foil strip on each arrow key. Write code to turn the switch on whenever there is a collision. After the first time a collision occurs the user will press an arrow key. This will only happen one time unless the user is a sadomasochist.

    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.

    Sunday, December 29, 2013 2:39 AM
  • Here is beauty of using flags. Let's call the boolean flag "isCollision" for example

    Dim isCollision as Boolean

    . . . . . . .  then in the event code which detects the collision - also set the flag isCollision = True

    . . .  . .  . then in the keypress events which dectect key presses - add the following early in the code - like

        Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            If isCollision Then Exit Sub


    Leon C Stanley - - A dinky di VBer - - Code is like fresh baked bread - it goes stale after a while - - so why try to immortalize it ? and turn it into a Class? ^^ :-)

    Sunday, December 29, 2013 5:45 AM
  • So at the beginning of the KeyDown event write " If isCollision Then Exit Sub " so if the boolean is triggered off it will skip the whole keydown process?
    Monday, December 30, 2013 3:41 PM
  • Hi,

     That would depend on if you want your character to stop moving no mater what if it comes into contact with something or if you want to be able to move in the other directions but not in the direction that it is contacting something. Maybe the code of your KeyDown event and/or a clearer explanation of how you want the character to be able to move would help us understand better.

    EDIT : I wanted to give you this link which does not really have much to do with your question but, you may want to look at. It was written by one of our very good moderators Reed Kimble that gives some very good tips on making games in VB.NET.

    How to Create Video Games in VB.Net (Windows Forms)

    • Edited by IronRazerz Monday, December 30, 2013 4:05 PM
    Monday, December 30, 2013 3:55 PM
  • So I am trying to make the game Frogger, where a frog tries to cross a road with cars to get to safety. When he hits a car he dies, so I don't want the player to be able to move at all until he respawns to try again.
    Monday, December 30, 2013 4:09 PM
  • So I am trying to make the game Frogger, where a frog tries to cross a road with cars to get to safety. When he hits a car he dies, so I don't want the player to be able to move at all until he respawns to try again.

     As long as you are using the Forms KeyDown event and there is not anything else in the KeyDown event that needs to be done even if the frog is hit then that should work by placing it at the top of the KeyDown event. That is if you are setting the "isCollision" boolean to True when the frog is hit and then back to False again when the frog is given another life.



    • Edited by IronRazerz Monday, December 30, 2013 4:25 PM
    Monday, December 30, 2013 4:22 PM
  • Here is beauty of using flags. Let's call the boolean flag "isCollision" for example

    Dim isCollision as Boolean

    . . . . . . .  then in the event code which detects the collision - also set the flag isCollision = True

    . . .  . .  . then in the keypress events which dectect key presses - add the following early in the code - like

        Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            If isCollision Then Exit Sub


    Leon C Stanley - - A dinky di VBer - - Code is like fresh baked bread - it goes stale after a while - - so why try to immortalize it ? and turn it into a Class? ^^ :-)

    You can use "FrogCanMove" as the name of the boolean. Then in the keypress check if the flag is true or false.

    You then can set this boolean to true when the frog respawns.

    Monday, December 30, 2013 11:12 PM