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

2019/1/22

how to create and remove an object depending on if a key is being pressed

TobyRoby TobyRoby

2019/1/22

#
Hey, i would like to display a text box with instructions of how to play a game that i am making. This text box should appear when the 'm' key is being pressed down the remove when the key is let go of. Following is the code in my world class and then the code of my instructionsPage class:
instructionsPage instructionsPage = new instructionsPage();
        
        if ("m".equals(Greenfoot.getKey()))
        {
            addObject(instructionsPage,350,150);
            removeObject(instructionsPage);
        }
public class instructionsPage extends Actor
{
    String text = "-The objective of the game is to capture more pebbles in the mancala than the opposing player\n"+
    "-Fill each of the 12 dips up with 4 pebbles in each (leave the mancalas empty)\n"+
    "-Player one picks up all the pebbles in a chosen dip (of which there are six to choose from), they then put one pebble in each hole going anticlockwise around the board.\n"+
    "-If you run into your own mancala you deposit a pebble, if you run into the opponent’s mancala you skip it\n"+
    "-If the last pebble distributed lands on their mancala then player one has another turn\n"+
    "-If the last pebble distributed from a dip lands in a dip with no pebbles in it, then the player picks up that single pebble and all the opponents pebbles in the adjacent dip.\n"+
    "-The game is complete when one of the players has no pebbles left in their 6 dips, when this happens the remaining pebbles in the dips of the second player are put into their mancala. The pebbles of each Mancala are added up (usually this would be done by the players, but mine will be automated) and the player with the most pebbles wins.\n";
    public void act() 
    {
        setImage(new GreenfootImage(text, 15, Color.BLACK, Color.WHITE));
    }    
}
TobyRoby TobyRoby

2019/1/22

#
also the part i am having the problem with is the removing of the text, when i press the 'm' key it is created fine.
danpost danpost

2019/1/22

#
TobyRoby wrote...
also the part i am having the problem with is the removing of the text, when i press the 'm' key it is created fine.
I doubt that. It looks like nothing happens initially when you press the "m" key. Only if you continue to hold it down will it be displayed. But, it also looks like it is immediately removed from the world before is actually has a chance to be rendered in the display. At any rate, use isKeyDown instead of getKey and put the removal line in an else clause.
TobyRoby TobyRoby

2019/1/22

