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

2016/10/7

Weird bug?

Freekywill Freekywill

2016/10/7

#
Hey, I am currently making a game where your character is always in the middle of the screen, with the objects moving around you when you press the wasd keys. My game was starting to get laggy, so I decided to store all objects in an array that were outside a certain range of me. The objects initially spawn, and store in the array when I move away, however they won't come back once they have been despawned. This is the code in MyWorld, where I have the objects being created and stored.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    public static String CurrentMenu = "World";
    boolean open = false;
    private boolean eKeyDown = false;
    String key;
    static Material[] things = new Material[10000];
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.

        super(1145, 580, 1, false);

        Var.xOffset = 0;
        Var.yOffset = 0;

        addObject(new Player(), 1145/2, 580/2);
        addObject(new Minimap(), 1045, 480);
        addObject(new Inventory(), 1200, 800);
        addObject(new Crafting(), 1145/2, 580/2);
        SpawnStuff();
        setActOrder(Minimap.class);

    }

    public void act()
    {        
        if(CurrentMenu.equals("Crafting")) setPaintOrder(InventorySlot.class, Button.class, Crafting.class, MapIcon.class, Minimap.class, Barrier.class, Player.class);
        else if(CurrentMenu.equals("Inventory")) setPaintOrder(InventorySlot.class, Inventory.class, MapIcon.class, Minimap.class, Barrier.class, Player.class);
        else setPaintOrder(InventorySlot.class, MapIcon.class, Minimap.class, Material.class, Barrier.class, Player.class, Crafting.class, Button.class);

        RespawnThings();
    }

    public void RespawnThings()
    {
        for(int i = 0; i < things.length; i++)
        {
            if(things[i] != null)
            {
                if(CheckDistance(things[i].locX,things[i].locY) < 2000)
                {
                    addObject(things[i],things[i].locX,things[i].locY);
                }
                else
                {
                    removeObject(things[i]);
                }
            }
        }
    }

    public double CheckDistance(double xLoc, double yLoc)
    {
        double xFromPlayer = Math.abs(572.5 - xLoc);
        double yFromPlayer = Math.abs(290 - yLoc);
        double dFromPlayer = Math.sqrt((Math.pow(xFromPlayer,2)) + (Math.pow(yFromPlayer,2)));

        return dFromPlayer;
    }

    public void SpawnStuff()
    {
        for(int i = 0; i < 1000; i++)
        {
            things[i] = new Tree(Greenfoot.getRandomNumber(10000), Greenfoot.getRandomNumber(10000),i);
        }
        for(int i = 500; i < 1000; i++)
        {
            things[i] = new Stone(Greenfoot.getRandomNumber(10000), Greenfoot.getRandomNumber(10000),i);
        }
    }
}
danpost danpost

2016/10/7

#
The lagging is due to you doing much more than you need to. You do not need to list the actors out of range, or otherwise. You do not need to remove and add actors from the world just because they leave the view through the scenario window. As an unbounded world, all object locations will be kept up to date even when not within the viewport. The way you are going about keeping the character mid-screen is clumsy. Best is to keep a reference to the character in the World subclasss and use its offset from mid-screen to adjust all actors in the world via the 'act' method of the object of that World subclass (in the MyWorld class). Another thing is that you are continually setting the paint order. This is something that can be done once each time the value of CurrentMenu changes. You can make a method to accept the set the new value of CurrentMenu and set the correct paint order at that time. Call the method from the constructor of the MyWorld class and whenever there is a change in the CurrentMenu value.
Freekywill Freekywill

2016/10/7

#
I think you misunderstand, I have thousands of trees and rocks in my world, I think that is the reason I am lagging. Thank you for the help provided, but all I want to know is why the objects wont respawn into my world after I leave the area and come back.
danpost danpost

2016/10/7

#
Freekywill wrote...
I think you misunderstand, I have thousands of trees and rocks in my world, I think that is the reason I am lagging. Thank you for the help provided, but all I want to know is why the objects wont respawn into my world after I leave the area and come back.
For one thing, half of all your trees in the array are replaced with stones (look at the value of 'i' for both loops in the SpawnStuff method). If they are not in the array, they will not respawn. It would help also, if your array was not so large. You only have 1500 actors (trees and stones together), but you are checking 10,000 references for possible actors in the array. Computers are fast, but excess processing to such extent can cause lagging quickly.
You need to login to post a reply.