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

2014/8/22

Priniting

1
2
applefan57 applefan57

2014/8/22

#
Hi, I am fairly new to Greenfoot. How do I set a time for a print statement to print? at the moment I have used an if statement with the condition of if an object is clicked, but the print statement only prints while the object is clicked. How would I lengthen this time?
danpost danpost

2014/8/22

#
Please show the code you are using: * if statement to print statement and other related code in that class * class code of actor, if any, used to show text Use the 'code' link below the 'Post a reply' box to insert your code into your post.
applefan57 applefan57

2014/8/22

#
public void p1acannotMove(){
        GameBoard gameWorld = (GameBoard) getWorld();  // get a reference to the world
        Player1CounterA player1countera = gameWorld.getPlayer1CounterA();//gets a reference to the counter
        Player1CounterB player1counterb = gameWorld.getPlayer1CounterB();  
        Dice dice = gameWorld.getDice();//gets a reference to the dice
        if(Greenfoot.mouseClicked(player1countera)){ // if Player1CounterA is clicked, the if statement will run
            if(player1countera.getY() == 0 || player1countera.getY() == 10 && Dice.roll == 4 ){ // if Player 1 counter a cannot move, the if statement will run
                if(player1counterb.getY() != 0 || player1counterb.getY() != 10 && Dice.roll != 4){// if player 1 counter b cannot move, the if statemnt will run
                    setImage(new GreenfootImage(PlayerNameInput.text2+", you cannot move this counter" , 20, Color.WHITE, Color.BLUE));

                }

            }
        }

    }
applefan57 applefan57

2014/8/22

#
That's the method that I'm struggling with, and I'll put in the class code as well
applefan57 applefan57

2014/8/22

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

public class PlayerTurn extends logic
{
    Dice dice = new Dice();
    RollDiceButton rollDiceButton = new RollDiceButton();

    public static boolean PlayerOne = false;

    public void act(){
        playerTurn();//calls 
        hasWon();
        p1cannotMove();
        p2cannotMove();
        p1acannotMove();
        p1bcannotMove();
        p2acannotMove();
        p2bcannotMove();

    }

