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

2016/4/18

Removing Object - Nothing happens?

1
2
idk1234 idk1234

2016/4/18

#
Hi, I really new to Greenfoot and I need some help. What I am trying to do is with a certain chance someone is "infected" (the object is removed). I have used the following code but nothing seems to happen?
    /**
     * Infect a Survivor with Virus
     */
    public void infect()
    {        
        if(Greenfoot.getRandomNumber(100)<50)
        {
            removeObject(SurvivorWASD);       
        }
        if(Greenfoot.getRandomNumber(100) >50)
        {
            removeObject(SurvivorWASD);   

        }        
    }
It's probably some stupid mistake on my part but I just can't get my head around it :/ Thanks in advance!
idk1234 idk1234

2016/4/18

#
Sorry, I copied the code when I was trying stuff out. Here is the code as it is now:
    /**
     * Infect a Survivor with Virus
     */
    public void infect()
    {        
        if(Greenfoot.getRandomNumber(100) <50)
        {
            removeObject(SurvivorWASD);       
        }
        if(Greenfoot.getRandomNumber(100) >50)
        {
            removeObject(SurvivorARROW);   

        }        
    }
danpost danpost

2016/4/18

#
If 'SurvivorWASD' and 'SurvivorARROW' are class names, of course it will not work. The 'removeObject' method requires an actor object for its parameter, not a class name. I presume that the code given is in the Virus class and the Virus actor is currently touching one of the objects of those classes. Will need to see the code detecting collision to assist here.
idk1234 idk1234

2016/4/18

#
Well the actual virus code is working. This code is from the world. What I am trying to do is at the start of the round is, a random person is selected to become the virus, to get the game going but as you can tell, I'm not sure how to remove the survivor so I can replace it with a Virus.
danpost danpost

2016/4/18

