Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Checking who has the first Y in a list.  (Read 291 times)
0 Members and 1 Guest are viewing this topic.
Offline roseslayer

Junior Member


Medals: 1



« Posted 2013-06-15 18:57:11 »

Hi there,

I was coding today some constructions, but the problem I saw was that the constructions were overlapping each other. So I thaught let's make a simple forloop that look for the first y and then draws it.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
    public void renderEntities(Screen screen) {
        for (Entity e : getEntities()) {
            for (int yDraw = 0; yDraw < this.height; yDraw++){
               int yPlace = e.getY();
               if(yPlace == yDraw){
                   e.render(screen);
               }
           }
        }
    }


The idea was simple, but afterwards I checked what I did and I thaught: What am I doing? So I looked at my code again and started to think at what was first the code:

1  
2  
3  
4  
5  
public void renderEntities(Screen screen) {
        for (Entity e : getEntities()) {
                e.render(screen);
        }
}


This code is evrytime taking the first entity in the list (then the second etc.), so why was I looking for the first entity's y? then ++ it, this will make entities that are later in the list and have a higher y invisible. But I don't know how to search wich Y is first and then using that entity first, does anyone know how this works good and can explain me how it works? Because I need to learn how to programm and I want to solve the problem!

Already thanks!
-RoseSlayer

Fundamentum W.I.P.
Offline Danny02
« Reply #1 - Posted 2013-06-15 20:07:02 »

It looks like you want to sort your collection of entities. You can do it like this.

1  
2  
3  
4  
5  
6  
7  
8  
       Comparator<Entity> entityYComparator = new Comparator<Entity>() {

            @Override
            public int compare(Entity o1, Entity o2) {
                return o1.getY() - o2.getY();
            }
        };
        Collections.sort(entities, entityYComparator );
Offline roseslayer

Junior Member


Medals: 1



« Reply #2 - Posted 2013-06-15 21:51:50 »

@Danny02, I am glad you helped me with  this!
I searched a small tutorial for it what it is actually doing really so I can use it in more ways!

Thanks again!
-RoseSlayer

Fundamentum W.I.P.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (72 views)
2014-09-10 06:39:09

Tekkerue (37 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (43 views)
2014-09-07 01:13:42

Longarmx (27 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (34 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!