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

2012/10/31

Character moving through objects

MickMulder MickMulder

2012/10/31

#
I just started programming 8 weeks ago, and started making a platformer. Everythings working fine except, i can walk through cliffs. Can anybody help to make a code so that i can't walk through cliffs anymore. Make the wall an impassable object.
Gevater_Tod4711 Gevater_Tod4711

2012/10/31

#
you just have to check if there is an object at the position you want to move to. instead of:
setLocation(x, y);
do this:
if (getWorld().getObjectsAt(x, y, Object.class).isEmpty()) { //instead of object you must add the classname of the object your searching for;
    setLocation(x, y);
}
MickMulder MickMulder

2012/11/1

#
i made this code, but i'm probably doing something wrong:
    public void solidObject()
    {
        int x = getX();
        int y = getY();
        if (getWorld().getObjectsAt(x, y, Cliff.class).isEmpty()) 
        {  
            setLocation(x, y);  
        }  
    }
whats wrong with it?
Gevater_Tod4711 Gevater_Tod4711

2012/11/1

#
You are checking if there is no Cliff object at your position. You have to check at the position you want to move to.
    public void solidObject()  
    {  
        int x = getX();  
        int y = getY();  
        x += moveX;//moveX is the change in x directon you want your character to move;
        y += moveY;//moveY is the same in y direction;
        if (getWorld().getObjectsAt(x, y, Cliff.class).isEmpty())   
        {    
            setLocation(x, y);    
        }    
    }  
You need to login to post a reply.