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

2021/6/7

Not achieving collision

Tot Tot

2021/6/7

#
Hey. I'm having an issue with my code. The idea of the code is to move the player 40 pixels in any of the arrow key directions, but not diagonally. There are a couple caveats, though. The first is that Players may not move if there is a wall in the place they are trying to move to. Secondly, if there is a box in front of them when they move, players should be able to push the box, but not if there is a wall on the other side of the box blocking the way. The code to push the boxes works, but currently my players can travel through walls. Boxes will also go though walls if pushed by a player. Here's the code. It runs in the act method of the Player.
   public void act() 
    {
        move();
        // action code here.
    }  

    public void move()
    {
        if(Greenfoot.isKeyDown("Right"))
        {
            boolean wall;
            Actor box = getOneObjectAtOffset​(0+40, 0, Box.class);
            if (box != null) {
                wall = haveWall(getX()+80,getY());
            }else {
                wall = haveWall(getX()+40,getY());
            }
            if (wall == false) {
                setLocation(getX()+40,getY());
                if(box != null) {
                    getWorld().removeObject(box);
                    getWorld().addObject(new Box(), getX()+40, getY());
                }
            }
            Greenfoot.delay(20);
        }

        else if(Greenfoot.isKeyDown("Left"))
        {
            boolean wall;
            Actor box = getOneObjectAtOffset​(0-40, 0, Box.class);
            if (box != null) {
                wall = haveWall(getX()-80,getY());
            }else {
                wall = haveWall(getX()-40,getY());
            }
            if (wall == false) {
                setLocation(getX()-40,getY());
                if(box != null) {
                    getWorld().removeObject(box);
                    getWorld().addObject(new Box(), getX()-40, getY());
                }
            }
            Greenfoot.delay(20);
        }

        else if(Greenfoot.isKeyDown("Up"))
        {
            boolean wall;

            Actor box = getOneObjectAtOffset​(0, 0+40, Box.class);

            if (box != null) {
                wall = haveWall(getX(),getY()-80);
            }else {
                wall = haveWall(getX(),getY()-40);
            }

            if (wall == false) {
                setLocation(getX(),getY()-40);
                if(box != null) {
                    getWorld().removeObject(box);
                    getWorld().addObject(new Box(), getX(), getY()-40);
                }
            }
            Greenfoot.delay(20);
        }

        else if(Greenfoot.isKeyDown("Down"))
        {
            boolean wall;
            Actor box = getOneObjectAtOffset​(0, 0+40, Box.class);
            if (box != null) {
                wall = haveWall(getX(),getY()+80);
            }else {
                wall = haveWall(getX(),getY()+40);
            }
            if (wall == false) {
                setLocation(getX(),getY()+40);
                if(box != null) {
                    getWorld().removeObject(box);
                    getWorld().addObject(new Box(), getX(), getY()+40);
                }
            }
            Greenfoot.delay(20);
        }
    }

    public boolean haveWall(int x, int y)
    {
        Actor walls = getOneObjectAtOffset​(x, y, Wall.class);

        if(walls != null) {
            return true;
        }else {
            return false;
        }
    }
danpost danpost

2021/6/7

#
The movement of Player and Box would be quite similar to Bug and Mushroom in my Sokoban Movement Demo scenario.. Maybe you can get some pointers from it.
You need to login to post a reply.