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

2019/6/3

Problems Loading Next Level

Radical-Shadow Radical-Shadow

2019/6/3

#
I'm having trouble loading the next level in the MyWorld class. When I set the second level text file as the default, it loads normally. Can anyone help me find (and hopefully solve) the problem?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Scanner;
import java.io.FileReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.lang.Integer;
import java.io.FileInputStream;
import java.io.File;

public class MyWorld extends World
{
    private ArrayList<String> lines = new ArrayList<String>();
    public static String currentLevel = ("levelOne.txt");
    private HashMap<String, Actor> objectKey;
    /**
     * Constructor for objects of class MyWorld. 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        objectKey = new HashMap<String, Actor>();
        setPaintOrder(Instructions.class,Color.class,yellowObstacle.class,blackObstacle.class,redObstacle.class,Water.class,Trampoline.class,Water.class,floatingPlatform.class,Ground.class,Player.class,Blank.class);
        initMap();
        readMap();
        act();        
    }    

    /**
     * Create each level as the player completes the previous one.
     */
    public void act()
    {
        if(Player.isLevelTwo == true)
        {
            currentLevel = ("levelTwo.txt");
            removeFromLevel();
            initMap();
            readMap();
        }
    }

    /**
     * initMap - initializes map
     */
    public void initMap()
    {
        try
        {
            Scanner levelScanner = new Scanner(new File(currentLevel));
            try
            {
                int y = 0;
                while(levelScanner.hasNextLine())
                {                 
                    String line = levelScanner.nextLine();
                    if(line.length() == 0)
                    {
                        lines.add(line);      
                    }
                    else
                    {
                        int x = 0;  
                        for(char nextChar:line.toCharArray())
                        {
                            String coordKey = (x + ";" + y);                              
                            if(nextChar == ('p'))
                            {
                                objectKey.put(coordKey, new Player());
                                x+=37;
                            }
                            else if(nextChar == ('x'))
                            {
                                objectKey.put(coordKey, new Ground());
                                x+=37;
                            }
                            else if(nextChar == ('y'))
                            {
                                objectKey.put(coordKey, new floatingPlatform());
                                x+=50;
                            }
                            else if(nextChar == ('w'))
                            {
                                objectKey.put(coordKey, new Water());
                                x+=37;
                            }
                            else if(nextChar == ('i'))
                            {
                                objectKey.put(coordKey, new Color());
                                x+=38;
                            }
                            else if(nextChar == ('z'))
                            {
                                objectKey.put(coordKey, new blackObstacle());
                                x+=37;
                            }
                            else if(nextChar == ('r'))
                            {
                                objectKey.put(coordKey, new redObstacle());
                                x+=10;
                            }
                            else if(nextChar == ('m'))
                            {
                                objectKey.put(coordKey, new yellowObstacle());
                                x+=37;
                            }
                            else if(nextChar == ('t'))
                            {
                                objectKey.put(coordKey, new Trampoline());
                                x+=37;
                            }    
                            else if(nextChar==('b'))
                            {
                                objectKey.put(coordKey, new Blank());
                                x+=37;
                            }
                        }
                    }
                    y += 37;
                }                
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }
        catch(Exception e)
        {
            System.out.println("Scenario file missing");
            return;
        }        
        //System.out.println("test");
    }

    /**
     * Sets the current level.
     */
    public void setCurrentLevel(String currentLevel)
    {
        this.currentLevel = currentLevel;
    }

    /**
     * Places each object in the coordinates according to its placement in the text file.
     */
    public void readMap()
    {
        for(String key:objectKey.keySet())
        {
            int splitIndex = key.indexOf(";"); 
            int yValue = Integer.parseInt(key.substring(splitIndex+1));
            int xValue = Integer.parseInt(key.substring(0, splitIndex));
            this.addObject(objectKey.get(key), xValue, yValue);
        }
    }
    
    /**
     * Removes objects from level.
     */
    public void removeFromLevel()
    {
        for(String key:objectKey.keySet())
        {
            this.removeObject(objectKey.get(key));
        }
        objectKey.clear();
    }
}
Super_Hippo Super_Hippo

2019/6/3

#
I am not exactly sure what you are doing here, but could it be that it is just extremely laggy because you constantly add and remove tons of objects when the condition in line 35 is met?
Radical-Shadow Radical-Shadow

2019/6/3

#
That makes sense. Do you know if there's any way to minimize that lag?
danpost danpost

2019/6/3

#
Radical-Shadow wrote...
That makes sense. Do you know if there's any way to minimize that lag?
Process the level change in a different method (other than the act method). The act method is called continuously by greenfoot while the scenario is running; other method are only called when needed.
You need to login to post a reply.