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

2017/6/10

Spend all day on this Im desperate pls help me

Leon1347896433589 Leon1347896433589

2017/6/10

#
At the beginning the mainplayer is placed in the middle of the world and all the objects around him. Because the map is really big I set bounded on false so that all objects can move out of the visible part of the map and that those objects that are created outside can move in. In order to have a camera I let all objects move around the mainplayer so that it looks as if he would move and the camera would follow him. So, and my problem is that everything laggs around because there are too many objects in the world at the same time so I thought that the best solution is to remove all the objects that leave the visible part of the map and just recreate them when they are supposed to move back into the picture. I managed to remove all objects that leave map but I just dont know how to solve the other part of that Problem. Has someone maybe an idea?
Super_Hippo Super_Hippo

2017/6/10

#
I don't think that it is better to remove and readd them when needed instead of having them move outside of the world. You would constantly have to check if an object leaves or would enter the world. Removing and readding them is also only possible if the object itself don't do anything because they wouldn't do anything when they are removed. If they objects don't need to do anything, what are they? How many objects, how big is the world? Maybe it is possible to draw them on the world background. Then you need to "move" the background with the main player. If your world is "really big", maybe it is possible to split your world into different areas which will be separated. What code do you use to move the objects?
Leon1347896433589 Leon1347896433589

2017/6/10

#
public void steuerung() { long fZeit = System.currentTimeMillis(); if (fZeit >= fZeitCP + 50) { if (Greenfoot.isKeyDown("up")) { for (int i = 0; i < Overall.objekte.size(); i++) { Overall.yKA = Overall.yKA + 1; Overall.objekte.get(i).setRotation(90); Overall.objekte.get(i).move(1); Overall.objekte.get(i).setRotation(0); } } if (Greenfoot.isKeyDown("down")) { for (int i = 0; i < Overall.objekte.size(); i++) { Overall.yKA = Overall.yKA - 1; Overall.objekte.get(i).setRotation(270); Overall.objekte.get(i).move(1); Overall.objekte.get(i).setRotation(0); } } if (Greenfoot.isKeyDown("right")) { for (int i = 0; i < Overall.objekte.size(); i++) { Overall.xKA = Overall.xKA - 1; Overall.objekte.get(i).setRotation(180); Overall.objekte.get(i).move(1); Overall.objekte.get(i).setRotation(0); } } if (Greenfoot.isKeyDown("left")) { for (int i = 0; i < Overall.objekte.size(); i++) { Overall.xKA = Overall.xKA + 1; Overall.objekte.get(i).setRotation(0); Overall.objekte.get(i).move(1); } } fZeitCP = fZeit; } }
Leon1347896433589 Leon1347896433589

2017/6/10

#
the objects are walls the Player isnt supposed to pass or objects he has to interact with i dont think i can just draw them on the background
Super_Hippo Super_Hippo

2017/6/10

#
What are Overall, xKA/yKA and objekte? Oh and why do you use that fZeit?
Leon1347896433589 Leon1347896433589

2017/6/10

#
Overall is the Name of the world xKA and yKA are something i created while trying to solve the Problem so they are not necessary for the movement of the objects Objekte is an arraylist which contains all the objects that are added to the world and fZeit is creating a 50 millisec delay before this method can be used again i hoped the lagging would stop if i would add a Little delay so that the objects couldnt be moved that fast around the world
Leon1347896433589 Leon1347896433589

2017/6/10

#
oh and currently i have around 750 object on the world that all move at the same time but im not finished yet so so i plan to put more objects on it
Super_Hippo Super_Hippo

2017/6/10

#
If you have many wall objects forming one wall, you could consider replacing them with one wall with a bigger image. The code to move the other objects could look like this:
int dx=0, dy=0;
if (Greenfoot.isKeyDown("up")) dy--;
if (Greenfoot.isKeyDown("down")) dy++;
if (Greenfoot.isKeyDown("left")) dx--;
if (Greenfoot.isKeyDown("right")) dx++;

for (Actor a : (java.util.List<Actor>) getWorld().getObjects(null))
{
    if (a != this) a.setLocation(a.getX()+dx, a.getY()+dy);
}
Do not use this fZeit thing. If you want the scenario to be executed slower, lower the execution speed.
Leon1347896433589 Leon1347896433589

2017/6/10

#
uhm i never used the method getObjects() could you explain the 7. line a Little bit in Detail?
Super_Hippo Super_Hippo

2017/6/10

#
It's a for-each loop. It gets all objects (null finds all objects in the world while Wall.class would find all Wall objects (and objects of Wall's subclasses if any)). Then this list is casted to a List of actors (because I wasn't sure if this happens automatically. Maybe you can remove this part.) What it then does is it takes the first Actor in the list which is then 'a'. It executes lines 9, then 'a' will be the next object in the list and so on.
You need to login to post a reply.