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?
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);
}
}
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();
}
}
}

