Jesus, seperate entities PLEASE

Been going at this for a couple of hours on JUST this, and I can’t seem to figure it out.
using it for debugging on collision detection.
well, I have an entity class, and all npcs and the player are entities, I keep all the entities in an arraylist except for the player.
my problem is, there is a method in the entity class that has their Rectangle, and since I’m debugging, I go to the render method and do, g.draw(rectangle), and it only draws it for the one entity, then I tried looping it from a different class eg.(for(Entity e : ents) {g.draw(e.getRectangle());}, and STILL only draws one of the entitys. This is really pissing me off.
Let me know if you need some of the code, or you could just explain to me what could be causing it.
And let me know if you need more info.
Thanks.

Posting the code would be most helpful.

Also try doing a println of each rect to the logs or using the debugger. This will help determine if they are all the same or if they are offscreen etc.

Why people have to ask code from you while you’re asking for help?

First aid:

  • Make sure your ents has > 1 entities
  • Debug everything that static
  • Printout ents.size() and entity’s position

Jesus says NO.

for(int i = 0; i < ents.size(); i++)
g.draw(ents.get(i));

Assuming you’re using arraylist. Like the others said, check the list size, make sure you’re not breaking the loop anywhere, make sure that it truly isn’t drawing it rather than it drawing it offscreen or really small/large.

Most likely all your entities are referring to the same rectangle making it show at one place only. Do the following in your entities:

public Rectangle getRectangle(){
rectangle.setRect(getX(),getY(),getWidth(),getHeight());
return rectangle;
}

Less likely, quite obvious so you probably thought about it, the rectangles are off-screen. Eg you are drawing using the coordinates of the entities but remember while the worldmap may be large (2000x2000 pixels) the gamescreen might only be 512x512 in which case rendering with x,y values outside 512x512 will not show the rectangles, here is a very easy fix:


public Rectangle getRectangle(){
rectangle.setRect(getX()%512, getY()%512, getWidth(), getHeight());
return rectangle;

This again assumes you have screensize 512x512, just change with your actual widths and heights.

This will at least update the rectangle even if there is only one for all of the entities, assuming you have those getters in your class :slight_smile:

Jesus says NO.

for(int i = 0; i < ents.size(); i++)
g.draw(ents.get(i));

Assuming you’re using arraylist. Like the others said, check the list size, make sure you’re not breaking the loop anywhere, make sure that it truly isn’t drawing it rather than it drawing it offscreen or really small/large.

Most likely all your entities are referring to the same rectangle making it show at one place only. Do the following in your entities:

public Rectangle getRectangle(){
rectangle.setRect(getX(),getY(),getWidth(),getHeight());
return rectangle;
}

Less likely, quite obvious so you probably thought about it, the rectangles are off-screen. Eg you are drawing using the coordinates of the entities but remember while the worldmap may be large (2000x2000 pixels) the gamescreen might only be 512x512 in which case rendering with x,y values outside 512x512 will not show the rectangles, here is a very easy fix:


public Rectangle getRectangle(){
rectangle.setRect(getX()%512, getY()%512, getWidth(), getHeight());
return rectangle;

This again assumes you have screensize 512x512, just change with your actual widths and heights.

This will at least update the rectangle even if there is only one for all of the entities, assuming you have those getters in your class :slight_smile: