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:
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
How can I get the my actor to detect the barriers?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | { // 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); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | { // 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 ); } } |