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

2018/5/9

Help with error/code

hosfeldli hosfeldli

2018/5/9

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Terrorist here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy extends Actor
{
    private int i;

    public int time;
     
    MyWorld world =(MyWorld)getWorld();
    //HealthBar health = world.getHealthBar();
    /**
     * Act - do whatever the Terrorist wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Enemy()
    {
    }
    public void act() 
    {
      Moving();  
      Damage();  
      time--;
      if(time <= 0)
      {
        setImage("Terrorist.png");
        }

    }  
    
    
    
    
    
    public void Moving()
    {
        
        if(getY() == 400) 
        {
            while (i < 30)
            {
                setLocation(getX()-1, getY());
                i++;
            }
        }
    
    }
    
    public void Damage()
    {
         int number = getWorld().numberOfObjects();   
         MyWorld world =(MyWorld)getWorld();
         HealthBar1 healthbar = world.getHealthBar1();
         HealthBar2 healthbar2 = world.getHealthBar2();
         HealthBar3 healthbar3 = world.getHealthBar3();
        if(Greenfoot.getRandomNumber(20000) < number)
        {
            getWorld().addObject(new Damage(), 500, 250); 
            world.addLife(-1);
            setImage("ShootingT.png");
            time = 50;
            Greenfoot.playSound("MP5_SMG-GunGuru-703432894.wav");
        } 
        if(number >= 1)
        {
            if(healthbar.Health <= 0)
                if(getX() < 400 && getX() > 200)
                    world.removeObject(this);
        }
        if(number >= 1)
        {
            if(healthbar2.Health <= 0)
            {
                if(getX() < 500 && getX() > 400)
                    world.removeObject(this);
            } 
        }
        if(number >= 1)
        {
            if(healthbar3.Health <= 0)
            {
                if(getX() < 700 && getX() > 500)
                    world.removeObject(this);
                }
        }
    }
}
It says that the object isn't in world and I ust want to know a way to fix it. If I should just upload it tell me.
hosfeldli hosfeldli

2018/5/9

#
Greenfoot Scenario Here is the scenario
Yehuda Yehuda

2018/5/9

#
You didn't say where the error is (You should post the Output Window contents). Line 15 will return null. Lines 45 - 49 are equivalent to:
setLocation(getX() - 30, getY());
Lines 74 - 76 and 82 - 84 are unnecessary.
hosfeldli hosfeldli

2018/5/9

#
That's not the problem, the problem is in 69 - end
Yehuda Yehuda

2018/5/9

#
hosfeldli wrote...
That's not the problem, the problem is in 69 - end
Yehuda wrote...
You didn't say where the error is (You should post the Output Window contents).
I don't see any problem with the if-statement on line 69, nor do I see a problem with the end of line 69.
hosfeldli hosfeldli

2018/5/9

#
Just run the program
Yehuda Yehuda

2018/5/9

#
That doesn't help me help you since I didn't see anything wrong (besides for the impossibility of winning). It was just a little hard to figure out what to do and how to do it.
hosfeldli hosfeldli

2018/5/9

#
Huh, there isn't a "actor not in world error" for the last if statements(past 69)
Yehuda Yehuda

2018/5/9

#
I ran it on the site, and anyway if-statements can run even when the actor is not in a World.
danpost danpost

2018/5/10

#
The problem is you have 3 distinct reasons for removing the actor from the world, but you cannot check subsequent conditions if a previous condition was true and the actor was removed -- mainly using getX. You can concatenate the possible conditions for removing the actor so that no other code needs to execute after the removal. Replace lines 69 through 90 with this:
if (number >= 1)
{
    if (getX() < 400 && getX() > 200 && healthbar.Health <= 0
        || getX() < 500 && getX() > 400 && healthbar2.Health <= 0
        || getX() < 700 && getX() > 500 && healthbar3.Health <= 0)
            world.removeObject(this);
}
You need to login to post a reply.