CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2011
    Posts
    1

    If statement help

    Hey,
    i have a question about if statements. I am trying to write a code for movement of a sprite. I want to have different acceleration for the y direction and x direction. I believe I am very close in getting the kind of motion i need, there is just one problem i am having.

    Here's an example of what I'm trying to do:

    if (currentKeyboardState.IsKeyUp(Keys.A) (but not) (currentKeyboardState.IsKeyDown(Keys.D))
    {
    Do.Something
    }

    //Basically i need some kind of function to put where the (but not) is. So if the A key is up and the D key is down i don't want the "Do.Something" to run. If anyone could help that would be great.

  2. #2
    Join Date
    Feb 2008
    Posts
    108

    Re: If statement help

    Try this:
    if ((currentKeyboardState.IsKeyUp(Keys.A)) && (!currentKeyboardState.IsKeyDown(Keys.D)))
    Developing using:
    .NET3.5 / VS 2010

  3. #3
    Join Date
    Dec 2008
    Posts
    144

    Re: If statement help

    ^this.

    using the and (&&) and or(||) operators in a conditional allows you to build in a lot of extra complexity.

    Also, the bang(!) is the symbol for not. Since the assumption for the IsKeyDown method is that it returns a bool, the bang can be attached to the object method. In other cases you'll need to use it as part of an evaluation, like:
    Code:
    if(someVariable != null)
        DoStuff();
    HTH
    Code:
    if (Issue.Resolved)
    {
         ThreadTools.Click();
         MarkThreadResolved();
    }

  4. #4
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: If statement help

    Quote Originally Posted by Jim_Auricman View Post
    Try this:
    if ((currentKeyboardState.IsKeyUp(Keys.A)) && (!currentKeyboardState.IsKeyDown(Keys.D)))
    Just to help you understand the syntax, the above is the same as:

    Code:
    if (currentKeyboardState.IsKeyUp(Keys.A) == true && currentKeyboardState.IsKeyDown(Keys.D) == false)
    It's not a bug, it's a feature!

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

Featured