Skip to main content
edited tags
Source Link

[I found this post helpful to what I'm trying to do] (Java Slick2d image depth/layering).

I'm fairly new to the world of coding, and there's probably a simple solution here. I made this array (the actual one is more complex since it includes everything I have so far) for the y values of every object that needs to be rendered, but I don't know how to use it to keep track of which y values correspond to which object in order to draw them from lowest y value to highest y value.

//Printing order
    if (sprite.getY() < tree1.getY()+150) { //if the player is behind the tree

        g2d.drawImage(deer.getImage(), deer.getX(), deer.getY(), this);

        g2d.drawImage(sprite.getImage(), sprite.getX(), 
            sprite.getY(), this);
        g2d.drawImage(skin.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(hair.getImage(), sprite.getX(), 
                sprite.getY(), this);

        //the tree is drawn after everything else   
        g2d.drawImage(tree.getImage(), tree.getX(), tree.getY(), this);
    }


    if (sprite.getY() >= tree1.getY()+150) { //if the player is in front of the tree 

        //the tree is drawn before everything else
        g2d.drawImage(tree1tree.getImage(), tree1tree.getX(), tree1tree.getY(), this); 

        g2d.drawImage(deer.getImage(), deer.getX(), deer.getY(), this);

        g2d.drawImage(sprite.getImage(), sprite.getX(), 
            sprite.getY(), this);
        g2d.drawImage(skin.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(hair.getImage(), sprite.getX(), 
                sprite.getY(), this);
    }

From here, how would I use the yValues array to order the drawing?

[I found this post helpful to what I'm trying to do] (Java Slick2d image depth/layering).

[I found this post helpful to what I'm trying to do] (Java Slick2d image depth/layering).

I'm fairly new to the world of coding, and there's probably a simple solution here. I made this array (the actual one is more complex since it includes everything I have so far) for the y values of every object that needs to be rendered, but I don't know how to use it to keep track of which y values correspond to which object in order to draw them from lowest y value to highest y value.

//Printing order
    if (sprite.getY() < tree1.getY()+150) { //if the player is behind the tree

        g2d.drawImage(deer.getImage(), deer.getX(), deer.getY(), this);

        g2d.drawImage(sprite.getImage(), sprite.getX(), 
            sprite.getY(), this);
        g2d.drawImage(skin.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(hair.getImage(), sprite.getX(), 
                sprite.getY(), this);

        //the tree is drawn after everything else   
        g2d.drawImage(tree.getImage(), tree.getX(), tree.getY(), this);
    }


    if (sprite.getY() >= tree1.getY()+150) { //if the player is in front of the tree
        //the tree is drawn before everything else
        g2d.drawImage(tree1.getImage(), tree1.getX(), tree1.getY(), this); 

        g2d.drawImage(deer.getImage(), deer.getX(), deer.getY(), this);

        g2d.drawImage(sprite.getImage(), sprite.getX(), 
            sprite.getY(), this);
        g2d.drawImage(skin.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(hair.getImage(), sprite.getX(), 
                sprite.getY(), this);
    }

From here, how would I use the yValues array to order the drawing?

I'm fairly new to the world of coding, and there's probably a simple solution here. I made this array (the actual one is more complex since it includes everything I have so far) for the y values of every object that needs to be rendered, but I don't know how to use it to keep track of which y values correspond to which object in order to draw them from lowest y value to highest y value.

//Printing order
    if (sprite.getY() < tree1.getY()+150) { //if the player is behind the tree

        g2d.drawImage(deer.getImage(), deer.getX(), deer.getY(), this);

        g2d.drawImage(sprite.getImage(), sprite.getX(), 
            sprite.getY(), this);
        g2d.drawImage(skin.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(hair.getImage(), sprite.getX(), 
                sprite.getY(), this);

        //the tree is drawn after everything else   
        g2d.drawImage(tree.getImage(), tree.getX(), tree.getY(), this);
    }


    if (sprite.getY() >= tree1.getY()+150) { //if the player is in front of the tree 

        //the tree is drawn before everything else
        g2d.drawImage(tree.getImage(), tree.getX(), tree.getY(), this); 

        g2d.drawImage(deer.getImage(), deer.getX(), deer.getY(), this);

        g2d.drawImage(sprite.getImage(), sprite.getX(), 
            sprite.getY(), this);
        g2d.drawImage(skin.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(hair.getImage(), sprite.getX(), 
                sprite.getY(), this);
    }

From here, how would I use the yValues array to order the drawing?

[I found this post helpful to what I'm trying to do] (Java Slick2d image depth/layering).

deleted 363 characters in body
Source Link

I'm fairly new to the world of coding, and there's probably a simple solution here. I made this array (the actual one is more complex since it includes everything I have so far) for the y values of every object that needs to be rendered, but I don't know how to use it to keep track of which y values correspond to which object in order to draw them from lowest y value to highest y value.

int [] yValues = {fox.getY(), deer.getY(), bunny.getY(), sprite.getY(), tree2.getY()+100, tree1tree.getY()+150};
    Arrays.sort(yValues);

The fox, deer, and rabbit just randomly walkwalks around, and the player (sprite) is controlled by arrow keys. I I added to the y value of the trees so that characters that walk around would stay behind them.

Right now I've hard-coded the layering, depending on when the sprite is in front of or behind the first tree (hair and skin y positions are dependent on the y position of the sprite, which is why I didn't include them in the yValues array).

//Printing order
    if (sprite.getY() < tree1.getY()+150) {
     //if the player g2d.drawImage(bunny.getImage(),is bunny.getX(),behind bunny.getY(),the this);tree
        g2d.drawImage(fox.getImage(), fox.getX(), fox.getY(), this);
        g2d.drawImage(deer.getImage(), deer.getX(), deer.getY(), this); 

        g2d.drawImage(sprite.getImage(), sprite.getX(), 
            sprite.getY(), this);
        g2d.drawImage(skin.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(hair.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(tree2.getImage(), tree2.getX(), tree2.getY(), this);
        //the tree is drawn after everything else   
        g2d.drawImage(tree1tree.getImage(), tree1tree.getX(), tree1tree.getY(), this);
    } 


    if (sprite.getY() >= tree1.getY()+150) { //if the player is in front of the tree
        //the tree is drawn before everything else
        g2d.drawImage(tree1.getImage(), tree1.getX(), tree1.getY(), this); 
        g2d.drawImage(bunny.getImage(), bunny.getX(), bunny.getY(), this);
        g2d.drawImage(fox.getImage(), fox.getX(), fox.getY(), this);
        g2d.drawImage(deer.getImage(), deer.getX(), deer.getY(), this); 

        g2d.drawImage(sprite.getImage(), sprite.getX(), 
            sprite.getY(), this);
        g2d.drawImage(skin.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(hair.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(tree2.getImage(), tree2.getX(), tree2.getY(), this);  
    }

I'm fairly new to the world of coding, and there's probably a simple solution here. I made this array for the y values of every object that needs to be rendered, but I don't know how to use it to keep track of which y values correspond to which object in order to draw them from lowest y value to highest y value.

int [] yValues = {fox.getY(), deer.getY(), bunny.getY(), sprite.getY(), tree2.getY()+100, tree1.getY()+150};
    Arrays.sort(yValues);

The fox, deer, and rabbit just randomly walk around, and the player (sprite) is controlled by arrow keys. I added to the y value of the trees so that characters that walk around would stay behind them.

Right now I've hard-coded the layering, depending on when the sprite is in front of or behind the first tree (hair and skin y positions are dependent on the y position of the sprite, which is why I didn't include them in the yValues array).

//Printing order
    if (sprite.getY() < tree1.getY()+150) {
        g2d.drawImage(bunny.getImage(), bunny.getX(), bunny.getY(), this);
        g2d.drawImage(fox.getImage(), fox.getX(), fox.getY(), this);
        g2d.drawImage(deer.getImage(), deer.getX(), deer.getY(), this);
        g2d.drawImage(sprite.getImage(), sprite.getX(), 
            sprite.getY(), this);
        g2d.drawImage(skin.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(hair.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(tree2.getImage(), tree2.getX(), tree2.getY(), this);
        //the tree is drawn after everything else   
        g2d.drawImage(tree1.getImage(), tree1.getX(), tree1.getY(), this);
    }
    if (sprite.getY() >= tree1.getY()+150) {
        //the tree is drawn before everything else
        g2d.drawImage(tree1.getImage(), tree1.getX(), tree1.getY(), this); 
        g2d.drawImage(bunny.getImage(), bunny.getX(), bunny.getY(), this);
        g2d.drawImage(fox.getImage(), fox.getX(), fox.getY(), this);
        g2d.drawImage(deer.getImage(), deer.getX(), deer.getY(), this);
        g2d.drawImage(sprite.getImage(), sprite.getX(), 
            sprite.getY(), this);
        g2d.drawImage(skin.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(hair.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(tree2.getImage(), tree2.getX(), tree2.getY(), this);  
    }

I'm fairly new to the world of coding, and there's probably a simple solution here. I made this array (the actual one is more complex since it includes everything I have so far) for the y values of every object that needs to be rendered, but I don't know how to use it to keep track of which y values correspond to which object in order to draw them from lowest y value to highest y value.

int [] yValues = {deer.getY(), sprite.getY(), tree.getY()+150};
    Arrays.sort(yValues);

The deer just randomly walks around, and the player (sprite) is controlled by arrow keys. I added to the y value of the trees so that characters that walk around would stay behind them.

Right now I've hard-coded the layering, depending on when the sprite is in front of or behind the tree (hair and skin y positions are dependent on the y position of the sprite, which is why I didn't include them in the yValues array).

//Printing order
    if (sprite.getY() < tree1.getY()+150) { //if the player is behind the tree

        g2d.drawImage(deer.getImage(), deer.getX(), deer.getY(), this); 

        g2d.drawImage(sprite.getImage(), sprite.getX(), 
            sprite.getY(), this);
        g2d.drawImage(skin.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(hair.getImage(), sprite.getX(), 
                sprite.getY(), this);

        //the tree is drawn after everything else   
        g2d.drawImage(tree.getImage(), tree.getX(), tree.getY(), this);
    } 


    if (sprite.getY() >= tree1.getY()+150) { //if the player is in front of the tree
        //the tree is drawn before everything else
        g2d.drawImage(tree1.getImage(), tree1.getX(), tree1.getY(), this); 

        g2d.drawImage(deer.getImage(), deer.getX(), deer.getY(), this); 

        g2d.drawImage(sprite.getImage(), sprite.getX(), 
            sprite.getY(), this);
        g2d.drawImage(skin.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(hair.getImage(), sprite.getX(), 
                sprite.getY(), this);
    }
added 913 characters in body; edited title
Source Link

How to use an array of y values to render a 2D top-down Java game?

I understandI'm fairly new to the conceptworld of the answercoding, and there's probably a simple solution here. My only question is how to use an array I created based offmade this array for the y-values values of the objectsevery object that needs to be rendered, but I would likedon't know how to renderuse it to keep track of which y values correspond to which object in order to draw them from lowest y value to highest y value.

int [] yValues = {fox.getY(), deer.getY(), bunny.getY(), sprite.getY(), tree2.getY()+100, tree1.getY()+150};
    Arrays.sort(yValues);

The fox, deer, and rabbit just randomly walkswalk around, and the player (sprite) is controlled by arrow keys. I added to the y value of the trees so that characters that walk around would stay behind them.

//Printing order
    if (sprite.getY() < tree1.getY()+150) {
        g2d.drawImage(bunny.getImage(), bunny.getX(), bunny.getY(), this);
        g2d.drawImage(fox.getImage(), fox.getX(), fox.getY(), this);
        g2d.drawImage(deer.getImage(), deer.getX(), deer.getY(), this);
        g2d.drawImage(sprite.getImage(), sprite.getX(), 
            sprite.getY(), this);
        g2d.drawImage(skin.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(hair.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(tree2.getImage(), tree2.getX(), tree2.getY(), this);
        //the tree is drawn after everything else   
        g2d.drawImage(tree1.getImage(), tree1.getX(), tree1.getY(), this);
    }
    if (sprite.getY() >= tree1.getY()+150) {
        //the tree is drawn before everything else
        g2d.drawImage(tree1.getImage(), tree1.getX(), tree1.getY(), this); 
        g2d.drawImage(bunny.getImage(), bunny.getX(), bunny.getY(), this);
        g2d.drawImage(fox.getImage(), fox.getX(), fox.getY(), this);
        g2d.drawImage(deer.getImage(), deer.getX(), deer.getY(), this);
        g2d.drawImage(sprite.getImage(), sprite.getX(), 
            sprite.getY(), this);
        g2d.drawImage(skin.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(hair.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(tree2.getImage(), tree2.getX(), tree2.getY(), this);  
    }

From here, how would I use the yValues array to order the drawing?

The above code works for the tree enter image description here

But not for other objects, like this deer:

How to use an array to render a 2D top-down Java game?

I understand the concept of the answer. My only question is how to use an array I created based off the y-values of the objects I would like to render.

int [] yValues = {fox.getY(), sprite.getY(), tree2.getY()+100, tree1.getY()+150};
    Arrays.sort(yValues);

The fox just randomly walks around, and the player (sprite) is controlled by arrow keys. I added to the y value of the trees so that characters that walk around would stay behind them.

//Printing order
    if (sprite.getY() < tree1.getY()+150) {
        g2d.drawImage(fox.getImage(), fox.getX(), fox.getY(), this);
        g2d.drawImage(sprite.getImage(), sprite.getX(), 
            sprite.getY(), this);
        g2d.drawImage(skin.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(hair.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(tree2.getImage(), tree2.getX(), tree2.getY(), this);  
        g2d.drawImage(tree1.getImage(), tree1.getX(), tree1.getY(), this);
    }
    if (sprite.getY() >= tree1.getY()+150) {
        g2d.drawImage(tree1.getImage(), tree1.getX(), tree1.getY(), this);
        g2d.drawImage(fox.getImage(), fox.getX(), fox.getY(), this);
        g2d.drawImage(sprite.getImage(), sprite.getX(), 
            sprite.getY(), this);
        g2d.drawImage(skin.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(hair.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(tree2.getImage(), tree2.getX(), tree2.getY(), this);  
    }

From here, how would I use the yValues array to order the drawing?

How to use an array of y values to render a 2D top-down Java game?

I'm fairly new to the world of coding, and there's probably a simple solution here. I made this array for the y values of every object that needs to be rendered, but I don't know how to use it to keep track of which y values correspond to which object in order to draw them from lowest y value to highest y value.

int [] yValues = {fox.getY(), deer.getY(), bunny.getY(), sprite.getY(), tree2.getY()+100, tree1.getY()+150};
    Arrays.sort(yValues);

The fox, deer, and rabbit just randomly walk around, and the player (sprite) is controlled by arrow keys. I added to the y value of the trees so that characters that walk around would stay behind them.

//Printing order
    if (sprite.getY() < tree1.getY()+150) {
        g2d.drawImage(bunny.getImage(), bunny.getX(), bunny.getY(), this);
        g2d.drawImage(fox.getImage(), fox.getX(), fox.getY(), this);
        g2d.drawImage(deer.getImage(), deer.getX(), deer.getY(), this);
        g2d.drawImage(sprite.getImage(), sprite.getX(), 
            sprite.getY(), this);
        g2d.drawImage(skin.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(hair.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(tree2.getImage(), tree2.getX(), tree2.getY(), this);
        //the tree is drawn after everything else   
        g2d.drawImage(tree1.getImage(), tree1.getX(), tree1.getY(), this);
    }
    if (sprite.getY() >= tree1.getY()+150) {
        //the tree is drawn before everything else
        g2d.drawImage(tree1.getImage(), tree1.getX(), tree1.getY(), this); 
        g2d.drawImage(bunny.getImage(), bunny.getX(), bunny.getY(), this);
        g2d.drawImage(fox.getImage(), fox.getX(), fox.getY(), this);
        g2d.drawImage(deer.getImage(), deer.getX(), deer.getY(), this);
        g2d.drawImage(sprite.getImage(), sprite.getX(), 
            sprite.getY(), this);
        g2d.drawImage(skin.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(hair.getImage(), sprite.getX(), 
                sprite.getY(), this);
        g2d.drawImage(tree2.getImage(), tree2.getX(), tree2.getY(), this);  
    }

From here, how would I use the yValues array to order the drawing?

The above code works for the tree enter image description here

But not for other objects, like this deer:

added 3 characters in body; edited title
Source Link
Loading
Formatting
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401
Loading
Source Link
Loading