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
5
idk1234 idk1234

2016/4/26

#
Sweet, so how I had it was I had it only infect and prepare when the TwoPlayer button was clicked. Is it okay if I add lines 7,8 and the infect method back into the act method (- to only execute if I click button) I had before? Because if I do so the infect method isn't working anymore - nothing gets removed
danpost danpost

2016/4/26

#
idk1234 wrote...
Sweet, so how I had it was I had it only infect and prepare when the TwoPlayer button was clicked. Is it okay if I add lines 7,8 and the infect method back into the act method (- to only execute if I click button) I had before? Because if I do so the infect method isn't working anymore - nothing gets removed
I gave you an 'infect' method. You do not need a different one. You can use the button and remove the 'started' method. You can also move lines 8 and 9 into a prepare method so that it can be called some time after the world construction.
idk1234 idk1234

2016/4/26

#
Yeah I've kept the infect method and done as you say but it doesn't remove the selected survivor anymore
danpost danpost

2016/4/26

#
idk1234 wrote...
Yeah I've kept the infect method and done as you say but it doesn't remove the selected survivor anymore
Show the new code with the button.
idk1234 idk1234

2016/4/27

#
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)
 */
import greenfoot.*;

public class TwoPlayer extends World
{    
    private TwoPlayer_Button TwoPlayer_Button;
    public TwoPlayer_Button getTwoPlayer_Button()
    {
        return TwoPlayer_Button;
    }
    
    public TwoPlayer()
    {
        super(1366, 768, 1);
        act();
    }

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

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

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

}
danpost danpost

2016/4/27

#
import greenfoot.*;
 
public class TwoPlayer extends World
{    
    private TwoPlayer_Button twoPlayer_Button;
     
    public TwoPlayer()
    {
        super(1366, 768, 1);
    }
    
    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()); 
        }
    }
 
    public void act()
    {
        if (Greenfoot.mouseClicked(twoPlayer_Button))
        {
            removeObject(twoPlayerButton);
            prepare();
            infect();           
        }
    }
 
    private void prepare()
    {
        addObject(new SurvivorWASD(), 393, 372);
        addObject(new SurvivorARROW(), 574, 536);
    }
 
}
Here, I got rid of duplicate, unnecessary and unwanted code. I also added a line to remove the button when clicked. You know, the action of infecting a player could be considered part of preparing the world. It would be much simpler to choose which one gets infected in the 'prepare' method and have the 'infect' method infect the one chosen. Then those methods would look like this:
private void prepare()
{
    Actor[] survivors = { new SurvivorWASD(), new SurvivorARROW() };
    addObject(survivors[0], 393, 372);
    addObject(survivors[1] 574, 536);
    int chosen = Greenfoot.getRandomNumber(2);
    infect(survivors[chosen]);
}

public void infect(Actor survivor)
{
    removeObject(survivor);
    Actor virus = survivor instanceof SurvivorWASD ? new VirusWASD() : new VirusARROW();
    addObject(virus, survivor.getX(), survivor.getY());
}
This will probably work out better in the long run, anyway, because the 'infect' method is more useful in that form.
idk1234 idk1234

2016/4/28

#
Thank you soo much for ur help, it works!!! Also, are you a moderator here? If not you deserve to be :)
idk1234 idk1234

2016/4/28

#
Ok well last question: How would I delay the infect method from occurring for example 10 second. I want there to be like a grace period where survivors can walk around and then 10 secs later (as example) the infected is chosen. Is this possible?
danpost danpost

2016/4/28

#
idk1234 wrote...
Ok well last question: How would I delay the infect method from occurring for example 10 second. I want there to be like a grace period where survivors can walk around and then 10 secs later (as example) the infected is chosen. Is this possible?
Add an instance int field to track the elapsed time (count act cycles) -- something between 500 and 600 should be a sufficient number to trigger the infect.
idk1234 idk1234

2016/4/29

#
Could you show me how to? I followed a tutorial on a timer that counts DOWN until it hits 0 but its not working because I don't know what method to put inside it. Heres the code
    private void infectDelay()
    {
        if (startInfectDelay >0) {
            startInfectDelay--;
            if (startInfectDelay == 0) {
                //Don't know here
                               
            }
        }
    }
danpost danpost

2016/4/29

#
idk1234 wrote...
Could you show me how to? I followed a tutorial on a timer that counts DOWN until it hits 0 but its not working because I don't know what method to put inside it. Heres the code < Code Omitted >
What did you need the delay timer for? What did you want to do when the time was elapsed?
idk1234 idk1234

2016/4/29

#
I want it to wait 10 seconds before it infects the survivors. In those 10 seconds the survivors can just walk around until infection begins
danpost danpost

2016/4/29

#
Adding the timer is what allows your survivors to walk around by themselves before any infection takes place (by allowing act cycles to continue at a normal rate which using delay or sleep do not do). When the timer has completed, you just want to infect. The act method in the survivor classes will execute on any survivor in the world during the time the timer is counting down.
idk1234 idk1234

2016/4/29

#
So is there any way I can "Delay" the infect code for certain amount of time?
danpost danpost

2016/4/29

#
idk1234 wrote...
So is there any way I can "Delay" the infect code for certain amount of time?
That is what the timer was added to do. Why do you keep asking that?
There are more replies on the next page.
1
2
3
4
5