Results 1 to 6 of 6

Thread: if statement

  1. #1

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544

    if statement

    I'm using the following if statement in my game, but I can only move the player to the left, not to the right....what is wrong??

    PHP Code:
    if (KEY_DOWN(VK_LEFT))
    if (!
    paddle.<=0)
        
    paddle.paddle.-10;
        
    Move_BOB(&paddle);
    if(
    KEY_DOWN(VK_RIGHT))
        if(!
    paddle.SCREEN_WIDTH)
            
    paddle.paddle.+10;
            
    Move_BOB(&paddle); 
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169

    Re: if statement

    Originally posted by CyberCarsten
    I'm using the following if statement in my game, but I can only move the player to the left, not to the right....what is wrong??

    PHP Code:
    if (KEY_DOWN(VK_LEFT))
    if (!
    paddle.<=0)
        
    paddle.paddle.-10;
        
    Move_BOB(&paddle);
    if(
    KEY_DOWN(VK_RIGHT))
        if(!
    paddle.SCREEN_WIDTH)
            
    paddle.paddle.+10;
            
    Move_BOB(&paddle); 
    Some judicious insertion of braces later:
    Code:
    if (KEY_DOWN(VK_LEFT)) {
        if (paddle.x > 0) {
            paddle.x = paddle.x -10;
            Move_BOB(&paddle);
        }
    } else if(KEY_DOWN(VK_RIGHT)) {
        if(paddle.x < SCREEN_WIDTH) {
            paddle.x = paddle.x +10;
            Move_BOB(&paddle);
        }
    }
    If you miss out the braces it only takes the next statement, meaning that in your first, the Move_BOB would be executed whether it was VK_LEFT or any other.

    The modified one should work...

    Edit: I also changed your comparison expressions, they were comparing a boolean value to a number (precedence of the ! operator).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    Now I can move it both ways, but it also goes outside the screen...
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Perhaps altering the checks to take the size of the paddle into account?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    I changed your suggestion for the right movement and I got it to stop when it reached the right end of the screen, but when I press Left it just continues out to the left of the screen...

    PHP Code:
    if (KEY_DOWN(VK_LEFT)) {
        if (
    paddle.0) {
            
    paddle.paddle.-10;
            
    Move_BOB(&paddle);
        }
    } else if(
    KEY_DOWN(VK_RIGHT)) {
        if(
    paddle.paddle.width  SCREEN_WIDTH) {
            
    paddle.paddle.+10;
            
    Move_BOB(&paddle);
        }

    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Code:
    if (KEY_DOWN(VK_LEFT)) {
        paddle.x = paddle.x -10;
        if (paddle.x < 0)
            paddle.x = 0;
        Move_BOB(&paddle);
    }
    This should work.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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