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

2013/11/16

Is there any way to check if there are no keys pressed?

soyazhe soyazhe

2013/11/16

#
Hi, I'm currently making a simple greenfoot game about a guy climbing Mount Everest. I want it so that whenever a key isn't pressed, or there is no user input, the climber's body temperature will start decreasing. This is what I have, but it won't work..
public climber()
    {
        backpackFound = 0; 
        bodyTemp = 30;
        
        setDirection (EAST);
        
    }
    
    public void act() 
    { 
                                             
        if (Greenfoot.isKeyDown("right")) 
        {
            if (direction != EAST) 
            {
                setDirection(EAST);
            }
            
            if( canMove()) 
            {
                move(1);
            }
            keyPressed = true;
            
        }
       
        
        else if (Greenfoot.isKeyDown("left")) 
        {
            if (direction != WEST) 
            {
                setDirection(WEST);
            }
            if( canMove()) 
            {
                move(1);
            }
            keyPressed = true;
        }
       
        
        else if (Greenfoot.isKeyDown("up")) 
        {
            if (direction != NORTH) 
            {
                setDirection(NORTH);
            } 
            if( canMove()) 
            {
                move(1);
            }
            keyPressed = true;
        }
       
        
        else if (Greenfoot.isKeyDown("down")) 
        {
            if (direction != SOUTH) 
            {
                setDirection(SOUTH);
            } 
            if( canMove()) 
            {
                move(1);
            }
            keyPressed = true;
        }
        
        else
        {
            keyPressed = false;
        }

        if (keyPressed = false)
        {
            bodyTemp -= 10;
        }
Any help at all would be greatly appreciated. Thank you :)
danpost danpost

2013/11/16

#
In line 75, you are using a single equal sign (which is used for field assignment) instead of a double equal sign (which is used for conditional equality).
soyazhe soyazhe

2013/11/16

#
Thanks, but that doesn't fix the problem I have. I have a counter for bodyTemp displaying on the screen and when I'm not pressing any buttons it should be decrementing but nothing happens. :/
public level1()
    { 
       
        super(90, 90, 5); 
        setPaintOrder(ScoreBoard.class, Counter.class, climber.class, rock.class, backpack.class);
        populate();
        

        addObject(tempText, 70, 14);
        tempText.setText("Temperature: " + (climber.bodyTemp) + "degrees");
    }
Should I be calling climber.bodyTemp another way?
danpost danpost

2013/11/16

#
The code given above for Level1 is the Level1 constructor code. It is only called one time for each Level1 world creation. If you want the text of the 'tempText' object to be updated continuously, line 10 needs to be moved to the world 'act' method (if you do not have one, it will need to be added).
You need to login to post a reply.