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

2013/9/21

Help with obstacle collision, please

joeschmoe joeschmoe

2013/9/21

#
I have an actor that I want to be able to detect other actors(walls and such) and not move through them. Right now I have this:
{
        // get user directions
        int dx = 0, dy = 0;
        if (Greenfoot.isKeyDown("up")) dy--;
        if (Greenfoot.isKeyDown("down")) dy++;
        if (Greenfoot.isKeyDown("left")) dx--;
        if (Greenfoot.isKeyDown("right")) dx++;
        // move the actor
        setLocation(getX()+dx, getY()+dy);
        
        //dont go through walls and shit
        if (getOneIntersectingObject(Dabree.class) != null) setLocation(getX()-dx, getY()-dy);
         if (getOneIntersectingObject(barrier.class) != null) setLocation(getX()-dx, getY()-dy);
         if (getOneIntersectingObject(Tele.class) != null) setLocation(getX()-dx, getY()-dy);
    }
And it works well. One problem, I cant turn the actor, only move it's position. In some games I just want to move the actor but h shoots in this scenario so he must turn. So I want to use the turn() method. If I use instead
{
        // move and turn methods
        if(Greenfoot.isKeyDown("up")){
            move(2);
        }
        if(Greenfoot.isKeyDown("left")){
            turn(-4);
        }
        if(Greenfoot.isKeyDown("right")){
            turn(4);
        }
        if(Greenfoot.isKeyDown("down")){
            move(-2);
        }
    }
How can I get the my actor to detect the barriers?
wabuilderman wabuilderman

2013/9/21

#
watch your language... don't cuss
joeschmoe joeschmoe

2013/9/21

#
oops forgot that was in there.. :( Sorry!
danpost danpost

2013/9/21

#
I created an actor that does pretty much what you want. However, the movement is a bit more like what a tank would do. If you are interested, I will post it.
joeschmoe joeschmoe

2013/9/21

#
Thanks for your help danpost. I think I will keep the current movement system. I'm adding some mouse functions so instead of arrow keys to move and space to shoots it's wasd and mouse to aim and shoot. Also with your permission I'd like to use your XActor and XWorld sub class things. They have a bunch of cool methods.
danpost danpost

2013/9/21

#
joeschmoe wrote...
Also with your permission I'd like to use your XActor and XWorld sub class things. They have a bunch of cool methods.
That is what they are there for. Glad you like.
joeschmoe joeschmoe

2013/9/21

#
great, check out the scenario! http://www.greenfoot.org/scenarios/9323
You need to login to post a reply.