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

2015/12/13

How to get a variable from an actor class to a world class

Brookili Brookili

2015/12/13

#
I need some help to make it so that when the actor leaves a village, he spawns in the village entrance, rather than spawning all the way at the beginning. This is the game.
Super_Hippo Super_Hippo

2015/12/13

#
Well, I get an 'UnsupportedClassVersionError' when trying to open your scenario. I guess this could be because of something which Java 7 doesn't support. Anyways, how is the topic name related to the text you have written? Do you have different subclasses of world? So for example 'Village' and 'Outside' (I know, very creative but you know what I mean^^). Then, you somewhere have this piece of code when leaving the village with the actor:
Greenfoot.setWorld(new Outside());
Instead, you could pass a parameter. For example an int. So, let's say, if the actor can move to this world from four sides, 0 could represent it coming from the top, 1 from the right, 2 from the bottom and 3 from left. Then your code in the Outside class could look like this:
public Outside()
{
    //whatever you have here without spawning the main actor
}

public Outside(final int pos)
{
    this();
    switch(pos)
    {
        case 0: addObject(new MainActor(), getWidth()/2, 10); break;
        case 1: addObject(new MainActor(), getWidth()-10,  getHeight()/2); break;
        case 2: addObject(new MainActor(), getWidth()/2, getHeight()-10); break;
        case 3: addObject(new MainActor(), 10,  getHeight()/2); break;
    }
}
When changing the world, it would be like this (the 0 has to be replaced with the correct number depending on where the actor should spawn).
Greenfoot.setWorld(new Outside(0));
The constructor without parameters is only needed if you want to create it as a first world. If not, you can place everything from there at the beginning of the other constructor instead of the 'this();'.
Brookili Brookili

2015/12/13

#
So I used the code that you wrote, and it works, but how do I make it set to case 0 at the beginning of the game?
danpost danpost

2015/12/13

#
You should have the main constructor (lines 1 through 4) calling the other one (lines 6 through 16):
public Outside()
{
    this(0);
}

public Outside(int pos)
{
    // whatever code you HAD in the main constructor
    // plus the spawning code for the main actor
}
Brookili Brookili

2015/12/14

#
I tried this and it worked, thank you so much!
You need to login to post a reply.