Hi,
currently I am working on a school project (grade 12) in which we are supposed to create a small game. My idea was something like the old DOS-Game "Sopwith" which some of you may know; you are flying a plane and have to shoot / bomb some ground targets.
Until now, i managed to get a flying plane which shoots some bullets, crashes and explodes when you touch the world's borders, the ground or enemy buildings. Also, there is a complete world with hills, factorys, moving tanks and some other stuff. So far so good.
When a bullet hits an enemy, it gets removed from the world. But as one bullet normally does not destroy a whole tank, i added a private int which should count the bullets which already hit it.
Problems are: 1st the variable does not count down, ergo the enemy can not be destroyed. 2nd if I write "getWorld().removeObject(this);" to remove the bullet from the world after hitting a target the game stops, so i have to respawn it in a place where it cant do any damage (just an anoying problem)...
Full bullet.class code:
If you want to see the whole game just ask and I'll upload it.
Greetings from Berlin :)
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class bullet here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class bullet extends plane
{
private int speed = -4;
private int factory_life = 20;
private int tank_life = 10;
private int flak_life = 5;
private int quarter_life = 20;
/**
* Act - do whatever the bullet wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
/**
* spawne die Kugel beim Flugzeug und beschleunige sie in die aktuelle Richtung
*/
Actor plane = getOneIntersectingObject(plane.class);
if(plane != null) {
setLocation(getX(), getY());
setRotation(rotation);
move(-4);
}
else
{
move(-4);
destroyEnemies();
if(atWorldEdge())
{
setImage("blank.png");
setLocation(100, 600);
move(0);
turn(180);
}
if (atGround())
{
setImage("blank.png");
setLocation(100, 600);
move(0);
turn(180);
}
}
}
/**
* Wenn Gegner getroffen, Leben abziehen. Objekt verschwinden lassen, falls Leben = 0.
*/
public void destroyEnemies()
{
Actor enemy = getOneIntersectingObject(factory.class);
if(enemy != null)
{
factory_life = factory_life - 1;
if (factory_life==0)
{
getWorld().removeObject(enemy);
}
setImage("blank.png");
setLocation(100, 600);
move(0);
turn(180);
}
Actor enemy2 = getOneIntersectingObject(tank.class);
if(enemy2 != null)
{
tank_life = tank_life - 1;
if (tank_life==0)
{
getWorld().removeObject(enemy2);
}
setImage("blank.png");
setLocation(100, 600);
move(0);
turn(180);
}
Actor enemy3 = getOneIntersectingObject(flak.class);
if(enemy3 != null)
{
flak_life = flak_life - 1;
if (flak_life==0)
{
getWorld().removeObject(enemy3);
}
setImage("blank.png");
setLocation(100, 600);
move(0);
turn(180);
}
Actor enemy4 = getOneIntersectingObject(quarter.class);
if(enemy4 != null)
{
quarter_life = quarter_life - 1;
if (quarter_life==0)
{
getWorld().removeObject(enemy4);
}
setImage("blank.png");
setLocation(100, 600);
move(0);
turn(180);
}
}
}
If you want to see the whole game just ask and I'll upload it.
Greetings from Berlin :)

