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

2013/5/11

Need some help with basic pacman

jaydjay jaydjay

2013/5/11

#
never mind solved it
GreenGoo GreenGoo

2013/5/11

#
Your method doesn't really make sense. The move() method moves the actor in the direction it is facing, which is typically changed using the turn() method, but you have instead combined move() and setLocation(). It can only really be one or the other.
jaydjay jaydjay

2013/5/11

#
I am trying to make my character move a direction until it hits a wall, how can i do this? When i tried this it just teleports to the end of the wall public void move() { if ( Greenfoot.isKeyDown("up") ) { setDirection(UP) ; while( !wallFront() ) { move(3); }
holla holla

2013/5/11

#
Just change your while loop to if, because a while loop is a loop, so you dont see any steps until your object reaches the wall.
jaydjay jaydjay

2013/5/12

#
holla wrote...
Just change your while loop to if, because a while loop is a loop, so you dont see any steps until your object reaches the wall.
I want it to be like pacman, showing the motion while moving, i have changed it to if and you need to hold onto the arrow key to move. I want it to be like pacman where you press left and it continues
danpost danpost

2013/5/12

#
You will need two instance int fields to hold the current direction of movement and the last detected change in direction. First check the arrow keys and set any detected change in direction. Then, if change in direction equals current direction, continue in current direction if not blocked. If change in direction is not equals to current direction, then if blocked, continue in current direction, else move in new direction and set the field holding the new current direction.
// using instance fields
private int curDir;
private int nxtDir;
// with this code
int dx = 0, dy = 0;
if (Greenfoot.isKeyDown("right")) dx++;
if (Greenfoot.isKeyDown("left")) dx--;
if (Greenfoot.isKeyDown("down")) dy++;
if (Greenfoot.isKeyDown("up")) dy--;
if (dx * dy == 0 && dx + dy != 0) nxtDir = (1-dx)*(dx*dx)+(2-dy)*(dy*dy);
if (canMove(nxtDir)) curDir = nxtDir;
if (canMove(curDir)) moveOn(curDir);

// with methods
private boolean canMove(int dir)
{
    setRotation(90*dir);
    move(2);
    boolean noObjs = getIntersectingObjects(Wall.class).isEmpty();
    move(-2);
    setRotation(0);
    return noObjs;
}

private void moveOn(int dir)
{
    setRotation(90*dir);
    move(2);
    setRotation(0);
}
Let me explain this one statement: if (dx * dy == 0 && dx + dy != 0) nxtDir = (1-dx)*(dx*dx)+(2-dy)*(dy*dy); Basically, the statement says "if one or the other of 'dx' and 'dy' are zero AND both are not zero, then save the direction directed by the state of the arrow keys. The value assigned to 'nxtDir' will be the angle directed divided by 90 (0, 1, 2, or 3).
danpost danpost

2013/5/12

#
It seems a bit wasteful moving back and forth, like that, we could replace the code from line 11 on with:
if (moves(nxtDir)) curDir = nxtDir; else moves(curDir);
 // with just the one method
private boolean moves(int dir)
{
    setRotation(90*dir);
    move(2);
    boolean noObjs = getIntersectingObjects(Wall.class).isEmpty();
    if (!noObjs) move(-2);
    setRotation(0);
    return noObjs;
}
You need to login to post a reply.