    public void playerTurn(){
        GameBoard gameWorld = (GameBoard) getWorld();  // get a reference to the world
        Player1CounterA player1countera = gameWorld.getPlayer1CounterA();//gets a reference to the counter
        Player1CounterB player1counterb = gameWorld.getPlayer1CounterB();//gets a reference to the counter
        Player2CounterA player2countera = gameWorld.getPlayer2CounterA();//gets a reference to the counter
        Player2CounterB player2counterb = gameWorld.getPlayer2CounterB();//gets a reference to the counter
        RollDiceButton rolldicebutton = gameWorld.getRollDiceButton();// gets a reference to the RollDiceButton
        Dice dice = gameWorld.getDice();//gets a reference to the dice

        if(rollDiceButton.count%2==0){
            PlayerOne = true;//boolean PlayerOne set to true
            setImage(new GreenfootImage(PlayerNameInput.text2+", It's your turn" , 20, Color.WHITE, Color.BLUE));

            if ( player1countera.p1hasMoved == false){//if either player 1 counters  is not moved, the if statement will run

                

                if ( Dice.roll==1){ // if the dice roll equals 1, then the if satement will run
                    setImage(new GreenfootImage(PlayerNameInput.text2+", Select the counter to move up 1 space" , 20, Color.WHITE, Color.BLUE)); 
                }
                else{
                    //do nothing
                }

                if ( Dice.roll==2){// if the dice roll equals 2, then the if satement will run
                    setImage(new GreenfootImage(PlayerNameInput.text2+", Select the counter to move up 2 spaces" , 20, Color.WHITE, Color.BLUE)); 
                }
                else{
                    // do nothing
                }

                if ( Dice.roll==3 ){// if the dice roll equals 1, then the if satement will run
                    setImage(new GreenfootImage(PlayerNameInput.text2+", Select the counter to move up 3 spaces" , 20, Color.WHITE, Color.BLUE)); 

                }
                else{
                    //do nothing
                }

                if ( Dice.roll==4){// if the dice roll equals 4, then the if satement will run
                    if(getY()==10 || this.getY()== 6 || this.getY()==0)
                    {
                    }
                    else
                    {
                        setImage(new GreenfootImage(PlayerNameInput.text2+", Select the counter to move down 1 space" , 20, Color.WHITE, Color.BLUE)); 
                    }
                }
                else{
                    //do nothing
                }

            }
        }

        else { //if Player 1 has moved, the else statement will run
            PlayerOne = false;
            setImage(new GreenfootImage(PlayerNameInput2.text2+", It's your turn" , 20, Color.BLACK, Color.YELLOW));    

            if (player2countera.p2hasMoved == false){//if either player 2 counter has moved, the else statement runs

                if ( Dice.roll==1){ // if the dice roll equals 1, then the if satement will run
                    setImage(new GreenfootImage(PlayerNameInput2.text2+", Select the counter to move up 1 space" , 20, Color.BLACK, Color.YELLOW)); 
                }
                else{}

                if ( Dice.roll==2){// if the dice roll equals 2, then the if satement will run
                    setImage(new GreenfootImage(PlayerNameInput2.text2+", Select the counter to move up 2 spaces" , 20, Color.BLACK, Color.YELLOW)); 
                }
                else{}

                if ( Dice.roll==3 ){// if the dice roll equals 1, then the if satement will run
                    setImage(new GreenfootImage(PlayerNameInput2.text2+", Select the counter to move up 3 spaces" , 20, Color.BLACK, Color.YELLOW)); 

                }
                else{}

                if ( Dice.roll==4){// if the dice roll equals 4, then the if satement will run
                    if(getY()==10 || this.getY()== 6 || this.getY()==0)
                    {
                        //do nothing
                    }
                    else
                    {
                        setImage(new GreenfootImage(PlayerNameInput2.text2+", Select the counter to move down 1 space" , 20, Color.BLACK, Color.YELLOW)); 
                    }
                }
                else{
                    //do nothing
                }

            }
            else{
                //do nothing
            }

        }

    }

    public void hasWon(){
        GameBoard gameWorld = (GameBoard) getWorld();  // get a reference to the world
        Player1CounterA player1countera = gameWorld.getPlayer1CounterA();//gets a reference to the counter
        Player1CounterB player1counterb = gameWorld.getPlayer1CounterB();//gets a reference to the counter
        Player2CounterA player2countera = gameWorld.getPlayer2CounterA();//gets a reference to the counter
        Player2CounterB player2counterb = gameWorld.getPlayer2CounterB();//gets a reference to the counter
        if(player1countera.getY() == 0 && player1counterb.getY() == 0 ){
            setImage(new GreenfootImage(PlayerNameInput.text2+", you have won!" , 20, Color.WHITE, Color.BLUE)); 
            Greenfoot.setWorld(new MenuWorld());//goes to menu world
        }
        if(player2countera.getY() == 0 && player2counterb.getY() == 0 ){
            setImage(new GreenfootImage(PlayerNameInput2.text2+", you have won!" , 20, Color.BLACK, Color.YELLOW)); 
            Greenfoot.setWorld(new MenuWorld()); // goes to menu world
        }
    }

    public void p1cannotMove()
    {  GameBoard gameWorld = (GameBoard) getWorld();  // get a reference to the world
        Dice dice = gameWorld.getDice();//gets a reference to the dice
        Player1CounterA player1countera = gameWorld.getPlayer1CounterA();// get a reference to the counter
        Player1CounterB player1counterb = gameWorld.getPlayer1CounterB();  

        RollDiceButton rolldicebutton = gameWorld.getRollDiceButton();
        if(this.PlayerOne == true ){ //if boolean PlayerOne is true, the if stement runs
            if ( player1countera.getY() == 10 && player1counterb.getY() == 10 && player1countera.p1hasMoved == false && Dice.roll == 4 ){
                setImage(new GreenfootImage(PlayerNameInput.text2+", you cannot move, please roll the dice" , 20, Color.WHITE, Color.BLUE));
            }
            else{
                //do nothing
            }
        }

    }

