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

2016/1/24

Boolean help

Alan_c Alan_c

2016/1/24

#
Hi i'm trying to get to grips with using boolean's and use it in my level layout. I think it should be something like setting Level 1() true then if score equal to set amount then set Level2() ture and Level1()false. Please forgive anything done wrong as i'm just getting to grips and i'm not sure on how to go about this. This is my current code
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()throws IOException 
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600,400,1); 
       
        Level1();
        
        //Level2();
        
        
    }
    public Counter getCounter()
    {
        return counter;
    }
    private void perpare()
    {
        addObject(counter,40,15);
    }
        
    public void Level1()throws IOException 
    {
         mapArray = readMapFromTextFile(MAP_WIDTH, MAP_HEIGHT, FILENAME);
         drawWallMapFromTextArray(WALL_WIDTH,WALL_HEIGHT,WALL_MARKER,PLAYER_WIDTH, PLAYER_HEIGHT, PLAYER_SPAWN,PLAYER_PU_WIDTH, PLAYER_PU_HEIGHT, PICK_UP, mapArray);    
         perpare();
    }
    public void Level2()throws IOException 
    {
         mapArray = readMapFromTextFile(MAP_WIDTH, MAP_HEIGHT, FILENAME2);
         drawWallMapFromTextArray(WALL_WIDTH,WALL_HEIGHT,WALL_MARKER,PLAYER_WIDTH, PLAYER_HEIGHT, PLAYER_SPAWN,PLAYER_PU_WIDTH, PLAYER_PU_HEIGHT, PICK_UP, mapArray);
         perpare();
    }
danpost danpost

2016/1/24

#
The only difference between the 'Level1' and 'Level2' methods is the filename. Now, it looks like (although the information is not explicitly given) you are using constant values for the filenames. A boolean could be used to determine which filename to use; but, this would be limiting -- in that if you deecide to add even more levels, a boolean value (which has only two values) would be insufficient. An int could be used (which would allow any number of levels). It can also be used as an index (or pointer) in an array of constant filenames.
Alan_c Alan_c

2016/1/24

#
Would you know of any examples, tutorials or anything that could help me with what your talking about. My thought process was to use the boolean like e.g. if score was over 1000, I would set Level1()false then, Level2()true then, if score was over 2000 then, Level2()false then, Level3()true Like wise for any other level. I know this may not be conventional but it's the limit of my knowledge at this current time
danpost danpost

2016/1/24

#
It seems to me that the score value alone can be used (being it is the limiting factor as to which level will be active). Or, to determine the current level:
int currentLevel = score/1000+1;
Now, this in itself does not tell you when to change levels. For that, you need to know when the score is changed from on level to another. Or, to determine a change in levels:
int oldLevel = score/1000+1;
score++; // some change in score
int newLevel = score/1000+1;
if (newLevel != oldLevel)
{
    updateLevel(newLevel); // sample method call (pseudo-coded)
}
You need to login to post a reply.