Vector not adding right?

Okay, so this piece of code is to my new project. I’m trying to get things to draw behind other things if they’re above it, and in front of things if they’re below it.

Player, Projectile, and Enemy all extend Thing.
if p1 has a projectile activated with y > enemy[i].y everything draws, but if p1 walks above enemy[i] or p1 fires a projectile while above enemy[i] than enemy[i] won’t draw and it says that drawOrder’s size is -1 of things.size()

also, if p1 fires a projectile above enemy[i] and than walks below enemy[i] everything draws, but the projectile is in front of enemy[i].

Here is the code that fills “things”


things = new Vector<Thing>();
		
		things.add(p1);
		
		for(int i = 0; i < p1.maxProjectiles; i++){
			if(p1.projectile[i].state != p1.projectile[i].STATE_DEAD){
				things.add(p1.projectile[i]);
			}
		}
		
		for(int i = 0; i < MAX_ENEMIES; i++){
			
			things.add(enemy[i]);
			
			for(int j = 0; j < enemy[i].maxProjectiles; j++){
				if(enemy[i].projectile[j].state != enemy[i].projectile[j].STATE_DEAD){
					things.add(enemy[i].projectile[j]);
				}
			}
		}
		
		drawOrder = new Vector<Integer>();

		minY(things, drawOrder);

and here is minY:


public static void minY(Vector<Thing> t, Vector<Integer> order) {		
	    int min = t.get(0).y;
	    int max = t.get(0).y;
	    
	    for (int i = 0; i < t.size(); i++) {
	        if (t.get(i).y <= min) {
	            min = t.get(i).y;
	            order.add(0, i);
	        }
	        
	        else if(t.get(i).y > min && t.get(i).y < max){
	        	
	        	int tempsize = order.size();
	        	
	        	for (int j = 0; j < tempsize; j++){
	        		if(t.get(i).y <= order.get(j)){
	        			order.add(j, i);
	        		}
	        		
	        		else if(t.get(i).y > max){
	        			max = t.get(i).y;
	        			order.add(i);
	        		}
	        	}
	        }
	        
	        else if (t.get(i).y >= max) {
	            max = t.get(i).y;
	            order.add(i);
	        }
	    }
	    
	}//end method max

So it looks like you’re trying to build a list that will determine the order in which you draw the things. Why not just sort the thing list into ascending order of y-coordinate?


Collections.sort( t, new Comparator<Thing>() {
		@Override
		public int compare( Thing p, Thing q )
		{
			return ( int ) Math.signum( p.y - q.y );
		}
	} );

Well I got it working with this code:


public static void minY(Vector<Thing> t, Vector<Integer> order) {		
	    int min = t.get(0).y + t.get(0).height;
	    int max = t.get(0).y + t.get(0).height;
	    
	    order.add(0, 0);
	    
	    for (int i = 1; i < t.size(); i++) {
	        if ((t.get(i).y  + t.get(i).height) < min) {
	            min = (t.get(i).y  + t.get(i).height);
	            order.add(0, i);
	        }
	        
	        else if((t.get(i).y + t.get(i).height) >= min && (t.get(i).y + t.get(i).height) < max){
	        	
	        	int tempsize = order.size();
	        	
	        	for (int j = 0; j < tempsize; j++){
	        		if((t.get(i).y + t.get(i).height) <= (t.get(order.get(j)).y + t.get(order.get(j)).height)){
	        			order.add(j, i);
	        		}
	        	}
	        }
	        
	        else if ((t.get(i).y + t.get(i).height) >= max) {
	            max = (t.get(i).y + t.get(i).height);
	            order.add(i);
	        }
	    }
	    
	    for(int i = 0; i < order.size(); i++){
	    	System.out.println("Order[" + i + "]: " + order.get(i) + " Y: " + (t.get(order.get(i)).y + t.get(order.get(i)).height) + " Class: " + t.get(order.get(i)).getClass().getName());
	    }
	    
	}//end method max