We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › Gamesprite Draw Order
Page Index Toggle Pages: 1
Gamesprite Draw Order (Read 995 times)
Gamesprite Draw Order
Aug 12th, 2007, 2:01am
 
I am looking for some insight into drawing sprites to the screen. I am using a 2D environment where the y coordinate determines the placement of the sprite from front most to back most. I am using this to create a pseudo perspective.
Re: Gamesprite Draw Order
Reply #1 - Aug 12th, 2007, 2:18am
 
and what is your question ?
Re: Gamesprite Draw Order
Reply #2 - Aug 12th, 2007, 2:30am
 
I have these variables:

Monk m_a = new Monk(); // player 1
Monk m_b = new Monk(); // player 2
Monk enemy = new Monk(); // new enemy
Monk enemy2 = new Monk(); // new enemy
Monk enemy3 = new Monk(); // new enemy
Monk enemy4 = new Monk(); // new enemy

from class:

public class Monk {

 int x = 130; // position of monk 'b' x-axis NOTE* constrain to play area
 int y = 64;  // position of monk 'b' y-axis
 int z = 0;   // position of monk 'b' z-axis - accounts for increased or decreased character scale
 int old_x = 0; // old positions
 int old_y = 0;
 int old_z = 0;
 int facing = 0; // facing 0 is to screen left, facing 1 is to screen right.
 int onGround = 1; // 1 = yes , 0 = no
 int playerNum = 1; // 0 = player 1, 1 = player 2.
 //stats
 int health = 100;
 int strength = 10;
 int vitality = 10;
 int holy = 50;
 float regen = 1;
 //Special stats / attacks
 boolean holyCow = false ;
 boolean holyMackerel = false;
 boolean holyMolie = false;
 boolean handOfGod = false;
 boolean sacraficialLamb = false;
 boolean theTenCommandments = false;
 boolean brewersYeast = false;
 boolean scribsScribbles = false;
 boolean blindFaith = false;
 boolean firstTestimate = false;
 boolean kingJames = false;
 boolean mosesWave = false;
 //animation sequence data = to the number of cells in the animation
 int sequence_walk = 2;
 int sequence_punch = 2;
 int sequence_stand = 4;
 PImage[] walkingL = new PImage[sequence_walk];
 PImage[] walkingR = new PImage[sequence_walk];
 PImage[] punchingL = new PImage[sequence_punch];
 PImage[] punchingR = new PImage[sequence_punch];
 PImage[] standingL = new PImage[sequence_stand];
 PImage[] standingR = new PImage[sequence_stand];
 void Monk(){
  //nothing
   int sequence_walk = 2;  // double the number to account for right and left movements.
   int sequence_punch = 2; // double the number to account for right and left movements.
   int sequence_stand = 4; // double the number to account for right and left movements.
   PImage[] walkingL = new PImage[sequence_walk];
   PImage[] walkingR = new PImage[sequence_walk];
   PImage[] punchingL = new PImage[sequence_punch];
   PImage[] punchingR = new PImage[sequence_punch];
   PImage[] standingL = new PImage[sequence_stand];
   PImage[] standingR = new PImage[sequence_stand];
 }
 void printVariables(){
  print(x+y+z+old_x+old_y+old_z+facing+ onGround + playerNum+health+ strength+vitality+holy);
 }
 void update(){
    if( x > old_x){
      old_x = x;
      facing = 1; //character should face right
    } else if(x < old_x) {
      old_x = x;
      facing = 0; //character should face left
    }
 }
 boolean collision(int colliderA, int colliderB, int spriteWidth, int spriteHight){
  // run the collision detection
  // very basic collision detection just tells if the two sprites have any portions touching.
  if ( colliderA <= x && colliderA >= (x + spriteHight)){
    if (colliderB <= y && colliderB >= (y + spriteWidth)){
     return true;
    }
  }
  else{
    return false;
  }
  return false;
 }
 
 void drawWalking(int frameNumber){
   if(facing == 0){
     image(walkingL[frameNumber], x , y);
   } else if(facing == 1){
     image(walkingR[frameNumber], x , y);  
   }
 }
 
 void drawStanding(int frameNumber){
   if(facing == 0){
     image(standingL[frameNumber], x , y);
   } else if(facing == 1){
     image(standingR[frameNumber], x , y);
   }
 }
 
 void drawPunching(int frameNumber){
   if(facing == 0){
     image(punchingL[frameNumber], x , y);
   } else if(facing == 1){
      image(punchingR[frameNumber], x , y);
   }    
 }


}

What is the best way to display these game sprites to answer the problem:

If monk.y is larger than all other monk.y's draw PImage first ...

Is there an easy way to implement this with out bloating my code with a huge number of "if" statements?

Re: Gamesprite Draw Order
Reply #3 - Aug 12th, 2007, 4:29am
 
sort your sprites by your y value and then render them to screen.
i assume high y-values draw first means they are rendered on top of others..

http://en.wikipedia.org/wiki/Sorting_algorithm

i used quicksort or radix sort years ago, but you can try out others and see which one fits.
Page Index Toggle Pages: 1