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

2014/10/17

making a working subclass of world (which is subclass of World)

1
2
Alwin_Gerrits Alwin_Gerrits

2014/10/17

#
Hey guys, I'm trying to get something done and it simply won't work. Could you guys help out? What I did is I made a subclass of 'World' called 'world' and from that I created a subclass called 'menu'. Obviously not the brightest names, but that aside I'm having some trouble getting this to work properly. In 'world' I do a couple of things, but mainly I specify the worlds size with 'super(1000, 550, 1);' and place my player in the world. Now so far everything works, but then I made a subclass of 'world' (watch the downcase) called 'menu' and I try to add some blocks there effectively making separate levels. Guess something is going horribly wrong though because it runs everything perfectly exept this subclass of the subclass. It doesn't even seem to want to touch the code... Any leads on a possible fix?
Super_Hippo Super_Hippo

2014/10/18

#
Did you set this world active? If the 'world' world is active, then the 'menu' world isn't. Btw, class names should always start with a capital letter.
Alwin_Gerrits Alwin_Gerrits

2014/10/18

#
I see, is there a way to make the 'menu' world into the active world then? If possible even do so from the class 'world'?
Alwin_Gerrits Alwin_Gerrits

2014/10/18

#
By the way, thanks for helping out on the other fora. I'm still pretty new and trying to help people that understand even less then I do, but having a second person correct me when I'm wrong or giving a different solution also helps me out a bit :).
Alwin_Gerrits Alwin_Gerrits

2014/10/18

#
Nvm actually, I want to find the solution of switching worlds myself. But if I really can't find how it works you can be sure to hear from me again in the near future ^^.
Super_Hippo Super_Hippo

2014/10/18

#
Right-click on the class and then 'new Menu()' (yes, change 'menu' to 'Menu'). To change the world after some action, you can use the following:
1
Greenfoot.setWorld(new Menu());
Edit: Well, then ignore this. :P
Alwin_Gerrits Alwin_Gerrits

2014/10/18

#
Found it myself before I read this ^^. Ty though
Alwin_Gerrits Alwin_Gerrits

2014/10/18

#
Hmm I seem to still screw up on something.... I made a subclass of 'World' called 'LevelPicker' and then made 2 subclasses of 'LevelPicker' called 'Level1' and 'Level2'. However using Greenfoot.setWorld(new Level2()); seems to pick 'LevelPicker' instead of 'Level2'. Now i'm guessing this has something to do with the fact that I'm using setWorld which probably only lets me chose 'LevelPicker' as a world. Is there some way to make it go to either one of the levels instead?
davmac davmac

2014/10/18

#
However using Greenfoot.setWorld(new Level2()); seems to pick 'LevelPicker' instead of 'Level2'.
No, it could not do that.
Now i'm guessing this has something to do with the fact that I'm using setWorld which probably only lets me chose 'LevelPicker' as a world.
The purpose of setWorld is to let you specify whatever world you want to set. setWorld doesn't know anything anout LevelPicker; LevelPicker is only in your own scenario. There's probably something wrong with the logic of your program - it's not doing what you think it is.
Alwin_Gerrits Alwin_Gerrits

2014/10/18

#
I see... Then making 'Level1' a direct subclass of World does seem to work, but it doesn't seem to build the world correctly..... Here's my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void chose()
    {
        String[] options = new String[] {"level 1", "level 2", "level3", "level4", "test", "example", "Quit"};
             
        response = JOptionPane.showOptionDialog(null, "Choose an option:", "Main Menu", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
         
        if(response == 0)
        {Greenfoot.setWorld(new Level1());}
         
        else if(response == 1)
        {Greenfoot.setWorld(new Level2());}
         
        else if(response == 2)
        {level3();}
         
        else if(response == 3)
        {level4();}
    }
Now making the initial choice there are no problems whatsoever, however once I call for this function later from 'player' (subclass of actor) it can't chose the other subclasses of World, only the ones I build within 'LevelPicker' (so level 3 and 4)('LevelPicker' is a first level subclass of 'World', the code above is in this subclass). I left some code away because it's not interesting right now (they were some options of the menu). Anyway, got any leads why this isn't working? Just giving me a hint should be enough, I'm trying to make a 'large' platform game for a school project and they sent me over here. Though I'm trying to find out as much as I can by myself, but I have a bad habit of trying the impossible apparently XD. Anyway, thanks already this stuff is really helping me out and most things I hear here i didn't find in the tutorial nor in the Greenfoot book :).
Super_Hippo Super_Hippo

2014/10/18

