I'm using greenfoot for a schoolproject. That's the reason why i'm asking this much questions :). My sonic can shoot lazers. First when my sonic did shoot lazers the lazers would get stuck at the right edge of the world. But if they hit my enemy1 (in the game called 'Vijand1') they would disappear. Now i found a way to remove the lazers that got stuck on the right side of the map. But when a lazer hits my enemy1 (Vijand1) the game stops. How do I fix this?This is my sonic code ^^^
public boolean atWorldEdge() {
if (getX() > getWorld().getWidth() - getImage().getWidth() || getY() > getWorld().getHeight() - getImage().getHeight())
{
return true;
}
else
{
return false;
}
}
/**
* Set the objectreference to the gardener
* @param g The objectreference to the gardener
*/
public void setsonic (dier s) {
sonic = s;
}
/**
* Move forward, not to slowly
*/
public void act() {
move(6);
if (isTouching (Vijand1.class)) {
Vijanden Vijand1 = (Vijand1)getOneIntersectingObject (Vijand1.class);
World world = getWorld();
world.removeObject (this);
}
{
if (this.atWorldEdge()==true)
{
World world;
world = getWorld();
world.removeObject(this);
return;
}
}
}
This is my lazer code^^
public void act()
{
move(3);
randomTurn();
turnAtEdge();
}
/**
* With a 10% probability, turn a bit right or left.
*/
public void randomTurn()
{
if ( Greenfoot.getRandomNumber(100) < 10 )
{
turn( Greenfoot.getRandomNumber(40)-20 );
}
}
/**
* If we reach the edge of the world, turn a little bit.
*/
public void turnAtEdge()
{
if (atWorldEdge())
{
turn(7);
}
}
}
and this is my Enemy1 code
public void act()
{
if (Greenfoot.isKeyDown("left")) {
setRotation(180);
move(3);
}
if (Greenfoot.isKeyDown("up")) {
setRotation(270);
move(3);
}
if (Greenfoot.isKeyDown("right")) {
setRotation(0);
move(3);
}
if (Greenfoot.isKeyDown("down")) {
setRotation(90);
move(3);
}
tryToEat();
if (Greenfoot.isKeyDown("space")) {
World world = getWorld();
Lazer Lazer = new Lazer();
Lazer.setsonic(this);
world.addObject(Lazer, getX(), getY());
}
}
public void tryToEat()
{
if (canSee(muntje.class) )
{
eat(muntje.class);
muntjeGegeten++;
Greenfoot.playSound ("Ooh he stealing!.mp3");
World1 world = (World1)getWorld(); //casting
world.updateScore();
}
}
public void checkVijand1()
{
if (canSee(Vijand1.class) )
{
Greenfoot.playSound("game-over.wav");
Greenfoot.stop();
}
}
}
