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

2012/8/3

Boolean & loop help

CrazyGamer1122 CrazyGamer1122

2012/8/3

#
i am trying to get better with greenfoot and was wondering if anyone could help me understand booleans and loops? any help is appreciated.
SPower SPower

2012/8/3

#
A boolean is a datatype which can be true or false. Some examples:
boolean aBoolean = true; // this one is true
boolean anotherBoolean = false; // this one is false
You can check if they're true or false doing this:
if (aBoolean == true) { // if the aBoolean is true,
// execute this
} else if (anotherBoolean == false) { // is the anotherBoolean is false,
// execute this
}
but you can also do this:
if (aBoolean) { // if the aBoolean is true,
// execute this
} else if (!anotherBoolean) { // is the anotherBoolean is false,
// execute this
}
that has the same result, it has just a bit less code. Loops are statements which execute code multiple times, you say how much. You've got 2 important loops: a for-loop and a while-loop. This is a for loop:
for (int i = 0; i < 10; i++) {
// execute this 10 (limit is 10) times
}
what a for loop does is it has an int (usually called 'i'), and it checks if it's smaller than some limit. In this case, our limit is 10, see this part:
i < 10;
If so, it executes the code between the {} A while loop is this:
while (*some condition*) {
 // execute this until condition is false
}
what it does, is it runs the code between the {}, until the condition between the () isn't true (bit like an if-statement). Do you understand? If you need more help, just ask me.
danpost danpost

2012/8/3

#
There is an abundance of information contained in The Java Tutorials. The second section on the left on the Home page, called 'Trails Covering the Basics' has most of the information you might need. The second item, 'Learning the Java Language' has the information on 'Variables' which would include the boolean type, as well as information on 'Control Flow Statements' which has what you would need on the 'for' statement. I found these tutorials to be of a substantial help; so much, I included the 'Home page' in my 'Favourite's bar.
CrazyGamer1122 CrazyGamer1122

2012/8/3

#
thank you. i learned a lot from this. by the way.... would a code like this work for a actor health meter? kinda long but please check it out.
import java.awt.Color;
private int life = 3

private void move()
{    
   if (Greenfoot.isKeyDown("right"))
   {
        move (3);
    }
   if (Greenfoot.isKeyDown("left")) 
   
   {
       move (-3)
   }
    jump()   
}
     

private void Alive()
    move();

private void Dead()
{     
    setImage(new GreenfootImage("You Lose",130, Color.RED, Color.BLACK));
    

if life > 0
{ 
 Alive()
}
else
{
  Dead()
}




public void jump()
    
    {  
        
        
        
        checkFall();
  
  if (onGround() && Greenfoot.isKeyDown("up")) 
        { 
            jump(); 
            checkFall();

        }
    }  

    private void checkFall()  
    {  
        if(!onGround() || gravity < 0)  
            fall();  
    }  

    private boolean onGround()  
    {  
        int myHeight = getImage().getHeight();  
        Actor ground = getOneObjectAtOffset(0,    myHeight/2, Ground.class);    
        if(ground != null)//if the ground is really there...  
            return true;  
        //(if not, return false)  
        return false;  
    }  
     
    private void fall()  
    {  
        setLocation(getX(), getY() + gravity);  
        gravity++;//the player will accelerate as he falls

    }  

    private void jump()  
    {  

        gravity -= 15;
        
    }  

danpost danpost

2012/8/3

#
I think your Alive() and Dead() methods are missing some brackets, and lines 27 through 34 are out there in NoWhereLand (probably should be in the act() method). As far as the workings of a health meter, there is nothing reducing the health at the moment.
CrazyGamer1122 CrazyGamer1122

2012/8/3

#
yeah i compiled this all into greenfoot and realized how bad it was. sorry about that, i was free-handing it. im working on a rocket game where the other rockets shoot back at random intervals (hence, the health meter). i understand the basics of boolean at least.
You need to login to post a reply.