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

2014/5/2

mover actor cada un cuadrado

mi.lopezm mi.lopezm

2014/5/2

#
I have a problem with an actor that I am programming need that a square moves only simultaneously initially when empece to programming it it was advancing up to the ceiling of the world now I have these attributes put
public class Sheriff extends Actor
{
    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;
   
    /**
     * Move one cell forward in the current direction.
     */
    public void move()
    {
        if (!canMove()) {
            return;
        }
        
        switch(direction) 
        {
            case SOUTH :
                setLocation(getX(),  getY() +1);
                break;
            case EAST :
                setLocation(getX() +1, getY() );
                break;
            case NORTH :
                setLocation(getX(),  getY() -1);
                break;
            case WEST :
                setLocation(getX() -1, getY() );
                break;
        }
    }
    
    /**
     * Test if we can move forward. Return true if we can, false otherwise.
     */
    public boolean canMove()
    {
        World myWorld = getWorld();
        int x = getX();
        int y = getY();
        
        switch(direction) 
        {
            case SOUTH :
                y++;
                break;
            case EAST :
                x++;
                break;
            case NORTH :
                y--;
                break;
            case WEST :
                x--;
                break;
        }
        // test for outside border
        if (x >= myWorld.getWidth() || y >= myWorld.getHeight()) 
        {
            return false;
        }
        else if (x < 0 || y < 0) 
        {
            return false;
        }
        return true;
    }
    
    /**
     * Act - hace lo que Sheriff quiere hacer. Este método se llama "cuando quiera" o whenever
     * los botones 'Actuar or 'Ejecutar' son presionados en el entorno.
     */
    public void act() 
    {
        int x = getX (); 
	    int y = getY ();
	    

        if (Greenfoot.isKeyDown("up"))
        {
          setLocation (x , y - 1);
        }
        
        if (Greenfoot.isKeyDown("down"))
        {
          setLocation (x , y + 1);
        }
        
        if (Greenfoot.isKeyDown("left"))
        { 
          setLocation (x - 1, y);
        }
        
        if (Greenfoot.isKeyDown("right"))
        {
	      setLocation (x + 1, y);
        }
        
        move();
    }    
}
My problem is that when it initiates always it me goes away until the end of the world and is I press the arrow up or below and it rises and goes down according to the arrow but I treat that it moves to the left side and the same thing does not become detached of the end of the world happens to me when I add:
public Sheriff()
{
  direction = WEST;
}
Someone can help me please
danpost danpost

2014/5/2

#
Remove the 'move();' statement at line 102 in your Sheriff code post above.
You need to login to post a reply.