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

2016/4/29

#
Like I said
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
    private void infectDelay()
    {
        if (startInfectDelay >0) {
            startInfectDelay--;
            if (startInfectDelay == 0) {
                //Don't know here
                               
            }
        }
    }
I don't know exactly what to put in the marked field
danpost danpost

2016/4/29

#
idk1234 wrote...
Like I said ... I don't know exactly what to put in the marked field
infect();
That is what you wanted to do when the 10 seconds were up.
idk1234 idk1234

2016/4/30

#
I guess I should have explained a bit better, infect(); doesn't work because the actual method is
public void infect(Actor survivor)
I tried putting "infect(Actor survivor);" but it's not working - comes up with ") expected" and "illegal start of expression.
danpost danpost

2016/4/30

#
idk1234 wrote...
I guess I should have explained a bit better, infect(); doesn't work because the actual method is
public void infect(Actor survivor)
I tried putting "infect(Actor survivor);" but it's not working - comes up with ") expected" and "illegal start of expression.
Alright, try this in the marked field:
Class[] classes = { SurvivorWASD.class, SurvivorARROW.class };
int rand = Greenfoot.getRandomNumber(classes.length);
infect((Actor)getObjects(classes[rand]).get(0));
idk1234 idk1234

2016/4/30

#
It's not working, am I doing something wrong?
import greenfoot.*;

public class TwoPlayer extends World
{    
    private TwoPlayer_Button twoPlayer_Button;
    private int startInfectDelay = 100;
    public TwoPlayer()
    {
        super(1366, 768, 1);
        act();
    }

    public void infect2()
    {
        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))
        {
            prepare();

        }
    }

    private void infectDelaystart()
    {
        startInfectDelay = 100;
    }

    private void infectDelay()
    {
        if (startInfectDelay >0) {
            startInfectDelay--;
            if (startInfectDelay == 0) {
                Class[] classes = { SurvivorWASD.class, SurvivorARROW.class };
                int rand = Greenfoot.getRandomNumber(classes.length);
                infect((Actor)getObjects(classes[rand]).get(0));   
            }
        }
    }

    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)
    {
        Actor virus = survivor instanceof SurvivorWASD ? new VirusWASD() : new VirusARROW();
        addObject(virus, survivor.getX(), survivor.getY());
        removeObject(survivor);

    }
}
danpost danpost

2016/4/30

#
First, remove line 10. You cannot check for mouse clicks during the construction of your world (which is what the 'act' method does). Nevermind, I was going to tell you to modify the 'prepare' and 'infect' methods and remove everything else except the constructor and act methods; but maybe I will show you what you should have here. Try this:
import greenfoot.*;
 
public class TwoPlayer extends World
{    
    private TwoPlayer_Button twoPlayer_Button;
    private int countdown;
    private Actor survivors;
    
    public TwoPlayer()
    {
        super(1366, 768, 1);
    }
    
    public void act()
    {
        prepare();
        infect();
    }

    private void prepare()
    {
        if (survivors == null)
        {
            survivors = new Actor[] { new SurvivorWASD(), new SurvivorARROW() };
            addObject(survivors[0], 393, 372);
            addObject(survivors[1], 574, 536);
        }
    }
    
    private void infect()
    {
        if (Greenfoot.mouseClicked(twoPlayer_Button)) // countdown starting
        {
            countdown = 100;
        }
        if (countdown == 0) return; // countdown already completed
        countdown--;
        if (countdown == 0) // countdown just completed
        {
            Actor viruses = new Actor[] { new VirusWASD(), new VirusARROW() }; // must be same order as 'survivors'
            int chosen = Greenfoot.getRandomNumber(2);
            addObject(viruses[chosen], survivor[chosen].getX(), survivor[chosen].getY());
            removeObject(survivor[chosen]);
            removeObject(twoPlayer_Button); // optional
        }
    }
}
idk1234 idk1234

2016/4/30

#
Thanks for the code and as well as explaining it. I am getting errors and am unable to compile it. Errors in prepare: incompatiables types on
new Actor[] { new SurvivorWASD(), new SurvivorARROW() };
and "array required but greenfoot.Actor found on the
[0] & [1]
I also get the first error in the infect method except with Virus. Do I need to add something like
private Survivor survivor;
?
Super_Hippo Super_Hippo

2016/4/30

#
Change line 7 to this:
private Actor[] survivors;
I think this should do it. I think line 40 has the same problem.
danpost danpost

2016/4/30

#
Super_Hippo wrote...
Change line 7 to this:
private Actor[] survivors;
I think this should do it. I think line 40 has the same problem.
These changes were definitely needed. My appologies.
idk1234 idk1234

2016/5/1

#
Thanks for the help guys, but I still get the same problem?
danpost danpost

2016/5/1

#
idk1234 wrote...
Thanks for the help guys, but I still get the same problem?
Be specific, You mentioned quite a few errors. What problem still remains? What is the current code around which the problem exists? etc.
danpost danpost

2016/5/1

#
idk1234 wrote...
I also get the first error in the infect method except with Virus. Do I need to add something like
private Survivor survivor;
?
No. On lines 42 and 43, change all three occurrences of 'survivor' to 'survivors' (plural)..
idk1234 idk1234

2016/5/1

#
Ok so most of the errors are gone now except 2. I get "incompatible types: greenfoot.Actor cannot be converted to greenfoot.Actor" on line 40 at
new Actor[] { new VirusWASD(), new VirusARROW() };
and also get an error on line 42 at
[chosen]
"array required, but greenfoot.Actor found"
danpost danpost

2016/5/1

#
idk1234 wrote...
Ok so most of the errors are gone now except 2. I get "incompatible types: greenfoot.Actor cannot be converted to greenfoot.Actor" on line 40 at
new Actor[] { new VirusWASD(), new VirusARROW() };
and also get an error on line 42 at
[chosen]
"array required, but greenfoot.Actor found"
Change line 40 to:
Actor[] viruses = new Actor[] { new VirusWASD(), new VirusARROW() };
That should solve both errors.
idk1234 idk1234

2016/5/2

#
Thanks, now it has complied but the counter doesn't seem to be working, I have also tried changing numbers but still nothing. Heres the full code right now:
import greenfoot.*;

public class TwoPlayer extends World
{
    private TwoPlayer_Button twoPlayer_Button;
    private int countdown;
    private Actor[] survivors;
    
    public TwoPlayer()
    {
        super(1366, 768, 1);
    }

    public void act()
    {
        prepare();
        infect();
    }

    private void prepare()
    {
        if (survivors == null)
        {
            survivors = new Actor[] { new SurvivorWASD(), new SurvivorARROW() };
            addObject(survivors[0], 393, 372);
            addObject(survivors[1], 574, 536);
        }
    }

    private void infect()
    {
        if (Greenfoot.mouseClicked(twoPlayer_Button)) // countdown starting
        {
            countdown = 10;
        }
        if (countdown == 0) return; // countdown already completed
        countdown--;
        if (countdown == 0) // countdown just completed
        {
            Actor[] viruses = new Actor[] { new VirusWASD(), new VirusARROW() }; // must be same order as 'survivors'
            int chosen = Greenfoot.getRandomNumber(2);
            addObject(viruses[chosen], survivors[chosen].getX(), survivors[chosen].getY());
            removeObject(survivors[chosen]);
            //removeObject(twoPlayer_Button); // optional
        }
    }
}
Also I'm not sure if this helps but when I click it "infects". Again, I really appreciate your help
There are more replies on the next page.
1
2
3
4
5
6