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

2017/3/15

What am I missing? moving one at a time...

alittle_bit alittle_bit

2017/3/15

#
Good day, For the past half a day, I've fixed, changed and reopened greenfoot in order to get to the point I am at with still the same problem(though probably fixed some other ones in the process). The game reports an error/crash when I place the 'Player' in the world and run the scenario due to (what I am sure is te problem) the following lines of code:
    public void act() 
    {
        while(Greenfoot.isKeyDown(null))//prevents the player moving more than once per button press
        {
            movePlayer(); //see method below, moves the player
        }
        if(!Greenfoot.isKeyDown(null)) //this line permits the player to move again
        {
            move = false;
        }
    }
My assumption is that, since the scenario immediately stops only when I have just started with a 'Player object' in the world, the error has to be either in the instantiation of the 'Player' (though the error would be when you add the object to the world in that case) or in the first thing that runs, which is the act method. In case I am wrong, I will provide part of the 'movePlayer()' method below:
    public void movePlayer()
    {
       if(Greenfoot.isKeyDown("w") && move==true) //Moves player up
       {
              x = getX();
              y = getY();
              setLocation(x, (y-speedSet()) );
              if(isTouching(Wall.class))
              {
                  setLocation (x,y+speedSet());
              }
       }
Hope some clear conclusion can be made so that this error does not happen again! (also, are there any similar threads on the same mistake?) Thank you!
danpost danpost

2017/3/15

#
One problem is the improper use of 'isKeyDown' which requires a String key value in its parameter. Using 'null' for the parameter makes no sense. If you change that and add a key string to the parameter, then the next thing is the 'while' loop, which will stop everything except the player (if the player can move and the "w" key is down (conditions in the 'movePlayer' method -- otherwise everything will be on hold) until the key is released. I think you are just overcomplicating things by trying to tell the player not to move. It will not move unless you tell it to move -- you do not have to tell it to stop -- just place the condition on moving. If I am correct, you do not need any conditions on calling 'movePlayer' in the act method and you do not need the boolean 'move' field. Just simply this:
public void act()
{
    movePlayer();
}

public void movePlayer()
{
    if (Greenfoot.isKeyDown("w"))
    {
        x = getX();
        y = getY();
        int dy = setSpeed();
        setLocation(x, y-dy);
        if (isTouching(Wall.class))
        {
            setLocation(x, y+dy);
        }
    }
}
Imagine an 'else' part to the 'if' block with no implementation:
else
{
}
That, in essence, is what "tells" it not to move. There is another small issue. It is in the way that you declare the 'x' and 'y' and use 'setLocation' to move AND back off of the walls. The code says to move forward and, if a wall is contacted, move backwards INSTEAD of forward. To have the player move back to its starting position before moving, change line 16 in my code here to:
setLocation(x, y);
You need to login to post a reply.