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


1 2 | boolean aBoolean = true ; // this one is true boolean anotherBoolean = false ; // this one is false |
1 2 3 4 5 | if (aBoolean == true ) { // if the aBoolean is true, // execute this } else if (anotherBoolean == false ) { // is the anotherBoolean is false, // execute this } |
1 2 3 4 5 | if (aBoolean) { // if the aBoolean is true, // execute this } else if (!anotherBoolean) { // is the anotherBoolean is false, // execute this } |
1 2 3 | for ( int i = 0 ; i < 10 ; i++) { // execute this 10 (limit is 10) times } |
1 | i < 10 ; |
1 2 3 | while (*some condition*) { // execute this until condition is false } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | 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 ; } |