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
davmac davmac

2014/10/19

#
Well, for one thing, JOptionPane is a Swing class, and you may only use Swing classes and methods on the Swing/AWT event dispatch thread (EDT). So, you're breaking the Swing threading rules and are potentially going to see some strange behavior because of that. However, I doubt that's what's causing the problem you describe. Anyway:
Above you can see the entire code of 'LevelPicker'.
That's clearly not the entire code of LevelPicker, since I don't see any import statements nor the definitions for methods level3, level4 etc. I think that essentially the problem is not what you think it is. You've got this idea that Greenfoot.setWorld(...) is not working in certain cases, but that's almost certainly not correct. But because you've got this idea, you're only posting snippets of code around your calls to Greenfoot.setWorld(...). You need to post more of the code, I think. Try to get it it down to a small example which still doesn't behave as you expect, and post the entire code for that.
Alwin_Gerrits Alwin_Gerrits

2014/10/19

#
I'm a little bit thick-headed yes... I'll try making a short new code and see if I encounter the same problem then return here with that code... seen my code is 170 rows long right now and i don't want to post that kind of programming here.. hmmm my problem seems to indeed lay elsewhere. Ty for the remark devmac. I tend to be bad at seeing my mistakes if I think I know where the problem is. Even so, a bit of debugging showed my the problem indeed lays elsewhere in the code. Again thanks for that. Need to keep my feet on the ground here :).
Alwin_Gerrits Alwin_Gerrits

2014/10/19

#
K, I think I narrowed it down pretty much to 2 standard commands not working. The problem is indeed within 'Level2' which is a subclass of 'World'. Now 'Level2' is called form 'LevelPicker' which I posted before. Now within level 2 for some reason commands from the 'World' class don't seem to respond. For example... well I'll just post the code as it is now.
public class Level2 extends World
{
    public static final int DIFF = 40; //Difference
    
    public Level2()
    {
        super(1000, 550, 1);
        System.out.println("level2");
        createLevel2();
    }
    
    public void createLevel2()
    {
        addObject(new Speler1(), 200, 525);
        System.out.println("createLevel2()");
        
        setBackground(new GreenfootImage("Jungle-Level.png"));
        
        plateauVer(listBV(0 ), listBH(0),  14);
        plateauVer(listBV(24), listBH(0),  3);
        plateauHor(listBV(12), listBH(1),  7);
        plateauHor(listBV(20), listBH(3),  5);
        plateauHor(listBV(14), listBH(6),  4);
        plateauVer(listBV(24), listBH(6),  8);
        plateauHor(listBV(7 ), listBH(8),  9);
        plateauVer(listBV(6 ), listBH(8),  4);
        plateauVer(listBV(9 ), listBH(9),  2);
        plateauHor(listBV(9 ), listBH(11), 5);
        plateauFakeHor(listBV(7), listBH(11), 2);
    }
    
    public void plateauHor(int x, int level, int width)
    {
        if (width>0)
        {
            int counter;
        
            for(counter=0; counter<width; counter++)
            {addObject(new platform(), x+(DIFF*counter), level);}
        }
    }
    
    public void plateauVer(int x, int level, int height)
    {
        System.out.println("plateauVer");
        if (height>0)
        {
            int counter;
        
            for(counter=0; counter<height; counter++)
            {
                addObject(new platform(), x, level-DIFF*counter);
                System.out.printf("why?");
            }
        }
    }
    
    public void stairsRight(int x, int level, int steps)
    {
        if (steps>0)
        {
            int counter;
            
            for(counter=0; counter<steps; counter++)
            {addObject(new platform(), x+DIFF*counter, level-DIFF*counter);}
        }
    }
    
    public void stairsLeft(int x, int level, int steps)
    {
        if (steps>0)
        {
            int counter;
            
            for(counter=0; counter<steps; counter++)
            {addObject(new platform(), x-DIFF*counter, level-DIFF*counter);}
        }
    }
    
    public void plateauFakeHor(int x, int level, int width)
    {
        if (width>0)
        {
            int counter;
        
            for(counter=0; counter<width; counter++)
            {addObject(new platformFake(), x+(DIFF*counter), level);}
        }
    }
    
