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

2016/2/1

Not sure on how to go about removing/clearing the screen

Alan_c Alan_c

2016/2/1

#
Hi there i'm not to sure on how to go about removing or clearing the screen but then redraw the next level while keeping the score counter. I've currently been trying to figure out (with some help from the users of this website) how to go about it here is my current code so far, on a side note my levels are created from text files.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.io.*;
import java.util.List; // (for array)
/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class MyWorld extends World
{
// varables
Counter counter = new Counter();

private final int WORLD_WIDTH = getWidth(); // Width of the world 
private final int WORLD_HEIGHT = getHeight(); // Height of the world

Wall wallTemplate = new Wall(); //creates a wall (Not used)
Player playerTemplate = new Player(); //creates a Player (Not used)
Pearl pickupTemplate = new Pearl();

//wall
GreenfootImage wtImg = wallTemplate.getImage(); // get the image of the wall
private final int WALL_HEIGHT = wtImg.getHeight(); //finds the height of the wall
private final int WALL_WIDTH = wtImg.getWidth(); //finds the width of the wall
//player
GreenfootImage plImg = playerTemplate.getImage(); // get the image of the player
private final int PLAYER_HEIGHT = plImg.getHeight(); //finds the height of the Player
private final int PLAYER_WIDTH = plImg.getWidth(); //finds the width of the player
//Item
GreenfootImage ppImg = pickupTemplate.getImage(); // get the image of the Players pickup
private final int PLAYER_PU_HEIGHT = ppImg.getHeight(); //finds the height of the Player Pickup
private final int PLAYER_PU_WIDTH = ppImg.getWidth(); //finds the width of the player Pickup

private final int MAP_WIDTH = (int)(WORLD_WIDTH/WALL_WIDTH); // changes the MAP_WIDTH depending on wall width
private final int MAP_HEIGHT = (int) (WORLD_HEIGHT/WALL_HEIGHT); // changes the MAP_HEIGHT depending on wall height

private final String FILENAME = "Map1.txt";
private final String FILENAME2 = "Map2.txt";

private final String WALL_MARKER = "*";
private final String PLAYER_SPAWN = "P";
private final String PICK_UP = "I";
String[][]mapArray = new String [MAP_HEIGHT][MAP_WIDTH];
///

///
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld(int lv)throws IOException 
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600,400,1); 
        perpare();
        loadLevel(1);
       
    }
    
    public void loadLevel(int lv)throws IOException 
    {
       if (lv==1) 
            Level1();
        else if(lv==2)
        {
            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();
         
    }
    
    public void drawWallMapFromTextArray(int WALL_WIDTH, int WALL_HEIGHT, String WALL_MARKER, int playerHeight, int playerWidth, String playerMarker,int pearlWidth,int pearlHeight, String ItemMarker,String [][] mapArray)
    {
      int x = 0;
      int y = 0;
       for (y=0;y<mapArray.length;y++)
       {
           for (x=0;x<mapArray[y].length;x++)
           {
               if(mapArray[y][x].equals(WALL_MARKER))
               {
                  int wallX = x*WALL_WIDTH + WALL_WIDTH/2;
                  int wallY = y*WALL_HEIGHT + WALL_HEIGHT/2;
                  addObject (new Wall(), wallX, wallY);
                  //addObject(counter,40,15);
                  perpare();
               }
               
               if (mapArray[y][x].equals(playerMarker))
               {
                    int playerX = x*playerWidth + playerWidth/2;
                    int playerY = y*playerHeight + playerHeight/2;
                    addObject(new Player(), playerX, playerY);
               }
               
               
               if (mapArray[y][x].equals(ItemMarker))
               {
                    int pearlX = x*pearlWidth + pearlWidth/2;
                    int pearlY = y*pearlHeight  + pearlHeight/2;
                    addObject(new Pearl(), pearlX,pearlY);
               }
           }
       }
    }   
    
    public String [][] readMapFromTextFile(int MAP_WIDTH, int MAP_HEIGHT, String fileName) throws IOException
    {
            BufferedReader br = null;
            String[][] mArray = new String[MAP_HEIGHT][MAP_WIDTH];
            try{
              br = new  BufferedReader(new FileReader(fileName));
              String l;
              int mapRow= 0; //tells us what row we're on
              while((l=br.readLine())!=null)
              {
               mArray[mapRow] = l.split("");
               mapRow++;
              }
            } finally {
             if (br!=null)
             br.close();
            }
            return mArray;
    }
}
danpost danpost

2016/2/1

#
I think the best thing that you can do is to not to create a new World object for each level. Just use an instance int field to track the level within the one world and then you can remove all objects and adjust the background when calling the 'loadLevel' method. By keeping the same world, you will not be creating a multiple Counter objects.
You need to login to post a reply.