#
So the method only works right at the beginning and doesn't work correctly when it is called later? This actually can't be. Which worlds work and which don't? Are the constructors of the worlds somehow different and might cause this? What are the methods 'level3' and 'level4' doing? (I am not really familiar with the JOptionPane, so I don't know if something is wrong with it.)
Alwin_Gerrits Alwin_Gerrits

2014/10/18

#
Maybe this will make my problem a little bit better to understand:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class LevelPicker extends World
{   
    public static final int DIFF = 40; //Difference
 
    int response;
     
    public LevelPicker()
    {
        super(1000, 550, 1); //Create a new world with 1000x550 cells with a cell size of 1x1 pixels.
 
        addObject(new Player(), 200, 525);
         
        chose();
    }
     
    public void chose()
    {
        String[] options = new String[] {"level 1", "level 2", "level3", "level4", "test", "example", "Quit"};
             
        response = JOptionPane.showOptionDialog(null, "Choose an option:", "Main Menu", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
         
        if(response == 0)
        {Greenfoot.setWorld(new Level1());}
         
        else if(response == 1)
        {Greenfoot.setWorld(new Level2());}
         
        else if(response == 2)
        {level3();}
         
        else if(response == 3)
        {level4();}
         
        else if(response == 4)
        {test();}
         
        else if(response == 5)
        {example();}
    }
Above you can see the entire code of 'LevelPicker'. When the world is build 'JOptoinPane' creates a window with a few options. In this case I made one with the options as displayed within 'String options'. So it's gonna make a screen pop up with a few options which include: "level 1", "level 2", "level3", "level4", "test", "example", "Quit". JOptionPane returns a value in the form of an integer depending on the made choise. Each option simply is 1 higher. (So level 1 would return 0 as where level 2 would return 1 and so on..). Taking these responds I determine what level to load. Now the 'level 1' and 'level 2' are actually subclasses of 'World', but the rest (including 'level 3' and 'level 4') are operations more down in the code above. In other words they are part of 'LevelPicker' (which just to be clear is allso a subclass of World). So now again my problem: When first compiling everything works fine and the Greenfoot.setWorld command works fine to. HOWEVER... after it picks a world to make there is an option within my game to bring up a pause screen. From the pause screen you can go to the 'LevelPicker' using the following command:
1
Greenfoot.setWorld(new LevelPicker());
So far so good. Anyway, it does get to the 'LevelPicker' and again it asks to make a choise. Now here comes the problem. When picking any level that's created within 'LevelPicker' ('level 3' and higher) the code works just fine. However, when picking 'level 1' or 'level 2' this doesn't seem to work. Somehow it doesn't seem to call forward the class which would override the empty world already created. However it does get to the Greenfoot.setWorld command because it can call forward the other levels... So yeah... if anybody has a clue on what's going wrong please do tell :)
Super_Hippo Super_Hippo

2014/10/19

#
I don't know if it has something to do with it, but try to remove line 5 and change line 20 to 'int response = ...'. By the way, instead of writing 'if(response == 0)...else if....else if', you can also write:
1
2
3
4
5
6
switch (response)
{
   case 0: Greenfoot.setWorld(new Level1()); break;
   case 1: Greenfoot.setWorld(new Level2()); break;
   //...
}
Alwin_Gerrits Alwin_Gerrits

2014/10/19

#
I tried what you replied... lol, but it didn't work. I'm still having the same problem where picking a world that's a subclass of 'World' like 'Level1' or 'Level2' only works the first time around. I even put in a popup that appears when the levels are called. The popup works just fine, even though it's behind the Greenfoot.setWorld command. So again it is getting into the right part of the loop but somehow the command doesn't seem to respond correctly.
1
2
3
4
5
6
7
8
9
10
switch(response)
        {
            case 0: Greenfoot.setWorld(new Level1()); JOptionPane.showMessageDialog(new JInternalFrame(), "title", "Title", JOptionPane.INFORMATION_MESSAGE); break;
            case 1: Greenfoot.setWorld(new Level2()); JOptionPane.showMessageDialog(new JInternalFrame(), "title", "Title", JOptionPane.INFORMATION_MESSAGE); break;
            case 2: level3();   break;
            case 3: level4();   break;
            case 4: test();     break;
            case 5: example();  break;
            case 6: level1();   break;
        }
The option panel simply has a title that says "titel" and some text in it saying the same thing.
Super_Hippo Super_Hippo

2014/10/19

#
Did you change the 'int response' thing? What is the code in Level1/Level2? Btw, you can also use 'System.out.println("test")' if you want to see, if the code was executed.
There are more replies on the next page.
1
2