    public void p2cannotMove()
    {GameBoard gameWorld = (GameBoard) getWorld();  // get a reference to the world
        Dice dice = gameWorld.getDice();//gets a reference to the dice
        Player2CounterA player2countera = gameWorld.getPlayer2CounterA();// get a reference to the counter
        Player2CounterB player2counterb = gameWorld.getPlayer2CounterB();  

        RollDiceButton rolldicebutton = gameWorld.getRollDiceButton();// gets a reference to the RollDiceButton

        if(PlayerTurn.PlayerOne == false){
            if ( player2countera.getY() == 10 && player2counterb.getY() == 10 && player2countera.p2hasMoved == false && Dice.roll == 4 ){
                setImage(new GreenfootImage(PlayerNameInput2.text2+", you cannot move, please roll the dice" , 20, Color.BLACK, Color.YELLOW));
            }
            else{
                //do nothing
            }
        }

    }

    public void p1acannotMove(){
        GameBoard gameWorld = (GameBoard) getWorld();  // get a reference to the world
        Player1CounterA player1countera = gameWorld.getPlayer1CounterA();//gets a reference to the counter
        Player1CounterB player1counterb = gameWorld.getPlayer1CounterB();  
        Dice dice = gameWorld.getDice();//gets a reference to the dice
        if(Greenfoot.mouseClicked(player1countera)){ // if Player1CounterA is clicked, the if statement will run
            if(player1countera.getY() == 0 || player1countera.getY() == 10 && Dice.roll == 4 ){ // if Player 1 counter a cannot move, the if statement will run
                if(player1counterb.getY() != 0 || player1counterb.getY() != 10 && Dice.roll != 4){// if player 1 counter b cannot move, the if statemnt will run
                    setImage(new GreenfootImage(PlayerNameInput.text2+", you cannot move this counter" , 20, Color.WHITE, Color.BLUE));

                }

            }
        }

    }

    public void p1bcannotMove(){
        GameBoard gameWorld = (GameBoard) getWorld();  // get a reference to the world
        Player1CounterA player1countera = gameWorld.getPlayer1CounterA();//gets a reference to the counter
        Player1CounterB player1counterb = gameWorld.getPlayer1CounterB();  
        Dice dice = gameWorld.getDice();//gets a reference to the dice
        if(Greenfoot.mouseClicked(player1counterb)){// if Player1CounterA is clicked, the if statement will run
            if(player1counterb.getY() == 0 || player1counterb.getY() == 10 && Dice.roll == 4 ){// if Player 1 counter b cannot move, the if statement will run
                if(player1countera.getY() != 0 || player1countera.getY() != 10 && Dice.roll != 4){// if player 1 counter a cannot move, the if statemnt will run
                    setImage(new GreenfootImage(PlayerNameInput.text2+", you cannot move this counter" , 20, Color.WHITE, Color.BLUE));

                }
            }
        }
        else{

        }
    }

    public void p2acannotMove(){
        GameBoard gameWorld = (GameBoard) getWorld();  // get a reference to the world
        Player2CounterA player2countera = gameWorld.getPlayer2CounterA();//gets a reference to the counter
        Player2CounterB player2counterb = gameWorld.getPlayer2CounterB();  
        Dice dice = gameWorld.getDice();//gets a reference to the dice
        if(Greenfoot.mouseClicked(player2countera)){// if Player2CounterA is clicked, the if statement will run
            if(player2countera.getY() == 0 || player2countera.getY() == 10 && Dice.roll == 4 ){// if Player 2 counter a cannot move, the if statement will run
                if(player2counterb.getY() != 0 || player2counterb.getY() != 10 && Dice.roll != 4){// if player 2 counter b cannot move, the if statemnt will run
                    setImage(new GreenfootImage(PlayerNameInput2.text2+", you cannot move this counter" , 20, Color.BLACK, Color.YELLOW));

                }
            }
        }

    }

