Hi everyone,
I'm working on a crab game for a school project. The game consist of crab which walks where you click and can eat worms on the way.
But I can't really find a solution to stop the crab from going any further. To stop at the x and y coordinates of the mouse doesn't work, so I have tried to create an actor at the position where the mouse clicks and stop the crab through isTouching().
But the actor needs to be invisible. So I have tried to turn the transparency of the class (CrabTouching) to 255, but that didn't work, so I tried it through a variable from the CopyOfCrab code, but that didn't work either.
Has anybody a way to turn the actor invisible, or how to stop the crab without another actor?
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import greenfoot.*; /** * Diese Klasse definiert eine Krabbe. Krabben leben am Strand. */ public class CopyOfCrab extends Actor { static int crabX, crabY; static int MouseToCrabX, MouseToCrabY; static double hypothenuse; public void CrabPosition() { crabX = this .getX(); crabY = this .getY(); } public void MouseToCrab() { MouseToCrabX = Math.abs(CrabWorld.mouseX - crabX); MouseToCrabY = Math.abs(CrabWorld.mouseY - crabY); } public void findhypothenuse() { hypothenuse = Math.round(Math.sqrt(Math.pow(MouseToCrabX, 2 ) + Math.pow(MouseToCrabY, 2 ))); } public void act() { turnTowards(CrabWorld.mouseX, CrabWorld.mouseY); if (Greenfoot.mouseClicked( null )){ getWorld().addObject( new CrabTouching(), CrabWorld.mouseX, CrabWorld.mouseY); while (!isTouching(CrabTouching. class )) { move( 5 ); Greenfoot.delay( 1 ); if (isTouching(Worm. class )) { removeTouching(Worm. class ); } } getWorld().removeObjects(getWorld().getObjects(CrabTouching. class )); } CrabPosition(); MouseToCrab(); findhypothenuse(); } public void move( double distance) { double angle = getRotation(); int x = ( int ) Math.round(getX() + Math.cos(angle) * 5 ); int y = ( int ) Math.round(getY() + Math.sin(angle) * 5 ); setLocation(x, y); } } |
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 | import greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage) public class CrabWorld extends World { /** * Erzeugt die Krabbenwelt (den Strand). Unsere Welt hat eine Größe * von 560x560 Zellen, wobei jede Zelle nur ein Pixel groß ist. */ public CrabWorld() { super ( 560 , 560 , 1 ); addObject( new CopyOfCrab(), getWidth()/ 2 , getHeight()/ 2 ); int NumberOfWorms = Greenfoot.getRandomNumber( 11 ); for ( int i = NumberOfWorms; i< 11 ; i++){ addObject( new Worm(), Greenfoot.getRandomNumber( 560 ), Greenfoot.getRandomNumber( 560 )); } } static int mouseX, mouseY; public void act() { if (Greenfoot.mouseMoved( null ) || Greenfoot.mouseDragged( null )) { MouseInfo mouse = Greenfoot.getMouseInfo(); mouseX = mouse.getX(); mouseY = mouse.getY(); } } } |