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

2016/4/21

Using getX & getY in world?

1
2
3
4
idk1234 idk1234

2016/4/21

#
Hi, the main problem of my other post has been resolved now but is there any way to use getX and getY in the world? I'm trying to use it as I used in in my Virus class: gets the X, Y of the Survivor class before it is removed and is replaced by Virus. It works in the Virus class but now I need the world to randomly select a survivor, get its X. Y and replace with Virus at the start of the game.
    /**
     * Infect a Survivor with Virus
     */
    public void infect()
    {        
        if(Greenfoot.getRandomNumber(100) <50)
        {
            removeObject(SurvivorWASD); 
            createNewVirusWASD();
        }
        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 = getX();
        int y = getY();

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

2016/4/21

#
That cannot be your current code! What happened to the changes that were suggested you make to the 'infect' method in your last post??? -- that of selecting a random Survivor object.
idk1234 idk1234

2016/4/21

#
Oh my gosh. I was using the wrong version of my game! *Facepalm* Sorry for wasting your time lol
idk1234 idk1234

2016/4/21

#
I can't find my new version, just a quick quesiton: for the virus infect code you gave me the following
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
How would I add this code if I had 2 players (survivors) - SurvivorARROW & SurvivorWASD?
danpost danpost

2016/4/21

#
I may have mention this in the previous discussion (idk). You could add a new subclass of Actor to be a superclass of both the SurvivorARROW and SurvivorWASD classes (name it 'Survivor'). Change all occurrences of 'Person' to 'Survivor' in your last post. The change in name is just for making it more descriptive.
idk1234 idk1234

2016/4/21

#
Awesome! It works perfectly!! Now how exactly would I remove the Survivor that is selected? And also can I use getX and getY to place a virus at the spot the survivor was? I really appreciate you helping me :D
danpost danpost

2016/4/21

#
idk1234 wrote...
Awesome! It works perfectly!! Now how exactly would I remove the Survivor that is selected? And also can I use getX and getY to place a virus at the spot the survivor was? I really appreciate you helping me :D
After line 8 above (which should now start with 'Survivor survivor = '), you can use:
int x = survivor.getX(); // similar for 'y'
Then you can remove 'survivor'. To determine which virus to add, you can do this:
Actor virus = survivor instanceof SurvivorWASD ? new VirusWASD() : new VirusARROW();
idk1234 idk1234

2016/4/25

#
What would I use to actually add the virus and where would I put it? Things I've tried result in null pointer exeption
danpost danpost

2016/4/25

#
idk1234 wrote...
What would I use to actually add the virus and where would I put it? Things I've tried result in null pointer exeption
Put the following line after line 9 above:
getWorld().addObject(virus, x, y);
idk1234 idk1234

2016/4/25

#
The x and y don't work, comes up with: Cannot find symbol. EDIT: I put it below the int x and int y and it successfully complied. But it still comes up with null pointer exeption
danpost danpost

2016/4/25

#
idk1234 wrote...
EDIT: I put it below the int x and int y and it successfully complied. But it still comes up with null pointer exeption
Please show the code you are using (cannot fix unseen code). Indicate the line the error occurs on. Show all code leading to and related to that line.
idk1234 idk1234

2016/4/25

#
Heres the 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 Virus Virus;
    private VirusWASD VirusWASD;
    private VirusARROW VirusARROW;
    private TwoPlayer_Button TwoPlayer_Button;
    private Animal Animal;
    private Actor Actor;
    private Survivor Survivor;
    public Virus getVirus()
    {
        return Virus;
    }
    
    public Survivor getSurvivor()
    {
        return Survivor;
    }

    public Actor getActor()
    {
        return Actor;
    }

    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 VirusARROW getVirusArrow()
    {
        return VirusARROW;
    }

    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); 
        act();
    }

    /**
     * Act Method
     */
    public void act()
    {if (Greenfoot.mouseClicked(TwoPlayer_Button))
        {
            prepare();
            infect();

        }
    }

    /**
     * Infect a Survivor with Virus
     */
    public void infect()
    {        
        int survivors = getObjects(Survivor.class).size();
        // execute operation only if there are persons in the world
        if (survivors > 0)
        {
            // choose random survivor (get index)
            int index = Greenfoot.getRandomNumber(survivors);
            // get sur vivor at index
            Survivor survivor = (Survivor)getObjects(Survivor.class).get(index);
            // random survivor selected
            int x = survivor.getX();
            int y = survivor.getY();
            getTwoPlayer().addObject(Virus, x, y);
            removeObject(survivor);
            Actor virus = survivor instanceof SurvivorWASD ? new VirusWASD() : new VirusARROW();
            addObject(virus, Survivor.getX(), Survivor.getY());

        }
    }

    /**
     * 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 = Survivor.getX();
        int y = Survivor.getY();

        world.addObject(newVirusWASD, Survivor.getX(), Survivor.getY());
    }

    /**
     * Create a new Virus(WASD) in the same location as the Survivor was.
     */
    private void createNewVirusARROW()
    {
        VirusARROW newVirusARROW;
        newVirusARROW = new VirusARROW();

        World world;
        world = getTwoPlayer();

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

        int x = Survivor.getX();
        int y = Survivor.getY();

        world.addObject(newVirusARROW, Survivor.getX(), Survivor.getY());
    }

    /**
     * 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 = new SurvivorWASD();
        addObject(SurvivorWASD,393,372);
        SurvivorARROW = new SurvivorARROW();

        addObject(SurvivorARROW,574,536);
    }

}
Error occurs in infect method: getTwoPlayer().addObject(Virus, x, y);
danpost danpost

2016/4/25

#
If you had started your field names with lowercase letters, you would be further along. The term 'TwoPlayer' could refer to a class or it could refer to an object of that class because on line 13 you name the field using the exact name of the class. In doing so, you allowed the 'getTwoPlayer' method to compile properly; but with erroneous results. Anyway, I do not see where you assign any TwoPlayer object to the 'TwoPlayer' field. And besides that, I do not even know why you add a 'getTwoPlayer' method for the TwoPlayer object to use. The 'this' keyword is sufficient to reference the TwoPlayer world in the TwoPlayer class; and using it is optional at the beginning of any method all or field name from within the class. In other words, you can just remove 'getTwoPlayer().' from the beginning of the problem line. And you can remove the 'TwoPlayer' field (line 13) as well as the 'getTwoPlayer' method (lines 51 to 55).
idk1234 idk1234

2016/4/25

#
Ohh I understand now, thanks. Edit: Same error? Still getting null pointer exeption: java.lang.NullPointerException at greenfoot.World.addObject(World.java:396)
danpost danpost

2016/4/25

#
To help out, I reduced the class to this. The less junk you have, the easier it will be to continue on with minimal hassle. I do not know how the TwoPlayer_Button was supposed to work, so I just had the initial infect occur when the scenario was started (when the 'Run' button is clicked on). If something is not how you would like it, we can fix it from here:
import greenfoot.*;

public class TwoPlayer extends World
{    
    public TwoPlayer()
    {
        super(1366, 768, 1);
        addObject(new SurvivorWASD(), 393, 372);
        addObject(new SurvivorARROW(), 574, 536);
    }
    
    public void started()
    {
        if (getObjects(Virus.class).isEmpty()) infect();
    }
    
    public void infect()
    {
        int survivors = getObjects(Survivor.class).size();
        if (survivors > 0)
        {
            int index = Greenfoot.getRandomNumber(survivors);
            Survivor survivor = (Survivor)getObjects(Survivor.class).get(index);
            removeObject(survivor);
            Actor virus = survivor instanceof SurvivorWASD ? new VirusWASD() : new VirusARROW();
            addObject(virus, survivor.getX(), survivor.getY()); 
        }
    }
}
There are more replies on the next page.
1
2
3
4