#
this is the rest of the world class (without all of the stuff that is definitely irrelevant). also it is created by just pressing the key not holding it down. all of the instructionsPage class is the same as above
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class MyWorld extends World
{

    Dip[] dipArray = new Dip[14];
    
    int playerTurn = 1;
    
    public MyWorld()
    {    
        super(700, 400, 1); 
        
        int totalPebble = 0;
        
        mancalaBoard mancalaBoard = new mancalaBoard();
        addObject(mancalaBoard,350,200);
        
        resetButton resetButton = new resetButton();
        addObject(resetButton,660,360);
        
        playerTurn playerTurn = new playerTurn();
        addObject(playerTurn,350,50);
        
        
        dipArray[0]=new Dip();  //these make the dips in the array positions and
        addObject(dipArray[0],208,259);     //decide their locations on the board
        dipArray[1]=new Dip();
        addObject(dipArray[1],269,263);
        dipArray[2]=new Dip();
        addObject(dipArray[2],328,259);
        dipArray[3]=new Dip();
        addObject(dipArray[3],388,260);
        dipArray[4]=new Dip();
        addObject(dipArray[4],441,260);
        dipArray[5]=new Dip();
        addObject(dipArray[5],506,262);
        dipArray[6]=new Dip();
        addObject(dipArray[6],554,189);
        dipArray[7]=new Dip();
        addObject(dipArray[7],504,153);
        dipArray[8]=new Dip();
        addObject(dipArray[8],444,157);
        dipArray[9]=new Dip();
        addObject(dipArray[9],381,151);
        dipArray[10]=new Dip();
        addObject(dipArray[10],322,155);
        dipArray[11]=new Dip();
        addObject(dipArray[11],262,153);
        dipArray[12]=new Dip();
        addObject(dipArray[12],204,153);
        dipArray[13]=new Dip();
        addObject(dipArray[13],147,197);
        
        
        
        for ( int i=0; i<dipArray.length; i++)
        {
            dipArray[i].totalPebble=4;  //this for loop makes all of the dips have a pebble value of 4
        }

        dipArray[6].totalPebble=0;  //these two lines change th value of the 'mancalas' so that they start with a value of 0
        dipArray[13].totalPebble=0;
    }
    
    public void act()   //this function is executed repeatedly during run-time
    {
        int x = 0;
        int y = 0;
        
        instructionsPage instructionsPage = new instructionsPage();
        
        if ("m".equals(Greenfoot.getKey()))
        {
            addObject(instructionsPage,350,150);
            World.removeObject(instructionsPage);
        }
danpost danpost

2019/1/22

#
Move line 71 to line 5 and see my edited post above. Also, change line 76 back to how you had it in your 1st post.
TobyRoby TobyRoby

2019/1/22

#
what do you mean by see my edited post above? also i did what you said above and it didnt do anything, i think it was creating and deleting as i click 'm'. Is there a way of creating it whilst a button (e.g. 'm') is being held down and then removing it after the key is let go?
danpost danpost

2019/1/23

#
TobyRoby wrote...
what do you mean by see my edited post above? also i did what you said above and it didnt do anything, i think it was creating and deleting as i click 'm'. Is there a way of creating it whilst a button (e.g. 'm') is being held down and then removing it after the key is let go?
Please show what you tried.
TobyRoby TobyRoby

2019/1/23

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

public class MyWorld extends World
{

    instructionsPage instructionsPage = new instructionsPage();
    
    Dip[] dipArray = new Dip[14];
    
    int playerTurn = 1;
    
    public MyWorld()
    {    
        super(700, 400, 1); 
        
        int totalPebble = 0;
        
        mancalaBoard mancalaBoard = new mancalaBoard();
        addObject(mancalaBoard,350,200);
        
        resetButton resetButton = new resetButton();
        addObject(resetButton,660,360);
        
        playerTurn playerTurn = new playerTurn();
        addObject(playerTurn,350,50);
        
        
        dipArray[0]=new Dip();  //these make the dips in the array positions and
        addObject(dipArray[0],208,259);     //decide their locations on the board
        dipArray[1]=new Dip();
        addObject(dipArray[1],269,263);
        dipArray[2]=new Dip();
        addObject(dipArray[2],328,259);
        dipArray[3]=new Dip();
        addObject(dipArray[3],388,260);
        dipArray[4]=new Dip();
        addObject(dipArray[4],441,260);
        dipArray[5]=new Dip();
        addObject(dipArray[5],506,262);
        dipArray[6]=new Dip();
        addObject(dipArray[6],554,189);
        dipArray[7]=new Dip();
        addObject(dipArray[7],504,153);
        dipArray[8]=new Dip();
        addObject(dipArray[8],444,157);
        dipArray[9]=new Dip();
        addObject(dipArray[9],381,151);
        dipArray[10]=new Dip();
        addObject(dipArray[10],322,155);
        dipArray[11]=new Dip();
        addObject(dipArray[11],262,153);
        dipArray[12]=new Dip();
        addObject(dipArray[12],204,153);
        dipArray[13]=new Dip();
        addObject(dipArray[13],147,197);
        
        
        
        for ( int i=0; i<dipArray.length; i++)
        {
            dipArray[i].totalPebble=4;  //this for loop makes all of the dips have a pebble value of 4
        }

        dipArray[6].totalPebble=0;  //these two lines change th value of the 'mancalas' so that they start with a value of 0
        dipArray[13].totalPebble=0;
    }
    
    public void act()   //this function is executed repeatedly during run-time
    {
        int x = 0;
        int y = 0;
        
        if ("m".equals(Greenfoot.getKey()))
        {
            addObject(instructionsPage,350,150);
            removeObject(instructionsPage);
        }
i tried this but it creates the text page but doesnt remove it afterwards
danpost danpost

2019/1/23

#
TobyRoby wrote...
<< Code Omitted >> i tried this but it creates the text page but doesnt remove it afterwards
TobyRoby wrote...
what do you mean by see my edited post above? also i did what you said above and it didnt do anything,
Apparently you did not do what I said above. You still are using getKey and the removal line is still in the same place.
TobyRoby TobyRoby

2019/1/24

#
oh that worked thank you, i dont think i saw the bit where you said about the else statement. here is the code if anyone in the future wants to know
if (Greenfoot.isKeyDown("m"))
        {
            addObject(instructionsPage,350,150);
        }
        else
        {
            removeObject(instructionsPage);
        }
You need to login to post a reply.