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

2013/9/1

Top Down Walls

dominicarrojado dominicarrojado

2013/9/1

#
I know this has been discussed alot but can't find what I'm looking for. How can I make a Top Down Wall that can't be passed from either top down left right? Thanks in advance. Xd
danpost danpost

2013/9/1

#
The simplest code for what you want depends on certain things about your scenario; mainly the following: (1) the movement code for your actor(s) (including instance fields used) (2) the size of your actor(s) and wall(s) Showing what code you have so far with respect to the movement of your actor(s) would help.
SPower SPower

2013/9/1

#
Something like this:
// the code where you move
if (Greenfoot.isKeyDown("left")) {
    Actor a = getOneObjectAtOffset(-1,0,Wall.class); // get an object of the class Wall, at 1 cell to the left and 0 cells up or down
    if (a == null) { // no wall
        // move the player
    }
}
// the same for other directions
for more info, look at the documentation, especially the Actor class documentation: http://www.greenfoot.org/files/javadoc/
dominicarrojado dominicarrojado

2013/9/1

#
import greenfoot.*; /** * The Robot class will create a user controlled robot that cannot go through barriers */ public class Player extends Actor { // The following field will be controlled by a bar that will tell the world to change it private int speed = 1; public static int currentX; public static int currentY; /** * Method act: * checks for keypresses and moves the robot the appropriate speed until it comes upon a barrier */ public void act() { for (int i = 0; i < speed; i++) move(); currentX = getX(); currentY = getY(); } /** * Method move: * checks for keypresses and moves the robot one unit unless a barrier is present; * will be called multiple times when the speed is increased */ private void move() { 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++; setLocation(getX() + dx, getY()); if (getOneIntersectingObject(Wall.class) != null) { if (dy == 0) { setLocation(getX(), getY() + 1); if (getOneIntersectingObject(Wall.class) == null) return; setLocation(getX(), getY() - 2); if (getOneIntersectingObject(Wall.class) == null) return; setLocation(getX() - dx, getY() + 1); return; } setLocation(getX() - dx, getY()); } setLocation(getX(), getY() + dy); if (getOneIntersectingObject(Wall.class) != null) { if (dx == 0) { setLocation(getX() + 1, getY()); if (getOneIntersectingObject(Wall.class) == null) return; setLocation(getX() - 2, getY()); if (getOneIntersectingObject(Wall.class) == null) return; setLocation(getX() + 1, getY() - dy); return; } setLocation(getX(), getY() - dy); } } /** * Method setSpeed: * recieves and sets a new speed value; * called by the world whenever a value of a bar class object changes; * * @param newSpeed A parameter to set the speed to */ public void setSpeed(int newSpeed) { speed = newSpeed; } } Already done this to my player but what about the enemy following it? You remembered the enemy following the player up down left right? How can I apply the same to the enemy?
SPower SPower

2013/9/1

#
I think the code I gave you should apply to your enemy class (with minor differences though), considering your dicussion about following the player.
danpost danpost

2013/9/1

#
Do you want the Player object also to only move up down left right?
dominicarrojado dominicarrojado

2013/9/1

#
Yes
danpost danpost

2013/9/1

#
Then you over-complicated it by splitting up the movement horizontally from the movement vertically. A simple move method would be:
public void move()
{
    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++;
    // do nothing if conflicting,invalid or no input given
    if (dx*dy != 0 || dx+dy == 0) return;
    // attempt move
    setLocation(getX()+dx, getY()+dy);
    // if intersecting wall, move back
    if (getOneIntersectingObject(Wall.class) != null) setLocation(getX()-dx, getY()-dy);
}
dominicarrojado dominicarrojado

2013/9/1

#
Ooops. Thanks. Xd But what about the following enemy?
danpost danpost

2013/9/1

#
What about the following enemy? Was that not taken care of in the other discussion thread?
dominicarrojado dominicarrojado

2013/9/1

#
Yes but if the enemy hit a wall?
SPower SPower

2013/9/1

#
Maybe you find bourne's scenario called pathfinding helpful: http://www.greenfoot.org/scenarios/8226
danpost danpost

2013/9/1

#
Please refer to the 'Enemy Follow Player' discussion thread.
You need to login to post a reply.