This site requires JavaScript, please enable it in your browser!
Greenfoot back
Dipne0p
Dipne0p wrote ...

2017/11/16

Make actor disappear

Dipne0p Dipne0p

2017/11/16

#
My actor gets removed from the world after its healthbar is up but it says "Actor not in world" as soon as the healthbar is up. The healthbar itself is working, it's just that the game stops after the actor gets removed from the world. Bullets:
boolean touchingEnemy = false;
public void act()
{
    move(10);
    checkEnemy();
}
public void checkEnemy()
{ 
Actor enemy = getOneIntersectingObject(Enemy.class); 
        if(enemy != null)
        { 
            World myWorld = getWorld();
            MyWorld myworld = (MyWorld)myWorld;
            HealthBar healthbar = myworld.getHealthBar();
            if(touchingEnemy == false)
{
                healthbar.loseHealth();
                touchingEnemy = true;
                if(healthbar.health <= 0)
{
                    myWorld.removeObject(enemy);
                }
            }
        }
        else
{
            touchingEnemy = false;
        }
}
How can I fix it?
danpost danpost

2017/11/16

#
Are you wanting to remove the enemy when the health of the actor is used up -- or do you want to remove the actor?
Dipne0p Dipne0p

2017/11/17

#
danpost wrote...
Are you wanting to remove the enemy when the health of the actor is used up -- or do you want to remove the actor?
When I said "actor" I was talking about the enemy, so I want to remove the enemy when its health is used up.
danpost danpost

2017/11/17

#
It seems odd that the health bar being decreased and checked for a zero value (lines 17 and 19) would be used for a touching enemy. Does the 'getHealthBar' method in your MyWorld class return the health bar of the intersecting enemy?
Dipne0p Dipne0p

2017/11/17

#
danpost wrote...
It seems odd that the health bar being decreased and checked for a zero value (lines 17 and 19) would be used for a touching enemy.
Why? Where else would I put it? The bullets touch the enemy and decrease its health and if the enemy's health is used up it should get removed.
danpost wrote...
Does the 'getHealthBar' method in your MyWorld class return the health bar of the intersecting enemy?
Yes
public Healthbar getHealthBar()
    {
        return healthbar; 
    }
danpost danpost

2017/11/17

#
I think you may be assuming more than you should or have a complete misunderstanding about something. That is to say, I do not think your health bar is actually set to any Enemy object (although you may think so). Please show the codes to your class that extends World.
Dipne0p Dipne0p

2017/11/17

#
import greenfoot.*;
public class MyWorld extends World
{
Healthbar healthbar = new Healthbar();
public MyWorld()
{
super(600, 400, 1);
addObject(new Enemy(), 250, 100);
addObject(new Shooter(), 150, 100);
}
public Healthbar getHealthBar()
{
return healthbar;
}
}
danpost danpost

2017/11/17

#
Okay -- so I guess I was mistaken since what you show is all you have in the MyWorld class. well, since you are removing the Enemy and I do not see where you might get that error in the Shooter class, it seems it can only be in the Healthbar class.
Dipne0p Dipne0p

2017/11/17

#
danpost wrote...
I do not see where you might get that error in the Shooter class
I get the error in the Bullet class, the Shooter shoots the bullets. More specifically this line is marked yellow:
Actor enemy = getOneIntersectingObject(Enemy.class);
and then it says: "Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed."
danpost wrote...
it seems it can only be in the Healthbar class.
import greenfoot.*;
public class Healthbar extends Actor
{
    int health = 10;
    int healthWidth = 80;
    int healthHeight = 15;
    int pixelsPerHealthPoint = (int)healthWidth/health;
    public Healthbar()
    {
         update();
    }
    public void update() 
    {
         GreenfootImage myImage = new GreenfootImage(healthWidth + 2, healthHeight + 2);
         myImage.setColor(Color.WHITE);
         myImage.drawRect(0, 0, healthWidth + 1, healthHeight + 1);
         myImage.setColor(Color.GREEN);a
         myImage.fillRect(1, 1, health * pixelsPerHealthPoint, healthHeight);
         setImage(myImage);
    }
    public void loseHealth()
    {
        health--;
        update();
    }
}
danpost danpost

2017/11/17

#
Dipne0p wrote...
I get the error in the Bullet class, the Shooter shoots the bullets. More specifically this line is marked yellow:
Actor enemy = getOneIntersectingObject(Enemy.class);
and then it says: "Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed."
Change line 7 in the Bullet class code above to this:
private void checkEnemy()
Do you still get the same error?
Dipne0p Dipne0p

2017/11/17

#
danpost wrote...
Do you still get the same error?
yep
danpost danpost

2017/11/17

#
Dipne0p wrote...
danpost wrote...
Do you still get the same error?
yep
Since you are getting a run-time error, that means your project compiles. You can therefore upload it onto greenfoot and include the source code so that it can be reviewed and tested. (or you could first show the Shooter class code here to review)
You need to login to post a reply.