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

2016/5/12

Round Display

StoveAteMe StoveAteMe

2016/5/12

#
Hey, so I wanted to display text when a new round starts, and what round it is. I'm using both my myWorld class, and a Text class that extends from actor. Here is the myWorld code:
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    int round = 1;  //the variable to indicate the round
    public boolean round2 = false;  //used to initiate round 2 sequence
    public boolean round3 = false;  //used to initiate round 3 sequence
  
   public MyWorld()
    {    
        super(600, 400, 1); 
        addObject(new Savior(), 300, 350);  
        firstWave();
        setBackground(new GreenfootImage("flag.jpg"));
        Text myText = new Text();
        addObject(myText, 300, 200);
       
    }
   
      public void act(){
            scanPause();
            secondWave();
            finalWave();
            Text myText;
            myText.displayRound();
            myText.displayRound2();
            myText.displayFinalRound();
            }
    
         //MY WORLD METHODS
           
            /** Precondition: Alien objects exist in world
             *  Postcondition: No Alien Objects present, Round increases, game pauses
             */  
            public void scanPause() {  //determines if alien objects exist, if not, increase round, and pause game
                  if(getObjects(Aliens.class).size() < 1) {
                  round++;
                  Greenfoot.stop();
       
               }
            }
            
            /** Precondition: It is round 1
             *  Postcondition: spawns a 5x5 area of Alien objects in round 1  
             */
            public void firstWave() {  //spawns first wave of enemies
               if(round == 1) { 
                  for (int i = 0; i < 5; i++) {
                     for (int j = 0; j < 5; j++) {
                         addObject(new Aliens(Greenfoot.getRandomNumber(5)), 25 + (j * 35), 30 + (i * 35));
            
                     }
                  }
               }
            }
            
            /** Precondition: It is round 2
             *  Postcondition: spawns a 6x6 area of Alien objects in round 2
             */
            public void secondWave() {  //spawns second wave of enemies
               if(round == 2) { 
                    if(round2) {}
                       else {
                         for (int i = 0; i < 6; i++) {
                            for (int j = 0; j < 6; j++) {
                               addObject(new Aliens(Greenfoot.getRandomNumber(5)),25 + (j * 35), 30 + (i * 35));
                               round2 = true; 
                            }
                         }
                    }
               }
            }
    
            /** Precondition: It is round 3, the final round
             *  Postcondition: Spawns a single Boss object, with a 5x5 area of Alien objects underneath it
             */
            public void finalWave() {  //spawns enemies for final boss
                if(round == 3) {
                     if(round3) {}
                        else {
                          for(int i = 0; i < 5; i++) {
                             for(int j = 0; j < 5; j++){
                                addObject(new Aliens(Greenfoot.getRandomNumber(5)), 200 + (j * 50), 40 + (i * 20));
                                round3 = true; 
                             }
                          }
                          addObject(new Boss(), 300, 100);
                     }
                }
            }
         
         
     }
And my Text code:
public class Text extends Actor
{
    /**
     * Act - do whatever the Text wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public Text() {
      this("");

    }
    
    public Text(String text)  
    {  
      setText(text);
    }  
    int round = 2;
    public boolean roundCheck = false;
    public boolean roundCheck2 = false;
    public boolean roundCheck3 = false;
    
        public void act() 
         {
            scanner();
         }    
    
          public void setText(String text) {
          setImage(new GreenfootImage(text, 30, Color.BLACK, new Color(0, 0, 0, 0)));
        }
   
        public void scanner() {
           if(getWorld().getObjects(Aliens.class).size() < 1) {
             round++;
          }
        }
    
        public void displayRound() {
           if(round == 1){   
               if(roundCheck){}
                  else{    
            Text roundOne = new Text();
            roundOne.setText("Round 1");
            getWorld().addObject(roundOne, 300, 200);
            roundCheck = true;
            Greenfoot.delay(50);
            getWorld().removeObject(roundOne);
          }
        }
    }
            
    public void displayRound2() {
      if(round == 2) {
          if(roundCheck2){}
              else{   
            Text myText = new Text();
            myText.setText("Round 2");        
            getWorld().addObject(myText, 300, 200);
            roundCheck2 = true;
            Greenfoot.delay(50);
            getWorld().removeObject(myText);
          }
        }
    }
    
    public void displayFinalRound() {
      if(round == 3) {  
          if(roundCheck3){}
              else{     
            Text roundFinal = new Text();
            roundFinal.setText("Final Round");      
            getWorld().addObject(roundFinal, 300, 200);
            roundCheck3 = true;
            Greenfoot.delay(50);
            getWorld().removeObject(roundFinal);
          }
        }
    }
}
Now the issue is, on the myWorld class in my act method, I'm getting an error I don't understand.
 myText.displayRound();
            myText.displayRound2();
            myText.displayFinalRound();
Basically the error is on myText, and it says: "myText might not have been initialized" How do I work around this error and get my desired results.
danpost danpost

2016/5/12

#
(1) You cannot call methods on a reference variable that has not been assigned an object to reference; line 27 of your MyWorld class declares a new object reference variable of type Text called 'myText' which by default has a 'null' value (it does not yet reference any object; then, the following lines (28 and 29) try to call methods on this variable which does not refer to any Text object to work with. (2) you have 'round' fields declared in both the MyWorld class and the Text class; it is usually not wise to have two different fields to attempt to hold the same information; it is too easy to not keep one up to date and cause erroneous behavior; (3) it is also never wise to have a class create objects of itself in non-static methods unless there is a reproduction at work; (4) it is best to code what needs to be done at the first moment that it can be done; putting in boolean fields to flag an action needs to be done just adds stuff to your code that is not needed; for example, you have three methods 'displayRound' through displayFinalRound' with Boolean fields in both the world class and the text class; these methods are all called every act cycle by the world class act method where you only need to call them (or actually only one of them) once at the time your 'scanner' method increments the round. The MyWorld class would be the place fore the scanner method with a call to it from the act method of the class. It could then look like this:
public void scanner() {
    if(getWorld().getObjects(Aliens.class).size() < 1) {
        round++; // at this point, we already know we are between rounds
        Text text = new Text("");
        if (round == 1) text.setText("Round 1");
        else if (round == 2) text.setText("Round 2");
        else if (round == 3) text.setText("Final Round");
        else text.setText("Game Over");
        addObject(text, 300, 200);
        Greenfoot.delay(50);
        removeObject(text);
        if (round == 1) firstWave();
        else if (round == 2) secondWave();
        else if (round == 3) finalWave();
        else Greenfoot.stop();
    }
}
Remove everything from line 30 on to the end in the Text class and remove the 'scanPause' method in the MyWorld class. All the fields in the Text class can also be removed along with the act method. The act method of the MyWorld class can contain just the one line calling the 'scanner' method above.
You need to login to post a reply.