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
7
danpost danpost

2016/5/2

#
On line 34, you set the counter value to 10. Ten act cycles will expire quite quickly (maybe 0.2 seconds). Try increasing the value to something like 300.
idk1234 idk1234

2016/5/2

#
So I increased it to 300 but it doesn't start the count down. It only starts the count down when I left click
danpost danpost

2016/5/2

#
idk1234 wrote...
So I increased it to 300 but it doesn't start the count down. It only starts the count down when I left click
When is the countdown supposed to start?
idk1234 idk1234

2016/5/2

#
When TwoPlayer_Button is clicked
idk1234 idk1234

2016/5/2

#
Also is there anyway I could "scroll" the world with more than 1 player?
danpost danpost

2016/5/2

#
idk1234 wrote...
Also is there anyway I could "scroll" the world with more than 1 player?
The only way to do that is to split the screen.
idk1234 wrote...
When TwoPlayer_Button is clicked
Is not that when the counter starts?
idk1234 idk1234

2016/5/2

#
The counter only starts when I left click for some weird reason
danpost danpost

2016/5/2

#
danpost wrote...
When is the countdown supposed to start?
idk1234 wrote...
When TwoPlayer_Button is clicked...The counter only starts when I left click for some weird reason
What am I not being told here? I ask when it is suppose to start -- you say when the button is clicked; and you are complaining about it starting when you click.
idk1234 idk1234

2016/5/3

#
I mean it doesn't start when I click TwoPlayer_Button. It starts when I left click on the world (after clicking on button).
danpost danpost

2016/5/3

#
idk1234 wrote...
I mean it doesn't start when I click TwoPlayer_Button. It starts when I left click on the world (after clicking on button).
Out of curiosity, what is the code in your TwoPlayer_Button class?
idk1234 idk1234

2016/5/3

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class TwoPlayer_Button here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TwoPlayer_Button extends Actor
{    
    /**
     * Act - do whatever the TwoPlayer_Button wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        startGame2P();
    }    

    /**
     * Start 2 player world.
     */
    public void startGame2P()
    {
        if (Greenfoot.mouseClicked(this))
        {
            Greenfoot.setWorld(new TwoPlayer());
        }
    }    
}    
Oh, I think the problem was here! As the button itself it trying to set the world?
idk1234 idk1234

2016/5/3

#
Nvm not sure anymore
idk1234 idk1234

2016/5/3

#
So, I added code so I could add walls (collision) but the walls end up spawning as soon as I press run. Also, the survivors don't spawn where they're supposed to (they shove up top left corner) when I moved the walls the survivors are fine. Can I not spawn survivors when collision is added? P.s. The wall picture is same size at world (png)
import greenfoot.*;

public class TwoPlayer extends Main_Menu
{
    private TwoPlayer_Button twoPlayer_Button;
    private int countdown;
    private Actor[] survivors;
    private Walls Walls;
    public TwoPlayer()
    {
        
    }

    public void act()
    {
        prepare();
        canAddWalls();//Walls kept spawning 5 million times
        walls(); 
        infect();
    }
    
    private void walls()
    {
        if (canAddWalls()) addObject(new Walls(), 683, 384);
    }
    public boolean canAddWalls()
    {
        // in the world class, use this line
        return getObjects(Walls.class).size() < 1;
    }

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

    private void infect()
    {
        if (Greenfoot.mouseClicked(twoPlayer_Button)) // countdown starting
        {
            countdown = 300;
        }
        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
        }
    }
}
danpost danpost

2016/5/3

#
idk1234 wrote...
< Code Omitted > Oh, I think the problem was here! As the button itself it trying to set the world?
This class should have no excess code in it (it should be basically how it was when the class was first created (an empty 'act' method and no 'startGame2P' method).
Super_Hippo Super_Hippo

2016/5/3

#
Btw, line 17 does nothing. The method which is called returns true or false, but this boolean value isn't used. The line there is useless.
There are more replies on the next page.
1
2
3
4
5
6
7