PDA

View Full Version : Changing classes that aren't extended into current class (MADE WITH GREENFOOT)



motormichael12
08-23-2008, 03:11 AM
I have classes that are categorized like this:

World
|-MainScreen
|-Active
Actor
|-Message

I have MainScreen as a way to choose which level you want. It makes text like this:


public class MainScreen extends World
{

private Message game1 = null;
private Message game2 = null;
private Message game3 = null;
private Message game4 = null;

public MainScreen()
{

super(800,600,1);

game1 = new Message("World 1",1);
addObject(game1,400, 50);

game2 = new Message("World 2",2);
addObject(game2,400, 100);

game3 = new Message("World 3",3);
addObject(game3,400, 150);

game4 = new Message("World 4",4);
addObject(game4,400, 200);
}
}

The message code is as follows:


public class Message extends Actor
{
/////////////// fieldds ////////////////////

/** holds the text of the message */
private String text;

/** the width of the message area */
private int width = 408;

/** the height of the message area */
private int height = 70;

private int value = 1;

///////////// Conbtructors /////////////////

public Message(String theText, int which)
{
text = theText;
updateImage();
value = which;
}

public Message(String theText, int theWidth, int theHeight)
{
text = theText;
width = theWidth;
height = theHeight;
updateImage();
}

////////////////// methods ///////////////////////
// private World world;
public void act()
{
if(Greenfoot.mouseClicked(this)) {
//System.out.println(value);
//World world = (Active) world;
}
}

public void setText(String theText)
{
text = theText;
updateImage();
}

private void updateImage()
{
Font font = new Font("Helvetica",Font.BOLD,20);
GreenfootImage image = new GreenfootImage(width,height);
image.setFont(font);
image.clear();
image.setColor(Color.BLACK);
image.drawString(text, 5, height / 2);
setImage(image);
}
}

What I need to happen is that, when one of the generated text labels is clicked, it will open class Active and pass the information along so that it can load the right world.

Is there any way to open an external class like that, or can I only open other classes within the Actors class from the Message class?