This site requires JavaScript, please enable it in your browser!
Greenfoot back
Student2394
Student2394 wrote ...

2018/8/29

Need help

Student2394 Student2394

2018/8/29

#
I am trying to make a basic movement system but my if statements will not work for some reason I can only get one of the iskeydown statements running at a time
public void Run(){
        if (IsOnGround == true){
            if (Greenfoot.isKeyDown("D")){
                movementspeed += 1;
                IsMoving = true;
        }
            if (Greenfoot.isKeyDown("A")){
                movementspeed -= 1;
                IsMoving = true;
        }
            else{
                IsMoving = false;
                if (movementspeed > 0){
                   movementspeed -= 1;
                }
                if (movementspeed < 0){
                   movementspeed += 1;
                }
            }
        }
    }
Student2394 Student2394

2018/8/29

#
currently the second statement works but not the first an elseif statement reverses this and movement is just setLocation(getX()+movementspeed,getY());
danpost danpost

2018/8/29

#
Student2394 wrote...
I am trying to make a basic movement system but my if statements will not work for some reason I can only get one of the iskeydown statements running at a time << Code Omitted >>
As is, your else part will execute regardless of the state of the D key (since else is linked only to the A key part). Move line 12 to the beginning of the method and then instead of else, just ask if it is still false at line 11.
You need to login to post a reply.