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

2016/2/29

Help, can't figure out how to detect walls in my open world

ccnetminder ccnetminder

2016/2/29

#
I am trying to create walls for my actors to interact with in a 2D scrolling world. I have gotten the world to move as the actor does, but I am hitting all sorts of problems when it comes to making the actor to stop at the wall. So far I have made it so the wall stops the actor, but then the world doesn't scroll around with it. I also accidentally made it so the wall scrolls with the world and doesn't actually collide with the actor (not really sure how that happened). Here's my code for the actor successfully moving around the world with the world moving: { private int speedX = 1; private int speedY = 1; private static final int SPEED = 5; private static final int BOUNDARY = 40; public void act() { handleKeyPresses(); boundedMove(); } private void handleKeyPresses() { handleArrowKey("left", -SPEED, 0); handleArrowKey("right", SPEED, 0); handleArrowKey("up", 0, -SPEED); handleArrowKey("down", 0, SPEED); } private void handleArrowKey(String k, int sX, int sY) { if( Greenfoot.isKeyDown(k) ) { speedX = sX; speedY = sY; } } private void boundedMove() { if( speedX+getX() <= BOUNDARY ) { setLocation(BOUNDARY, getY()); ((HikingWorld2D)getWorld()).shiftWorld(-speedX, 0); } else if( speedX+getX() >= getWorld().getWidth()-BOUNDARY ) { setLocation(getWorld().getWidth()-BOUNDARY, getY()); ((HikingWorld2D)getWorld()).shiftWorld(-speedX, 0); } else { setLocation(getX()+speedX, getY()); } if( speedY+getY() <= BOUNDARY ) { setLocation(getX(), BOUNDARY); ((HikingWorld2D)getWorld()).shiftWorld(0, -speedY); } else if( speedY+getY() >= getWorld().getHeight()-BOUNDARY ) { setLocation(getX(), getWorld().getHeight()-BOUNDARY); ((HikingWorld2D)getWorld()).shiftWorld(0, -speedY); } else { setLocation(getX(), getY()+speedY); } speedX = 0; speedY = 0; } } and here is where I have some errors in my code trying to stop the actor at a wall
public class Hiker extends Actor
{
    private int speedX = 1;
    private int speedY = 1;
    private static final int SPEED = 5;
    private static final int BOUNDARY = 40;

    public void act() {
        handleKeyPresses();
        boundedMove();
    }

    private void handleKeyPresses(int moveAmt) {
        int dx = 0, dy = 0;
        if(handleArrowKey("left", -SPEED, 0)) dx -= 1;
        if(handleArrowKey("right", SPEED, 0)) dx += 1;
        if(handleArrowKey("up", 0, -SPEED)) dy -= 1;
        if(handleArrowKey("down", 0, SPEED)) dy += 1;
        
                for (int i = 0; i < moveAmt; i++)
        {
            setLocation(getX() + dx, getY());
            if (getOneIntersectingObject(Wall.class) != null) setLocation(getX() - dx, getY());
            setLocation(getX(), getY() + dy);
            if (getOneIntersectingObject(Wall.class) != null) setLocation(getX(), getY() - dy);
        }
    }

    private void handleArrowKey(String k, int sX, int sY) {
        if( Greenfoot.isKeyDown(k) ) {
            speedX = sX;
            speedY = sY;
        }
    }

    private void boundedMove() {

        if( speedX+getX() <= BOUNDARY ) {
            setLocation(BOUNDARY, getY());
            ((HikingWorld2D)getWorld()).shiftWorld(-speedX, 0);
        } else if( speedX+getX() >= getWorld().getWidth()-BOUNDARY ) {
            setLocation(getWorld().getWidth()-BOUNDARY, getY());
            ((HikingWorld2D)getWorld()).shiftWorld(-speedX, 0);
        } else {
            setLocation(getX()+speedX, getY());
        }

        if( speedY+getY() <= BOUNDARY ) {
            setLocation(getX(), BOUNDARY);
            ((HikingWorld2D)getWorld()).shiftWorld(0, -speedY);
        } else if( speedY+getY() >= getWorld().getHeight()-BOUNDARY ) {
            setLocation(getX(), getWorld().getHeight()-BOUNDARY);
            ((HikingWorld2D)getWorld()).shiftWorld(0, -speedY);
        } else {
            setLocation(getX(), getY()+speedY);
        }
        speedX = 0;
        speedY = 0;
    }
}
I haven't really messed with the boundMove() at all because that works already. Thanks for the help!
danpost danpost

2016/2/29

#
As coded, your 'handleKeyPresses' method needs an int argument ( 'moveAmt' ); but, line 9 does not give a value for that argument. By the way, you said you got errors, but you did not post what they were. It would help if next time you show what error you are getting.
You need to login to post a reply.