Hey all, this should be my last question for the time being, but it's an important one. In the game I am creating, I have a main character, Kara, that shoots out these projectiles that hit enemies. Normally, the enemies would simply get removed along with the projectile which is not an issue, that was my first implementation in fact, but since then I have added a health UI system so the enemies are not complete pushovers and now take two hits to kill. After a lot of debugging, I've discovered that it really depends where I detect collision what the program produces (at least with the way I'm implementing things) where if I add code into the projectile, the projectile is removed but the enemy is fine and if I add in code to the enemy, it gets killed very quickly because the projectile is not removed and the enemy takes two hits in two consecutive frames. I guess one way to fix this is invincibility frames, which I know how to do, but that would be kind of ugly and would much prefer both the enemy take a little damage and the projectile be removed. PS- I already tried implementing both at once and what happens there is the projectile scenario overrides the enemy scenario almost as if I never implemented it in the enemy at all.
Enemy code:
Projectile code:
import greenfoot.*;
public class Enemy extends Actor
{
public int speed = 1;
//credit to danpost for creating some code for floating health bars
final int MIN_HEALTH = 0;
final int MAX_HEALTH = 20;
HealthDisplay healthDisplay;
int health = 15;
protected void addedToWorld(World world){
if (healthDisplay == null){
healthDisplay = new HealthDisplay();
}
//adds in a health display to show enemy health
world.addObject(healthDisplay, 0, 0);
healthDisplay.act();
healthDisplay.adjustHealth(0);
}
public void act()
{
moveAndTurn();
touch();
if (health == 0){
getWorld().removeObject(this);
}
}
public void moveAndTurn(){
Actor k = null;
//gets an actor, kara, and figures out where she is within a certain range to follow
if (getObjectsInRange(400, Kara.class).size() > 0)
{
k = getObjectsInRange(400, Kara.class).get(0);
}
if (k != null)
{
int karaX = k.getX();
int karaY = k.getY();
turnTowards​(karaX, karaY);
move(speed);
}
}
public void touch(){
Actor projectile;
projectile = getOneObjectAtOffset(0,0,Projectile.class);
//detects collision with the rocks and pushes the enemy backwards so it does not phase through
if (isTouching(Rock.class)){
this.move(-speed);
}
}
public class HealthDisplay extends Actor{
public void act(){
//handles placing the health display
if (Enemy.this.getWorld() == null){
getWorld().removeObject(this);
}
else{
setLocation(Enemy.this.getX(), Enemy.this.getY()-50);
}
}
//handles actually changing the display to represent the proper health
public void adjustHealth(int amt){
health += amt;
if (health > MAX_HEALTH){
health = MAX_HEALTH;
}
if (health<MIN_HEALTH){
health = MIN_HEALTH;
}
GreenfootImage img = new GreenfootImage(""+health, 24, Color.BLACK, Color.WHITE);
img.drawRect(0,0,21,24);
setImage(img);
}
}
}import greenfoot.*;
public class Projectile extends Actor
{
public void act()
{
move(8);
touch();
}
public void touch(){
World world;
world = getWorld();
Actor e = null;
if (isTouching(Enemy.class))
{
e = getOneObjectAtOffset(0,0,Enemy.class);
}
if (e != null)
{
//this is code that removes both as of now, but I'm hoping that I can simply send a signal to the enemy class to remove some health then
world.removeObject(e);
world.removeObject(this);
}
}
}
