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

2012/8/14

Movement help

Mongoose Mongoose

2012/8/14

#
Hello! im new in here and i been working with simple process so far. I need help with movement of my Actor. I have one class called Bucket and i want to make it like a "wall" i mean i dont want my character to step on the bucket. Sorry for my english and thanks for reading this.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;

/**
 * Write a description of class player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    private GreenfootImage playerNorth;
    private GreenfootImage playerSouth;
    private GreenfootImage playerWest;
    private GreenfootImage playerWest1;
    private GreenfootImage playerEast;
    private boolean step;
    private boolean down;
    private static final int EAST = 0;
    private static final int WEST = 1;
    private static final int NORTH = 2;
    private static final int SOUTH = 3;
    private int direction;   
    
    /**
     * Act - do whatever the player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {       
        processKeys();
    }    
     /**
     * Handle keyboard input.
     */
    private void processKeys()
    {         
       int dx = 0, dy = 0;
       if (Greenfoot.isKeyDown("up")) moveNorth();
       if (Greenfoot.isKeyDown("down")) moveSouth();
       if (Greenfoot.isKeyDown("left")) moveWest();
       if (Greenfoot.isKeyDown("right")) moveEast();     
       if (getOneIntersectingObject(Bucket.class) != null){ 
         switch(direction) {
            case SOUTH :
                setLocation(getX(), getY()-5);              
            case EAST :
                setLocation(getX()-5, getY());         
            case NORTH :
                setLocation(getX(), getY()+5);        
            case WEST :
                setLocation(getX()+5, getY());        
        }
    }
    }
    private void moveWest()
    {
           if ( step == true) 
           {
           setImage("playerWest1.png");
           step = false;
           }
           else if( step == false) {
            setImage("playerWest2.png");
            step = true;
            }
            else{
            setImage("playerWest.png");    
            }        
           setLocation(getX()-5, getY());
        } 
        private void moveEast() {
            if ( step == true) 
           {
            setImage("playerEast1.png");
           step = false;
           }
           else if( step == false) {
            setImage("playerEast2.png");
            step = true;
            }
            else{
            setImage("playerEast.png");    
            }                
           setLocation(getX()+5, getY());
        }  
    
    private void moveSouth(){
    if(Greenfoot.isKeyDown("down")) {
           if ( step == true) 
           {
            setImage("playerSouth1.png");
           step = false;
           }
           else if( step == false) {
            setImage("playerSouth2.png");
            step = true;
            }
            else{
            setImage("playerSouth.png");    
            }           
           setLocation(getX(), getY()+5);
        } 
    }
    
    private void moveNorth(){
    if(Greenfoot.isKeyDown("up")){
            if ( step == true) 
           {
            setImage("playerNorth1.png");
           step = false;
           }
           else if( step == false) {
            setImage("playerNorth2.png");
            step = true;
            }
            else{
            setImage("playerNortht.png");    
            }                      
            setLocation(getX(), getY()-5);       
        }
     }     
    }
danpost danpost

2012/8/15

#
I think the main problem is that nowhere are you setting 'direction' which should probably be done with each of the four methods for the four different directions. A couple of things I noticed looking over the code. In a couple of the movement methods (of those mentioned above) you are asking if the key is down AGAIN. Not needed. Next the last 'else' parts of those methods will never be reached as 'step' is either 'true', or it is 'false'; no other choices. I think you started to go with an idea in the 'processKeys' method, but did not follow through. Your first line (int dx = 0, dy = 0;) declares two integers that are never used. Had you continued along that route (and set up your variables in a way to make them easy to work with), you might have ended up with something more in the line of the following (I realize that some of the code may be a little over your head, but if you think it through carefully, you might come to some understanding of it)
import greenfoot.*; // only one you were using

public class Player extends Actor
{
    String[][] imageName = { { "playerEast1.png", "playerEast2.png" },
                                             { "playerSouth1.png", "playerSouth2.png" },
                                             { "playerWest1.png", "playerWest2,png" },
                                             { "playerNorth1.png", playerNorth2.png" } };
    private int step = 0; // this is better as an int
    private int direction = 0;  // did not need the 'final int's
    // actually do not need 'direction' here, either
    // but I left it in there
    
    public void act() 
    {       
        processKeys();
    }

     /**
     * Handle keyboard input.
     */
    private void processKeys()
    {         
        int dx = 0, dy = 0;
        if (Greenfoot.isKeyDown("up")) dy--;
        if (Greenfoot.isKeyDown("down")) dy++;
        if (Greenfoot.isKeyDown("left")) dx--;
        if (Greenfoot.isKeyDown("right")) dx++;
        if (dx == 0 && dy == 0) return; // no movement
        if (dx * dy != 0) return; // conflicting movement (diagonal)
        // only combinations of dx and dy at this point are
        //  1, 0;   0, 1;  -1, 0 ;  and  0, -1
        setLocation(getX() + dx * 5, getY() + dy * 5); // move
        direction =   (1 - dx) * dx * dx + (2 - dy) * dy * dy;
        // direction: 0 =EAST; 1 = SOUTH; 2 = WEST;  3 = NORTH)
        if (getOneIntersectingObject(Bucket.class) != null) setLocation(getX() - dx * 5, getY() - dy * 5);
        // moves back if Bucket present ^^

        // because of the way I set up the variables,
        // the following two statements replace all
        // four of your movement methods
        step = (1 + step) % 2; // value alternates between 0 and 1
        setImage(imageName[direction][step]);
    }
}
Mongoose Mongoose

2012/8/15

#
Thank you sir. I'm going to keep working hard!
You need to login to post a reply.