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

2014/1/17

Movement Help

JasonZhu JasonZhu

2014/1/17

#
I am going into walls in my maze game when I press 2 directional keys simultaneously. When I only use one directional key, it's fine. This is the code of my Player class: My World is 30,30,20.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.util.List;

public class Player extends Actor
{   
    private static final int EAST = 0;
    private static final int SOUTH = 90;
    private static final int WEST = 180;
    private static final int NORTH = 270;    

    private static final int SIZE = 20;
    private boolean canMove;
    private int dx;
    private int dy;

    public Player()
    {
        GreenfootImage img = new GreenfootImage(SIZE,SIZE);
        img.setColor(Color.WHITE);
        img.fill();
        setImage(img);
        dx = 0;
        dy = 0;
    }

    public void act() 
    {
        movement();
        wallInteraction();
    }   

    private void movement()
    {
        if(Greenfoot.isKeyDown("up")&&!facingUpWall()){
            setRotation(NORTH);
            move(1);
            dx = 0;
            dy = -1;
        }
        if(Greenfoot.isKeyDown("right")&&!facingRightWall()){
            setRotation(EAST);
            move(1);
            dx = 1;
            dy = 0;            
        }
        if(Greenfoot.isKeyDown("left")&&!facingLeftWall()){
            setRotation(WEST);
            move(1);
            dx = -1;
            dy = 0;
        }
        if(Greenfoot.isKeyDown("down")&&!facingDownWall()){
            setRotation(SOUTH);
            move(1);
            dx = 0;
            dy = 1;
        }     
    }

    private void wallInteraction()
    {
        Wall w = (Wall)getOneObjectAtOffset(dx,dy,Wall.class);
        if(w!=null){
            Color theColor = w.getMyColor();
            List<Wall> maze = getWorld().getObjects(Wall.class);
            for(Wall part:maze){
                if(part.getMyColor().equals(theColor)){
                    part.show();
                }
            }
        }
    }

    private boolean facingUpWall()
    {
        int xOffset=0, yOffset=0;
        switch( getRotation() ) {
            case NORTH: yOffset=-1; break;
        }
        Actor wall = getOneObjectAtOffset(xOffset,yOffset,Wall.class);
        return wall != null;        
    }

    private boolean facingRightWall()
    {
        int xOffset=0, yOffset=0;
        switch( getRotation() ) {
            case EAST: xOffset=1; break;
        }
        Actor wall = getOneObjectAtOffset(xOffset,yOffset,Wall.class);
        return wall != null;        
    }

    private boolean facingLeftWall()
    {
        int xOffset=0, yOffset=0;
        switch( getRotation() ) {
            case WEST: xOffset=-1; break;
        }
        Actor wall = getOneObjectAtOffset(xOffset,yOffset,Wall.class);
        return wall != null;        
    }

    private boolean facingDownWall()
    {
        int xOffset=0, yOffset=0;
        switch( getRotation() ) {
            case SOUTH: yOffset=1; break;
        }
        Actor wall = getOneObjectAtOffset(xOffset,yOffset,Wall.class);
        return wall != null;        
    }    
}
Is there anyway to fix this with my cell-like world set-up? Help greatly appreciated!
erdelf erdelf

2014/1/17

#
just place an else before the right, left and down if line, and i think it should work
Zamoht Zamoht

2014/1/17

#
Changing the if statements in the movement method to else if should fix it I believe.
JasonZhu JasonZhu

2014/1/17

#
I tried that, it doesn't provide a fix. I think what's happening is that the player moves before it actually checks whether it can or not. If there is at least 2 cells before the wall and I move at it, its fine.
JasonZhu JasonZhu

2014/1/17

#
In fact, I'm certain that's the problem after some testing.
Dragonblazze Dragonblazze

2014/1/17

#
Hey man i just made a maze-like game and i had the same problems the trick is you have to code for the chances that you are next to several walls. meaning there are walls on multiple sides. you have to add extra code relating towards multiple walls as well as singular walls.
JasonZhu JasonZhu

2014/1/17

#
Thanks for your reply, but this is happening for single faced walls as well. If I scale a wall beside it and then pressed the respective arrow key to move towards the wall, I end up going into the wall.
JasonZhu JasonZhu

2014/1/17

#
You can test I out with this link if you don't understand what I'm explaining: http://www.greenfoot.org/scenarios/10675
Dragonblazze Dragonblazze

2014/1/17

#
how far into the wall? can you pass all the way through it or only to the opposite edge? and what are your walls made of? give us some context here.
JasonZhu JasonZhu

2014/1/17

#
Have you checked out my scenario? I think it'd be the best way to understand my problem.
JasonZhu JasonZhu

2014/1/17

#
Problem still persists.
You need to login to post a reply.