    public void plateauFakeVer(int x, int level, int height)
    {
        if (height>0)
        {
            int counter;
        
            for(counter=0; counter<height; counter++)
            {addObject(new platformFake(), x, level-DIFF*counter);}
        }
    }
    
    public void stairsFakeRight(int x, int level, int steps)
    {
        if (steps>0)
        {
            int counter;
            
            for(counter=0; counter<steps; counter++)
            {addObject(new platformFake(), x+DIFF*counter, level-DIFF*counter);}
        }
    }
    
    public void stairsFakeLeft(int x, int level, int steps)
    {
        if (steps>0)
        {
            int counter;
            
            for(counter=0; counter<steps; counter++)
            {addObject(new platformFake(), x-DIFF*counter, level-DIFF*counter);}
        }
    }
    
    public static int listP(int l) //an arraylist of standard heights for player
    {
        ArrayList<Integer> lP = new ArrayList<Integer>(); //lP = levelPlayer
        
        int level;
        int maxLevel=14;
        int start = 530;
        
        for(level=0; level<maxLevel; level++)
        {lP.add(start-DIFF*level);}
        
        int answere = lP.get(l);
        
        lP.clear();
        
        return answere;
    }
    
    public static int listBH(int l)  //an arraylist of standard heights for platforms
    {                               //only use for putting player in the game! not for touching platforms and what not
        ArrayList<Integer> lBH = new ArrayList<Integer>(); //lBH = levelBlockHorizontal
        
        int level;
        int maxLevel=14;
        int start = 535;
        
        for(level=0; level<maxLevel; level++)
        {lBH.add(start-DIFF*level);}
        
        int answere = lBH.get(l);
        
        lBH.clear();
        
        return answere;
    }
    
    public static int listBV(int l)
    {
        ArrayList<Integer> lBV = new ArrayList<Integer>(); //LBV = levelBlockVertical
        
        int level;
        int maxLevel=25;
        int start = 25;
        
        for(level=0; level<maxLevel; level++)
        {lBV.add(start+DIFF*level);}
        
        int answere = lBV.get(l);
        
        lBV.clear();
        
        return answere;
    }
}
somehow setBackground and addObject don't seem to do what they should even do the System.out commands do respond. guess that's where the real problem resides...
Super_Hippo Super_Hippo

2014/10/19

#
Why do you create an array list in these list methods? Wouldn't the following do the same?
public static int listP(int s)
{
    return 530-DIFF*s;
}
Other than that, you don't need to create an extra int for a for-loop. You can just write 'for (int i=0;...)'. And of course, change your class names to start with a capital letter. If you create such a plateau, you create many platform objects next to each other. Why don't you just use another image? Another thing. You have 'platform' and 'platformFake'. You could use just one platform type and have a boolean (or int) to set the status. This way, you can easily change the platform to a fake platform after some action without removing and replacing the object. You can change line 17 to the following, no need to create a new GreenfootImage.
setBackground("Jungle-Level.png");
Probably this all won't really help you with your current problem though...
Alwin_Gerrits Alwin_Gerrits

2014/10/19

#
It doesn't, but I really appreciate it nevertheless. At some point we were told about array lists and I guess I thought this was a fine way of showing I understood how to use one. My teacher appreciated it at the time because of the way I used it, but looking at it now it's indeed way to much code with a more simple solution being yours. Also thanks for the setBackground thing. I guess thinking about it it's pretty obvious I wouldn't have to declare it as a new image seen it's already in my image list. Even though it didn't fix the problem at hand you did point out a few good things so again thanks for that
Alwin_Gerrits Alwin_Gerrits

2014/10/19

#
YES! I FIXED IT! Turns out I was changing world twice, once to go to the menu-world and once to go to the level-world. However, because it's being given no time between the two changes. That way the world it changes to isn't the level world, but the menu-world. In the end I fixed it by putting a copy of the menu into an actor so I don't have to call two different worlds in succession. Even though I fixed it myself I couldn't have done it without you guys, so thanks guys!!
You need to login to post a reply.
1
2