I would like to make a "guard" that turn around my hero but I'm having some issues.
For now I have this code:
I wanted to make him follow the hero and then add the "turning around" effect but like that I have 2 problems:
- as soon as I hit an edge of the world the distance between guard-hero is altered.
- it doesn't "turn" around my actor properly
How can I fix that or do that?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | public void act() { moveAroundHero(); checkKey(); } public void moveAroundHero() { PlayUniverse playUniverse = (PlayUniverse)getWorld(); Hero player = playUniverse.getplayer(); if (player.getY() < getY() && player.getX() < getX())setLocation(getX()+ 3 , getY()- 3 ); if (player.getY() >= getY() && player.getX() < getX())setLocation(getX()- 3 , getY()- 3 ); if (player.getY() >= getY() && player.getX() >= getX())setLocation(getX()- 3 , getY()+ 3 ); if (player.getY() <= getY() && player.getX() >= getX())setLocation(getX()+ 3 , getY()+ 3 ); } public void checkKey() { if (Greenfoot.isKeyDown( "right" )) { move( 5 ); } if (Greenfoot.isKeyDown( "left" )) { move(- 5 ); } if (Greenfoot.isKeyDown( "up" )) { setLocation(getX(), getY() - 5 ); } if (Greenfoot.isKeyDown( "down" )) { setLocation(getX(), getY() + 5 ); } } |