i am trying to get better with greenfoot and was wondering if anyone could help me understand booleans and loops? any help is appreciated.
boolean aBoolean = true; // this one is true boolean anotherBoolean = false; // this one is false
if (aBoolean == true) { // if the aBoolean is true,
// execute this
} else if (anotherBoolean == false) { // is the anotherBoolean is false,
// execute this
}if (aBoolean) { // if the aBoolean is true,
// execute this
} else if (!anotherBoolean) { // is the anotherBoolean is false,
// execute this
}for (int i = 0; i < 10; i++) {
// execute this 10 (limit is 10) times
}i < 10;
while (*some condition*) {
// execute this until condition is false
}
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;
}