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

2016/3/31

Making a message appear and disapear

MrSandwich MrSandwich

2016/3/31

#
So I'm trying to make a message appear at the begining of every level ( here level counts, as having x score not by changing worlds), and I'm having no luck. What I'm doing is the following: 1) Creating a messageBox class with the following code:
import greenfoot.*;
import java.awt.Color;

public class MessageBox extends Actor
{
 
    GreenfootImage level1 = new GreenfootImage("Level 1", 12, Color.WHITE, Color.BLACK);
    GreenfootImage level2 = new GreenfootImage("Level 2", 12, Color.WHITE, Color.BLACK);
    GreenfootImage level3 = new GreenfootImage("Level 3", 12, Color.WHITE, Color.BLACK);
    public Nivel()
    {
       setImage(level1); // just calling this because I was having problems with lag on the game
       
    }
   
    public void act() 
    {
      if(enemies.score == 4) // the score here is declared in the enemies Class as a public static int so other classes can see it
      {
          setImage(level2);
          
        }
      else if(enemies.score == 6)
      {
         setImage(level3); 
        }
   
}
}
2) Creating the object on the enemies class (where the score counter is), or maybe I should be calling it on the world class?
public void level()
{
    MessageBox level = new MessageBox();
    
   getWorld().addObject(level, getWorld().getWidth()/2, 800);
   if(timer > 5) { getWorld().removeObject(level);} // the timer is here so the message box can disapear after a certain period of time
       
}
What am I doing wrong here?
fejfo fejfo

2016/3/31

#
first of all I don't know where you keep your reference enemies is MessageBox except if enemies is the class in with case timer should be static and why is the the constructor of MessageBox called Nivel ? and why aren't you using the built in Label class (you have to import it first (edit --> import class...))
MrSandwich MrSandwich

2016/3/31

#
The constructor is called Nivel because i forgot to change it (I'm writing the program ( the variables and classes names) in another language so that slipped). The enemies is indeed the class, but my thought of process was just to put a simple integer that keeps on incremeting on the act and when it surpasses 5 the message would then disapear ( sort of like a introduction to the level, but I was trying to make it go away ). I'll look into the label class, didn't even know there was one. I'll report here any progress I made.
You need to login to post a reply.