Im trying to make a basic space invaders game but the bullet from the spaceship keeps getting an error when it hits the alien. It doesn't show me the error and the world just pauses.
Here is the spaceship class
This is the bullet class
and here is the world code
Thanks
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Spaceship here. * * @author (your name) * @version (a version number or a date) */ public class Spaceship extends Actor { int shootTimer = 0; public void act() { Move(); Shoot(); hit(); shootTimer++; } public Spaceship() { GreenfootImage image = getImage(); image.scale(75, 75); setImage(image); } public void Move() { int x = getX(); int y = getY(); if(Greenfoot.isKeyDown("left")){ setLocation(x-4, y); }else if(Greenfoot.isKeyDown("right")){ setLocation(x+4, y); } } public void Shoot() { if (Greenfoot.isKeyDown("up") & shootTimer > 30) { Bullet bullet = new Bullet(); getWorld().addObject(bullet, getX(), getY()); bullet.getImage().scale(15, 15); bullet.setRotation(getRotation()-90); bullet.move(45); shootTimer = 0; } } public void hit() { Actor Alienbullet; Alienbullet = getOneObjectAtOffset(0, 0, Alienbullet.class); if(Alienbullet !=null) { World world; world = getWorld(); getWorld().removeObject(Alienbullet); ((SpaceWorld)world).HealthBar.add(-10); } } }
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 Actor { Alien1 Alien1; public void act() { move (8); eat1(); if(isAtEdge()) { getWorld().removeObject(this); } } public Bullet() { GreenfootImage image = getImage(); image.scale(15, 15); setImage(image); } public void eat1() { Actor Alien1 = getOneObjectAtOffset(0, 0,Alien1.class); if(Alien1 != null) { World world = getWorld(); world.removeObject(Alien1); world.removeObject(this); ((SpaceWorld)world).counter.add(10); } } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class SpaceWorld extends greenfoot.World { Counter counter; HealthBar HealthBar; Alienbullet AlienBullet; /** * Constructor for objects of class MyWorld. * */ public SpaceWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(800, 900, 1); prepare(); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { Alien1 alien1 = new Alien1(); addObject(alien1,400,245); alien1.setLocation(401,235); removeObject(alien1); Spaceship spaceship = new Spaceship(); addObject(spaceship,372,734); spaceship.setLocation(405,807); spaceship.setLocation(398,804); spaceship.setLocation(402,820); spaceship.setLocation(398,828); counter = new Counter(); addObject(counter,720,843); HealthBar = new HealthBar(); addObject(HealthBar,75,860); Alien1 alien12 = new Alien1(); addObject(alien12,357,178); } }