#
idk1234 wrote...
Well the actual virus code is working. This code is from the world. What I am trying to do is at the start of the round is, a random person is selected to become the virus, to get the game going but as you can tell, I'm not sure how to remove the survivor so I can replace it with a Virus.
Oh -- so what you need is a way to select a random person (from what I will call the 'Person' class) in the world.
// first -- get number of Person objects in the world
int persons = getObjects(Person.class).size();
// execute operation only if there are persons in the world
if (persons > 0)
{
    // choose random person (get index)
    int index = Greenfoot.getRandomNumber(persons);
    // get person at index
    Person person = (Person)getObjects(Person.class).get(index);
    // now you have a random person to deal with
idk1234 idk1234

2016/4/18

#
Thanks for the help! I'm still not sure how I would remove them though, any ideas?
danpost danpost

2016/4/18

#
idk1234 wrote...
Thanks for the help! I'm still not sure how I would remove them though, any ideas?
Do you mean this?
removeObject(person);
But, you may want to put the virus in the world at the location of the person before removing the person (as there will be no location for the person once removed).
idk1234 idk1234

2016/4/18

#
Yeah I've been trying as you said: (removeObject) and absolutely nothing happens :(
danpost danpost

2016/4/18

#
You need to show the code you are now using (not just one line -- but, at minimum, the method it is in. Also, make sure the 'infect' method is being called from somewhere (like the constructor of the class or a 'started' method.
idk1234 idk1234

2016/4/19

#
This is the world code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TwoPlayer extends World
{
    private SurvivorWASD SurvivorWASD;
    private SurvivorARROW SurvivorARROW;
    private TwoPlayer TwoPlayer;
    private VirusWASD VirusWASD;
    private TwoPlayer_Button TwoPlayer_Button;
    private Animal Animal;
    public Animal getAnimal()
    {
        return Animal;
    }
    public SurvivorWASD getSurvivorWASD()
    { 
        return SurvivorWASD;
    }
    public SurvivorARROW getSurvivorARROW()
    {
        return SurvivorARROW;
    }
    public TwoPlayer getTwoPlayer()
    { 
        return TwoPlayer;
    }
    public VirusWASD getVirusWASD()
    { 
        return VirusWASD;
    }
    public TwoPlayer_Button getTwoPlayer_Button()
    {
        return TwoPlayer_Button;
    }

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public TwoPlayer()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1366, 768, 1); 
        {
            if (Greenfoot.mouseClicked(TwoPlayer_Button))
            {
                prepare();
                infect();

            }
        } 
    }
    
    /**
     * Infect a Survivor with Virus
     */
    public void infect()
    {        
        if(Greenfoot.getRandomNumber(100) <50)
        {
            removeObject(SurvivorWASD);       
        }
        if(Greenfoot.getRandomNumber(100) >50)
        {
            removeObject(SurvivorARROW);

        }        
    }

    /**
     * Create a new Virus(WASD) in the same location as the Survivor was.
     */
    private void createNewVirusWASD()
    {
        VirusWASD newVirusWASD;
        newVirusWASD = new VirusWASD();

        World world;
        world = getTwoPlayer();

        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();

        int x = getWidth();
        int y = getHeight();

        world.addObject(newVirusWASD, getWidth(), getHeight());
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        SurvivorWASD survivorWASD = new SurvivorWASD();
        addObject(survivorWASD,393,372);
        SurvivorARROW survivorARROW = new SurvivorARROW();
        addObject(survivorARROW,574,536);
    }

}
danpost danpost

2016/4/19

#
When your world is created, before the scenario is started, it is given its fields with their initial values (in your case, the initial value of all fields starts as 'null') and the constructor of the class is executed ( 'public 'Two_Player()' -- lines 46 through 58). Line 49 allows the World class to do what it needs to do to initialize your world. At this point, there are no objects in your world, yet your next executable line (line 51) looks for a mouse click on a button object. There are two issues with this. First, there is yet to be a button created. In fact, nowhere in your code do I see a TwoPlayer_Button object being created. The other is that the scenario is not running yet and cannot detect any mouse actions. Mouse action can only be detected while the scenario is running. So, any mouse action code needs to be placed within the act method or a method it calls or is linked to. There seems be quite a bit amiss with the coding you gave. Not a single one of your fields are being assigned any values because you are declaring local variables throughout the code using the same name as the fields. You need to remove the first word on each of the following lines: 81, 102, 104 I do not see where any Animal object is created and there is an issue with the 'TwoPlayer' field in that the only reference to the field is where you assign a World object to 'world' (lines 84 and 85) by accessing the value of the field (which is 'null' as nothing has been assigned to the field, as yet). Back to the button -- it should be created in the constructor, as well as being assigned to the field there, and the act method should check for clicks on it. Make sure you remove the button once clicked to prevent the action from happening again.
idk1234 idk1234

2016/4/19

#
Ok so the button is in a different world that I'm using for a main menu, it is created there. For lines 102 and 104 - those are in the original prepare method that gets created when you place items and save the world. The things you listed are not an issue, they work. The main issue is I cannot remove objects - it simply doesn't work.
danpost danpost

2016/4/19

#
Oh, okay -- so this is a secondary world that another world creates -- and that other world also creates and adds the button into this secondary world. But, how can these fields possibly get assigned any values. They are private and there are no 'public set<FieldName>(<value>)' type methods to assign them values from another class. The code the prepare method creates does not account for assigning values to your fields. You must remove the 'type declaration' on lines 102 and 104 so that the variable will refer to your field instead of a new declared variable local to the method. The lines 51 through 57 in the constructor is certainly an issue. Those lines need to be removed from the constructor and placed inside a 'public void act()' method.
idk1234 idk1234

2016/4/19

#
Omg thank you so much!! For the most part it seems to be working now. But theres a small problem: sometimes it removes both survivors and sometimes it keeps both survivors. Any ideas? Do I have to separate the infect method into 2? Sorry mate I'm really new to this
danpost danpost

2016/4/19

#
idk1234 wrote...
Omg thank you so much!! For the most part it seems to be working now. But theres a small problem: sometimes it removes both survivors and sometimes it keeps both survivors. Any ideas? Do I have to separate the infect method into 2? Sorry mate I'm really new to this
Change line 69 to this:
else
As you had it, you were generating two random numbers and having a possibility of one, or the other, or both, removed. The random numbers were each individually deciding upon the removal of one of the two objects.
There are more replies on the next page.
1
2