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

2014/10/1

How to make a message box?

kangmj kangmj

2014/10/1

#
Hi so message box "error" should appear when player does wrong moves How to make it?
Super_Hippo Super_Hippo

2014/10/1

#
Create an actor which has the error message as its image. To create and use such an image, you can use the following in the constructor of this class:
1
setImage(new GreenfootImage("error", 15, java.awt.Color.WHITE, java.awt.Color.BLACK));
kangmj kangmj

2014/10/1

#
Super_Hippo wrote...
Create an actor which has the error message as its image. To create and use such an image, you can use the following in the constructor of this class:
1
setImage(new GreenfootImage("error", 15, java.awt.Color.WHITE, java.awt.Color.BLACK));
is there an easier way? I just don't want to create a new class for it
danpost danpost

2014/10/1

#
There is a way to create an Actor object that displays a message without creating a new subclass of Actor. However, which ever way you go, you will have to deal with how to remove the actor after it is displayed (unless this will end that simulation). When using a class for it, you can check for the existence of an instance of that class in the world with 'getObjects(ClassName.class).isEmpty()'. This will work provided you have only one instance of the class present at a time. Any other situation (more than one instance or not using a separate subclass of Actor) will require a field be set up to hold a reference to the message actor, setting it to the actor when created and added to the world and setting it to 'null' when the message actor is removed from the world. If you do decide to go without creating a separate class:
1
2
3
4
5
// add instance field
Actor messageActor = new Actor(){};
// when adding message to world
messageActor.setImage(new GreenfootImage(...));
/*  getWorld().  */ addObject(messageActor, x, y);
BTW, behind the scenes, java will still create a class for this actor If you should be able to remove the message and proceed playing, then you should have something like the following in you act method (this will remove the actor when clicked on; you may want to detect some other action or state for removing the actor):
1
2
3
4
if (messageActor.getWorld() != null && Greenfoot.mouseClicked(messageActor))
{
    removeObject(messageActor);
}
The conditions in line 1 of the last snippet can be split to perform other duties if the message actor is NOT in the world (for normal play code).
You need to login to post a reply.