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

2014/4/4

Alternative ways of interacting with objects in the world.

trias95 trias95

2014/4/4

#
Hey there guys, i have a little bit of a graphical problem. im looking for an alternate way to stop my character from walking through objects in the world. ATM im using the code
Actor fountain;
        fountain = getOneObjectAtOffset(0,0, Fountain.class);
        if (fountain != null)
        {
            setRotation(getRotation()+180);
            move(5);
        }
But the movement is erratic and displeasing. Any suggestions are greatly appreciated.
trias95 trias95

2014/4/4

#
Actor fountain;
        fountain = getOneIntersectingObject(Fountain.class);
        if (fountain != null)
        {
            setRotation(getRotation()+180);
            move(5);
        }
Works a little better however when using this method, walking into the fountain backwards actually puts you inside it xD
danpost danpost

2014/4/4

#
Will have to see more of the class to see what variables and fields are available to this snippet and how your methods are factored.
Game/maniac Game/maniac

2014/4/4

#
Hope this helps :)
Actor fountain = getOneIntersectingObject(Fountain.class);
if(fountain!=null) {
    while(getX()+getImage().getWidth()/2 > fountain.getX()-fountain.getImage().getWidth()/2 && getX()+getImage().getWidth()/2 < fountain.getX()) setLocation(getX()-1, getY())
    while(getX()-getImage().getWidth()/2 < fountain.getX()+fountain.getImage().getWidth()/2 && getX()-getImage().getWidth()/2 >= fountain.getX()) setLocation(getX()+1, getY())
    while(getY()+getImage().getHeight()/2 > fountain.getY()-fountain.getImage().getHeight()/2 && getY()+getImage().getHeight()/2 < fountain.getY()) setLocation(getX(), getY()-1)
    while(getY()-getImage().getHeight()/2 < fountain.getY()+fountain.getImage().getHeight()/2 && getY()-getImage().getHeight()/2 >= fountain.getY()) setLocation(getX(), getY()+1)
}
You need to login to post a reply.