Hi guys i've been trying to make a game and I am quite new to java. I have the code here and it works but the problem is when I click to shoot the bullet it shoots and will destroy the other object, but if it goes out of the edge I get an error. Can anyone help me? Here's the code for the bullet class:
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 { public void act() { move(7); disappear(); collisionDetection(); //colision detection and point awarding //create bullet object } public void collisionDetection() { Actor Key = getOneObjectAtOffset(0, 0, Key.class); if (Key != null) { hitObject(); getWorld().removeObject(Key); getWorld().removeObject(this); } } //make bullet disappear public void disappear() { if( getX() <= 4 || getX() >= getWorld().getWidth()-2) { getWorld().removeObject(this); return; } if( getY() <= 4 || getY() >= getWorld().getHeight()-2) { getWorld().removeObject(this); return; } } public void hitObject() { Piano pianoWorld = (Piano) getWorld(); Counter counter = pianoWorld.getCounter(); counter.scoreCount(5); } }