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:
And my Text code:
Now the issue is, on the myWorld class in my act method, I'm getting an error I don't understand.
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.
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);
}
}
}
}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);
}
}
}
} myText.displayRound();
myText.displayRound2();
myText.displayFinalRound();
