Java-Gaming.org Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (595)
Games in Android Showcase (168)
games submitted by our members
Games in WIP (646)
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 717 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 Devvie


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.

 
deepthought (49 views)
2015-06-30 15:39:44

deepthought (56 views)
2015-06-30 15:39:09

deepthought (65 views)
2015-06-30 15:36:52

Za\'Anzabar (43 views)
2015-06-29 05:44:54

TritonDreyja (49 views)
2015-06-24 17:10:40

CopyableCougar4 (49 views)
2015-06-23 00:34:45

BurntPizza (58 views)
2015-06-21 20:36:46

cookiecompiler (98 views)
2015-06-11 15:42:53

cookiecompiler (58 views)
2015-06-11 15:41:14

NegativeZero (82 views)
2015-06-11 09:49:18
How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14
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!