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

2011/12/7

A simple "wall"

1
2
Builderboy2005 Builderboy2005

2011/12/9

#
Good luck!
darkmist255 darkmist255

2011/12/9

#
Great! So far I have the method in the class GroundMover that prevents any extending class from passing through and class extending nonPassable :D! If you're curious:
public class GroundMover extends Actor
{
    public void detectCollision(int moveSpeed, int xMomentum, int yMomentum)
    {
        if(getOneIntersectingObject(nonPassable.class) != null)
        {
            if(xMomentum > 0) //moving right
            {
                setLocation((getX() - moveSpeed), getY());
            }
            if(xMomentum < 0) //moving left
            {
                setLocation((getX() + moveSpeed), getY());
            }
            if(yMomentum > 0) //moving down
            {
                setLocation(getX(), (getY() - moveSpeed));
            }
            if(yMomentum < 0) //moving up
            {
                setLocation(getX(), (getY() + moveSpeed));
            }
        }
    }
}
chamuzi3 chamuzi3

2015/1/30

#
YOU CAN ALSO USE SOMETHING LIKE THIS FOR COLLISION AND WHEN YOU WHAT TO MOVE THE CHARACTER TO DIFFERNET DIRECTION WITHTOUT PASSING THROUGHT CHARACTERS SUCH AT THE WALL.
    public void act() 
    {
        move(4);
    }   

    public void move(int moveAmt)
    {
        // determine direction by keypress checking
        int dx = 0, dy = 0;
        if (Greenfoot.isKeyDown("d")) 
        {dx += 1;}
        if (Greenfoot.isKeyDown("a")) 
        {dx -= 1;}
        if (Greenfoot.isKeyDown("s"))
        {dy += 1;}
        if (Greenfoot.isKeyDown("w")) 
        {dy -= 1;}
        // check for wall on each step of move in both vertical and horizontal directions
        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);
        }
    }
You need to login to post a reply.
1
2