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

2013/10/21

Need help

buckeye4life22 buckeye4life22

2013/10/21

#
Hi everyone, I've been having trouble with my tag game. I have three players, and they're choosing their colors. I can't seem to find a way to "remember", so to say, their choices. Should I use an array? Please help everyone, I have been stuck here for a while. Thanks for your advice ahead of time!!
danpost danpost

2013/10/21

#
A simple array would be the way to go:
public Color[] playerColors = new Color[3]; // as instance field
Set playerColor to the color of the first player, playerColor to the color of the second, and playerColor to the third one. Either remove the possibilities as they are chosen or check the saved values to make sure that two players do not end up with the same color.
buckeye4life22 buckeye4life22

2013/10/21

#
What do you mean by playerColor? (Sorry I'm fairly new at this)
danpost danpost

2013/10/21

#
'Color' defines a one-dimensional array that holds Color objects. 'new Color' initialized the array and sets its length to 3 (the number of Color objects it can hold). 'playerColor' is the name given to the array and each of the three elements can be accessed by using its index number in square brackets as shown above. Remember, in java, counting starts at zero; so, the elements are at zero, one and two (playerColor, playerColor, and playerColor). With the initialized array, you can not assign values (or colors) to the array ('playerColor = java.awt.Color.green;' would assign the color green to the second player).
buckeye4life22 buckeye4life22

2013/10/21

#
Well where do you set playerColor though?
danpost danpost

2013/10/21

#
Wherever you have the code where the players choose their colors.
buckeye4life22 buckeye4life22

2013/10/21

#
Ohhhhhhhh Thank you very much danpost!!
buckeye4life22 buckeye4life22

2013/11/7

#
I'm sorry for this delayed reply, if your still there. I still don't see how I can extract that information that I put in the array to assign a color to the Players. If your not following, I have an array, I don't know what to do with it.
danpost danpost

2013/11/7

#
Please show what you have. Do not just say you have it. Use the 'code' tag below the 'Post a reply' box for entering code in your post.
buckeye4life22 buckeye4life22

2013/11/7

#
Okay, here's my World
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class ChooseColorWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ChooseColorWorld extends World
{
    //public Buttons[] playerColors = new Buttons[3];
    public int whichScreen = 0;
    public int whoIsChoosing=0;
    /**
     * Constructor for objects of class ChooseColorWorld.
     * 
     */
    public ChooseColorWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1100, 600, 1); 
        theMethod(whichScreen);

    }
    public void theMethod(int whichScreen)
    {
        
        if(whichScreen == 0)//No choice yet.
        {
            addObject(new Player1Choose(),635,300);
            addObject(new RedButton(),400,400);
            addObject(new BlueButton(),550,400);
            addObject(new GreenButton(),700,400);
        }
        if(whichScreen==1)//If first player chose red.
        {
            List<Actor> actors = getObjects(null);    
            removeObjects(actors);
            addObject(new Player2Choose(),635,300);
            addObject(new BlueButton(),475,400);
            addObject(new GreenButton(),625,400);
            whoIsChoosing = 1;
        }
        if(whichScreen==2)//If first player chose blue.
        {
            List<Actor> actors = getObjects(null);
            removeObjects(actors);
            addObject(new Player2Choose(),635,300);
            addObject(new RedButton(),475,400);
            addObject(new GreenButton(),625,400);
            whoIsChoosing=1;
        }
        if(whichScreen==3)//If first player chose green
        {
            List<Actor>actors = getObjects(null);
            removeObjects(actors);
            addObject(new Player2Choose(),635,300);
            addObject(new RedButton(),475,400);
            addObject(new BlueButton(),625,400);
            whoIsChoosing=1;
        }
        if(whichScreen==4)//If red and blue are chosen
        {
            List<Actor>actors = getObjects(null);
            removeObjects(actors);
            addObject(new Player3Choose(),660,300);
            addObject(new GreenButton(),550,400);
        }
        if(whichScreen==5)//If blue and green are chosen 
        {
            List<Actor>actors=getObjects(null);
            removeObjects(actors);
            addObject(new Player3Choose(),660,300);
            addObject(new RedButton(),550,400);
        }
        if(whichScreen==6)//If red and green are chosen
        {
            List<Actor>actors=getObjects(null);
            removeObjects(actors);
            addObject(new Player3Choose(),635,300);
            addObject(new BlueButton(),550,400);
        }
    }
    public void someMethod()
    {
        Players players= new Players();
        if(players.playerChoices[0]=="red" && players.playerChoices[1]=="blue")
        {
            whichScreen=4;
        }
        if(players.playerChoices[0]=="blue" && players.playerChoices[1]=="red")
        {
            whichScreen=4;
        }
        if(players.playerChoices[0]=="red" && players.playerChoices[1]=="green")
        {
            whichScreen=6;
        }
        if(players.playerChoices[0]=="green" && players.playerChoices[1]=="red")
        {
            whichScreen=6;
        }
        if(players.playerChoices[0]=="blue" && players.playerChoices[1]=="green")
        {
            whichScreen=5;
        }
        if(players.playerChoices[0]=="green" && players.playerChoices[1]=="blue")
        {
            whichScreen=5;
        }
    }
}
My Button: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.*; /** * Write a description of class RedButton here. * * @author (your name) * @version (a version number or a date) */ public class RedButton extends Buttons { public boolean choseMe=false; public RedButton() { GreenfootImage image = new GreenfootImage(100, 40); image.setColor(Color.RED); image.fill(); setImage(image); } /** * Act - do whatever the RedButton wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { ifClicked(); } public void ifClicked() { if(Greenfoot.mouseClicked(this)) { ChooseColorWorld w = (ChooseColorWorld)getWorld(); Players players = new Players(); if(w.whoIsChoosing == 0) { w.theMethod(1); players.colors=0; } if(w.whoIsChoosing==1) { w.someMethod(); } } } } And my Player has no content yet.
buckeye4life22 buckeye4life22

2013/11/7

#
Sorry, I have to leave, so I won't reply when you do.
danpost danpost

2013/11/7

#
Let me know if this is correct. You have three colors (red, green and blue) to be chosen, one to each player. If this is so, then all you need to do is remove the buttons when clicked on. The number of buttons in the world when one is clicked on subtracted from four is the player to get the color of that button. When a button is clicked on and there are two buttons in the world at that time, then the second player gets the color of the button clicked on and the color of the remaining button goes to the third player. As these colors are chosen, they should be set in an array in the world class. It might be easier to add the buttons into the world and, instead of checking for buttons clicks in the button class, check for the button clicks in the world class (it is easier to check the number of buttons there and the array should be there). You can use the 'instanceof' keyword to determine which actor the click happened on
if (getMouseInfo().getActor() instanceof RedButton)
after determining there was a click and that the mouse info object returned is not 'null'.
You need to login to post a reply.