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

2013/1/26

Get the World which is currently active

Super_Hippo Super_Hippo

2013/1/26

#
Hi, I am trying to program a Bomberman game with several levels (worlds). The idea is that when your actor dies, the level in which he is right now, starts new. How can I check, which world is active right now and reset this world?
if ( getWorld() == Level1.class )
            {
                Greenfoot.setWorld(new Level1());
            }
incomparable types: greenfoot.World and java.Class<Level1>
danpost danpost

2013/1/26

#
I believe that either of the following would work:
if (getWorld() instanceof Level1)
// or
if (Level1.class.isInstance(getWorld())) 
Super_Hippo Super_Hippo

2013/1/26

#
Thank you, both works. But now, i have another problem... Is it possible to keep the score and number of lives after resetting a world? If yes, how?
danpost danpost

2013/1/26

#
Again, there are at least two ways to pass the data to the new (or reset) world. One way is passing the data in a constructor of the world:
// sample constructor 
public Level1(int score, int lives)
{
    this();
    scoreCounter.setScore(score);
    livesCounter.setLives(lives); 
}
// using constructor call
Greenfoot.setworld(new Level1(score, lives));
The other way is to pass the data after construction of the new (or reset) world:
// sample code
Level1 level=new Level1();
level.getScoreCounter().setScore(score);
level.getLivesCounter().setLives(lives);
Greenfoot.setWorld(level);
Super_Hippo Super_Hippo

2013/1/26

#
The first one gives the error:
non-static method setScore(int) cannot be referenced from a static context
Same with getLives(int). The second one gives the same error after I changed it to the following, because it did not know the variables score/lives:
if (getWorld() instanceof Level1)
{
    Level1 level=new Level1();
    level.getScoreCounter().setScore(ScoreCounter.getScore());
    level.getLivesCounter().setLives(LivesCounter.getLives());
    Greenfoot.setWorld(level);
}
Sorry, but what am I doing wrong here?
danpost danpost

2013/1/26

#
I said that that was 'sample code'. I do not know the actual names of your variables, how you have them referenced, or what methods of access you have used in your scenario. The main steps are illustrated, but the code will need adjustment to suite your scenario. BTW, livesCounter and scoreCounter are NOT the names of the classes, but the names of the fields that hold objects of the Counter class (if that is what you named it).
danpost danpost

2013/1/26

#
Maybe I should have written the following:
// sample code
Level1 level=new Level1();
level.getScoreCounter().setScore(score);
level.getLivesCounter().setLives(lives);
Greenfoot.setWorld(level);
like this (which probably makes more sense):
// sample code
Level1 level=new Level1();
level.getScoreCounter().setScore(getScoreCounter().getScore());
level.getLivesCounter().setLives(getLivesCounter().getLives());
Greenfoot.setWorld(level);
Super_Hippo Super_Hippo

2013/1/26

#
danpost wrote...
I said that that was 'sample code'. I do not know the actual names of your variables, how you have them referenced, or what methods of access you have used in your scenario. The main steps are illustrated, but the code will need adjustment to suite your scenario.
I imported the Counter class and changed it to what you named it. Now there is the
getScoreCounter().
part twice in this line. But both do not work.
Cannot find symbol - method getScoreCounter().
level.getScoreCounter().setScore(getScoreCounter().getScore());  
I don't understand how to use a method of a certain class in another class. For me, it only seems to work when i use getWorld(). , get<OtherClass>(). never works. In my first game, i made all classes as subclasses of others which was very confusing, so i wanted to change it in this game.
danpost wrote...
BTW, livesCounter and scoreCounter are NOT the names of the classes, but the names of the fields that hold objects of the Counter class (if that is what you named it).
Okay, I do not get that. I think I did not name any "fields that hold objects of the Counter class". Or I just do not know what you mean again... It already starts that I do not know what line 4 does.
this();
Super_Hippo Super_Hippo

2013/1/26

#
I use them as classes now because I gave them a capital letter:
public Level1(int score, int lives)
    {
        this();
        ScoreCounter.setScore(score);
        LivesCounter.setLives(lives); 
    }
and
if (getWorld() instanceof Level1)  
            {
                LivesCounter.value--;               
                Greenfoot.setWorld(new Level1(ScoreCounter.value, LivesCounter.value));                           
            }
It works now. At least for the Lives, the score function is not included yet. Thank you very much for your help! :)
danpost danpost

2013/1/26

#
The line 'this();' calls and executes your constructor that begins with 'public Level1()'. The 'getScoreCounter()' and 'getLivesCounter()' are methods you might need to write in your world class to get access to those objects (I did say objects; not classes). Since you have two Counter objects in your world, we must keep references to them, to be able to tell one apart from the other. You will have to do the following when using the first method:
// add instance fields in the world class
Counter livesCounter=new Counter('Lives: ", 3);
Counter scoreCounter=new Counter("Score: ", 0);
// add the following methods in the world class
public Counter getLivesCounter()
{
    return livesCounter;
}
// and
public Counter getScoreCounter()
{
    return scoreCounter;
}
The first two lines should be placed within the class code but outside any methods. Class and instance object fields are usually placed near the top of the class code. Sample class example for the alternate method follows, while illustrating the class structure:
// import
import greenfoot.*;
// class declaration
public class ClassName extends SuperClassName
{
    // class fields and instance object fields
    Counter livesCounter, scoreCounter;
// constructors
    public ClassName()
    {
        super(600, 400, 1);
        scoreCounter=new Counter("Score: ", 0);
        livesCounter=new Counter("Lives: ", 3);
        // prepare world (add objects, assign values to fields, etc.
    }

    public ClassName(int score, int lives)
    {
        this(); // calls the previous constructor
        scoreCounter.setValue(score);
        livesCounter.setValue(lives);
    }
// any methods for this class go here
}
You can assign the value of the fields in the constructor, as shown in the example, or in the field declarations themselves, as exampled previously. When using the dual constructors, you will not need the 'getScoreCounter()' and 'getLivesCounter()' methods, because you will be setting their values from within the same class as their references (the fields 'scoreCounter' and 'livesCounter') are located.
You need to login to post a reply.