Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  Vector not adding right?  (Read 612 times)
0 Members and 1 Guest are viewing this topic.
Offline CyanPrime
« Posted 2010-09-21 15:08:42 »

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.y  everything draws, but if p1 walks above enemy or p1 fires a projectile while above enemy than enemy won't draw and it says that drawOrder's size is -1 of things.size()

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

Here is the code that fills "things"

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
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
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #1 - Posted 2010-09-21 15:24:19 »

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?
1  
2  
3  
4  
5  
6  
7  
Collections.sort( t, new Comparator<Thing>() {
      @Override
      public int compare( Thing p, Thing q )
      {
         return ( int ) Math.signum( p.y - q.y );
      }
   } );
Offline CyanPrime
« Reply #2 - Posted 2010-09-21 17:36:16 »

Well I got it working with this code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
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
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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (44 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (32 views)
2014-09-21 01:30:30

moogie (40 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
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!