Hi! I'm having trouble with my coding for a shooter game and I would like to have some advice and help. Currently, I'm having two problems.
First, I can't shoot according to where my player character is facing. It will always shoot downwards.
Second, The checkBoundaries and destroyEnemies methods are always clashing. If I put the checkBoundaries ABOVE destroyEnemies in the act method, it will pop out an error when I destroy an enemy but not when the bullet hits the edge of the board. Vice versa, if I put the checkBoundaries BELOW destroyEnemies in the act method, it will pop out an error when the bullet hits the edge but not when the bullet destroys an enemy.
May I have some help please?
Here is the code for my Player class.
Here is the code for my Bullet class.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Player here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Player extends Actor
{
/**
* Act - do whatever the Player wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private GreenfootImage left = new GreenfootImage("LeftMC.png");
private GreenfootImage right= new GreenfootImage("FrontMC.png");
private GreenfootImage up= new GreenfootImage("BackMC.png");
private GreenfootImage down= new GreenfootImage("FrontMC.png");
public void act()
{
turnImage();
fireBullet();
movePlayer();
playerHit();
}
public void turnImage()
{
if(Greenfoot.isKeyDown("s"))
{
setImage("FrontMC.png");
}
if(Greenfoot.isKeyDown("w"))
{
setImage("BackMC.png");
}
if(Greenfoot.isKeyDown("a"))
{
setImage("LeftMC.png");
}
if(Greenfoot.isKeyDown("d"))
{
setImage("RightMC.png");
}
}
public void movePlayer()
{
if(Greenfoot.isKeyDown("s"))
{
setLocation(getX(), getY() + 3);
}
if(Greenfoot.isKeyDown("w"))
{
setLocation(getX(), getY() - 3);
}
if(Greenfoot.isKeyDown("a"))
{
setLocation(getX() - 3, getY());
}
if(Greenfoot.isKeyDown("d"))
{
setLocation(getX() + 3, getY());
}
}
private boolean spaceDown;
public void fireBullet()
{
Bullet bullet = new Bullet();
if(!spaceDown && Greenfoot.isKeyDown("space")) {
spaceDown = true;
getWorld().addObject(bullet, getX(), getY());
if (getImage().equals("left")) bullet.setRotation(270);
else if (getImage().equals("down")) bullet.setRotation(180);
else if (getImage().equals("up")) bullet.setRotation(0);
else bullet.setRotation(90);
}
if(spaceDown && !Greenfoot.isKeyDown("space")){
spaceDown = false;
}
}
//public void fireBullet()
{
if(!spaceDown && Greenfoot.isKeyDown("space")) {
spaceDown = true;
getWorld().addObject(new Bullet(), getX(), getY());
}
if(spaceDown && !Greenfoot.isKeyDown("space")){
spaceDown = false;
}
}
public void playerHit()
{
Actor ghost = getOneIntersectingObject(ghost.class);
if(ghost != null)
{
World myWorld = getWorld();
myWorld.removeObject(this);
//Game Over Screen
}
}
}
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
{
/**
* Act - do whatever the Bullet wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
int maxDistanceTravelled = 100;
//private int speed = 5;
public void act()
{
//bulletMove();
move(4);
destroyEnemies();
checkBoundaries();
bulletRange();
}
public void bulletMove()
{
if(this.getRotation() == 90)
{
move(4);
}
if(this.getRotation() == 270)
{
move(-4);
}
if(this.getRotation() == 0)
{
setLocation(getX(), getY() - 4);
}
if(this.getRotation() == 90)
{
setLocation(getX(), getY() + 4);
}
}
//we add a method "checkBoundaries()" that destroys bullets that are off screen.
public void checkBoundaries()
{
World myWorld = getWorld();
if(this != null){
if(getX() > myWorld.getWidth() - 1)
myWorld.removeObject(this);
else if(getX() < 1)
myWorld.removeObject(this);
if(getY() > myWorld.getHeight() - 1)
myWorld.removeObject(this);
else if(getY() < 1)
myWorld.removeObject(this);
}
}
//"destroyEnemies()" destroys enemies.
public void destroyEnemies()
{
//"Enemy" can be any class that you want the bullet to destroy.
if(this != null){
Actor enemy = getOneIntersectingObject(ghost.class);
if(enemy != null) {
getWorld().removeObject(enemy);
getWorld().removeObject(this);
}
}
}
public void bulletRange(){
maxDistanceTravelled -= 1;
if(maxDistanceTravelled == 0)
{
getWorld().removeObject(this);
}
}
}