    public void p2bcannotMove(){
        GameBoard gameWorld = (GameBoard) getWorld();  // get a reference to the world
        Player2CounterA player2countera = gameWorld.getPlayer2CounterA();//gets a reference to the counter
        Player2CounterB player2counterb = gameWorld.getPlayer2CounterB();  
        Dice dice = gameWorld.getDice();//gets a reference to the dice
        if(Greenfoot.mouseClicked(player2counterb)){// if Player2CounterA is clicked, the if statement will run
            if(player2counterb.getY() == 0 || player2counterb.getY() == 10 && Dice.roll == 4 ){// if player 2 counter b cannot move, the if statemnt will run
                if(player2countera.getY() != 0 || player2countera.getY() != 10 && Dice.roll != 4){// if Player 2 counter a cannot move, the if statement will run
                    setImage(new GreenfootImage(PlayerNameInput2.text2+", you cannot move this counter" , 20, Color.BLACK, Color.YELLOW));

                }
            }
        }

    }
}
applefan57 applefan57

2014/8/22

#
The prints that I am struggling with are the p1aCannotMove, p1bCannotMove, p2aCannotMove, and the p2bCannotMove
Super_Hippo Super_Hippo

2014/8/22

#
You could add a
private int delay;
and start the 'act' method with
if (delay>0) {delay--; return;}
Then you only have to add
delay = 300; //adjust this to the time you want
every time you want the text to be showed. By the way, you could simplify your code, for example remove useless 'else //do nothing' and save your references as fields instead of getting them new every time. The 'Dice.roll==' could also be done in a nicer way probably. It is very much code for not that much happening and it is quite confusing like this.
danpost danpost

2014/8/22

#
You have an 'if' statement inside another 'if' statement inside the 'if' statement that checks for a click. The way you have written it, both sets of inside 'if' conditions must be true for the print to occur. If you want it that if either of the two sets of conditions (inner) to cause the print, then use (for an example from lines 199 and 200):
if (((player1counterb.getY() == 0 || player1counterb.getY() == 10) && Dice.roll == 4) ||
((player1countera.getY() != 0 || player1countera.getY() != 10) && Dice.roll != 4)) {
applefan57 applefan57

2014/8/22

#
thanks, but where and how would I insert the delay integer? I think the problem is that it only prints when the object is cocked, and then disappears, but I wanted to make it print the "you cannot move this counter" for a longer period of time so that it can be read
applefan57 applefan57

2014/8/22

#
*clicked
danpost danpost

2014/8/22

#
It seems that your scenario is turn-based. As such, I do not think that using 'Greenfoot.delay()' would be a hindrance. Just place the delay call after setting the text. Just note that no action by the user or by other actors will occur during the delay time.
applefan57 applefan57

2014/8/22

#
oh sorry, it is turn based, but if the delay could work for about 5 seconds that would probably work. I'll try thank thanks
applefan57 applefan57

2014/8/22

#
or could i delay it until another outer is clicked?
applefan57 applefan57

2014/8/22

#
*counter
Super_Hippo Super_Hippo

2014/8/22

#
Just place 'delay=300;' at the end of the 'p__cannotmove' methods after 'setImage(...)' and add the other parts that I noticed earlier. As it is right now, you change the image like every act cycle, so if you change it for a certain reason, you will change the image in the next moment again and you couldn't see it. To delay it until a counter is clicked, you can make this delay a boolean and start the act method with something like
if (delay)
{
    if (Greenfoot.mouseClicked(/*counter object*/) delay = false;
    else return;
}
Either this way or you have to add something like the following in the counter class 'act' method.
if (Greenfoot.mouseClicked(this)) PlayerTurn.delay = false;
This only works as it should if there is only one 'PlayerTurn' object and the 'delay' is static. I don't really have an idea how your game works, so I don't know if this helps.
There are more replies on the next page.
1
2