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

2015/5/27

How to make my gunman player to move Forward, Backwards, Right and left?

_E_X_B_R_Arjay _E_X_B_R_Arjay

2015/5/27

#
Im new here in Greenfoot and im working on a project for myself about a game and im having trouble making my player "Gunman" move forward or back wards n stuff like that. so i tried these codes for movement but it seems theyre for rotating but i want the player to just move forward and backwards n stuff. Need help + thx for commenting. #Arjay
danpost danpost

2015/5/27

#
_E_X_B_R_Arjay wrote...
so i tried these codes for movement
What codes? We cannot fix unseen codes (read the 'Posting code? read this!' link below the reply box before posting any codes).
_E_X_B_R_Arjay _E_X_B_R_Arjay

2015/5/27

#
public void act() { move(4); checkKeys(); tryToEat(); } /** * Check whether the control keys are being pressed, and turn * if they are. */ public void checkKeys() { if ( Greenfoot.isKeyDown("left") ) { turn(-5); } if ( Greenfoot.isKeyDown("right") ) { turn(5); } }
_E_X_B_R_Arjay _E_X_B_R_Arjay

2015/5/27

#
Danpost i just figured out how to do it. so nevermind. but IF ANYONE HERE WANTS A CODE THAT MAKES PLAYER GO (FORWARD BACKWARDS LEFT AND RIGHT) heres the code
/**
     * Act - do whatever the Survivor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveonKeyPress();
    }    
    
    public void moveRight()
    {
        int x = (getX() + 4);
        int y = (getY());
        
        setLocation(x, y);
    }
    
    public void moveLeft()
    {
        int x = (getX()- 4);
        int y = (getY());
        
        setLocation(x, y);
    }
    
    public void moveDown()
    {
        int x = (getX());
        int y = (getY() + 4);
        
        setLocation(x, y);
    }
    
    public void moveUp()
    {
        int x = (getX());
        int y = (getY() -4);
        
        setLocation(x, y);
    }
    
    public void moveonKeyPress()
    {
      if(Greenfoot.isKeyDown("left"))
     {
      moveLeft();
     }
     if(Greenfoot.isKeyDown("right"))
     {
        moveRight();
     }
     if(Greenfoot.isKeyDown("up"))
     {
        moveUp();
     }
     if(Greenfoot.isKeyDown("down"))
     {
       moveDown();
     }
        
   

    }
Super_Hippo Super_Hippo

2015/5/27

#
If you want a shorter version:
public void act()
{
    int dx=0, dy=0;
    if(Greenfoot.isKeyDown("left")) dx--;
    if(Greenfoot.isKeyDown("right")) dx++;
    if(Greenfoot.isKeyDown("up")) dy--;
    if(Greenfoot.isKeyDown("down")) dy++;
    if (dx!=0 || dy!=0) setLocation(getX()+dx*4, getY()+dy*4);
}
You need to login to post a reply.