The ship is unable to use the laser as the user moves the ship. I was wondering how I can fix the issue.
Laser Code:
Ship code:
Thank you!
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Laser extends Actor
{
public int alien_counter = 4;
private ScoreBoard score;
public Laser(ScoreBoard score)
{
this.score = score;
}
public void act()
{
move(5);
Defeat();
RemovingLaser();
}
public void Defeat() {
// Removes alien if laser is touching it
if (isTouching(Alien.class)) {
removeTouching(Alien.class);
alien_counter = alien_counter - 1;
score.addStar();
}
}
public void RemovingLaser() {
if (isAtEdge() || isTouching(FinalBoss.class)) {
getWorld().removeObject(this);
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Ship extends Actor
{
// True or false statement for touching other actors
boolean touchingAlien = false;
boolean touchingFinalBoss = false;
boolean touchingBossLaser = false;
private ScoreBoard score;
public Ship(ScoreBoard score)
{
this.score = score;
}
public void act()
{
movement();
Boundry();
hitAlien();
hitFinalBoss();
}
public void movement() {
// User input for navigating ship
if (Greenfoot.isKeyDown("up")) {
setLocation(getX(), getY() - 5);
}
if (Greenfoot.isKeyDown("down")) {
setLocation(getX(), getY() + 5);
}
if (Greenfoot.isKeyDown("right")) {
setLocation(getX() + 5, getY());
}
if (Greenfoot.isKeyDown("left")) {
setLocation(getX() - 5, getY());
}
// User input for shooting lasers (works when key is lifted)
if ("space".equals(Greenfoot.getKey())) {
shoot();
}
}
public void shoot() {
getWorld().addObject(new Laser(score), getX(), getY());
}
public void hitAlien() {
// Define the actor as the ship touching an alien
Actor alien = getOneIntersectingObject(Alien.class);
if (alien != null) {
World myWorld = getWorld();
MyWorld myworld = (MyWorld)myWorld;
HealthBar healthbar = myworld.getHealthBar();
if (touchingAlien == false) {
healthbar.loseHealth();
touchingAlien = true;
if (healthbar.health <= 0) {
// Game over since health has hit 0
myWorld.removeObject(this);
Greenfoot.setWorld(new GameOver());
}
}
else {
// Continue game
touchingAlien = false;
}
}
}
public void hitFinalBoss() {
if (getWorld() != null) {
Actor finalboss = getOneIntersectingObject(FinalBoss.class);
Actor bosslaser = getOneIntersectingObject(BossLaser.class);
if (finalboss != null || bosslaser != null) {
World finalWorld = getWorld();
FinalWorld finalworld = (FinalWorld)finalWorld;
HealthBar healthbar = finalworld.getHealthBar();
if (touchingFinalBoss == false || touchingBossLaser == false) {
healthbar.loseHealth();
touchingFinalBoss = true;
touchingBossLaser = true;
if (healthbar.health <= 0) {
// Game over since health has hit 0
finalWorld.removeObject(this);
Greenfoot.setWorld(new GameOver());
}
}
else {
// Continue game
touchingFinalBoss = false;
touchingBossLaser = false;
}
}
}
}
public void Boundry() {
if (getX() > 980) {
setLocation(980, getY());
}
if (getX() < 0) {
setLocation(0, getY());
}
if (getY() > 575) {
setLocation(getX(), 575);
}
if (getY() < 0) {
setLocation(getX(), 0);
}
}
}
