When am drawing background it gets drawn over all other images..

Started by
9 comments, last by TheCompBoy 12 years, 8 months ago
Hello guys again.. I've been sitting here trying differend solutions for this problem and now im tired so i whant to ask for some help.. But i don't realy whant to copy / paste the code i whant to learn from it..

Here is my render method

public void render(GameContainer gc, StateBasedGame game, Graphics g) throws SlickException{
super.render(gc, game, g);
levelbackground.draw(0,0);
}

It is drawing the background for the level..

But here i have a method called loadlevel:

public void loadLevel(StateBasedGame game){
levelNumber++;
if(Level.levelExist(levelNumber)){
this.clear();
Player player = new Player(getWidth() / 2,getHeight() / 2);
add(player);
level = Level.load(levelNumber, this);
}
}


And this method takes the player and draws him on the screen after checking if the level exist.. And in the level class the levels are created and all the monsters is placed..
But problem is when am drawing the background its getting drawn over all the sprites / pictures..

I tried to create a separate class called LevelBackground and inside i added the same as for the player and enemy (Player and enemy class uses a command called "setGraphic")
And i did this in the class setGraphic(levelbackground); and added it to the loadlevel method but that didn't work either.. And i tried to add the levelbackground.draw in other classes also but well no succes..

Please some one explain or teach me what i did wrong. Thanks.
Advertisement
Are you drawing the background image first?
No since i didn't find any way to draw it first.

EDIT: Or its like this.. levelloaded method is adding player to the level but i need i think also add the background the same way as i added the player..
Since now its drawing render method first but its not adding to the levelloaded and in someway the player and the enemies gets drawn under the background image..
Every single frame, you need to redraw everything.

First you draw your background.
Then you draw objects under the player.
Then you draw the player.
Then you draw objects over the player.
Then you draw the GUI.

This needs to happen every single frame. Things aren't just drawn once, they are drawn every time anything onscreen changes.
Yes i understand.. But everything is drawing correctly except the background.. Did you see the code i added?
In the levelloaded i think i have to add something like "LevelBackground background = new LevelBackground()" I think i need to add new class then add it like the player.. But i don't realy know how cus when i added the player it says like "Player player = new Player(getWidth() / 2,getHeight() / 2);"
I don't realy know when am adding background what i should type instead of getWidth and height.. Cus that didn't work..
Your game should look roughly something like this:

  • Program starts
    • Load all the resources
    • Setup the starting state of every object
  • Program main loop
    • Get input from the player
    • Update every object (including set the player's new position)
    • Draw every object
      • Draw the background
      • Draw the objects under the player
      • Draw the player
      • Draw the objects over the player
      • Draw the GUI
  • Loop again until exitted
  • Program stops
    • Unload all the resources


  • (Note: The above is simplified, and people can do it other ways, but start with having your program designed like that, before worrying about the more complicated methods)

    Yes i understand.. But everything is drawing correctly except the background.. Did you see the code i added?
    In the levelloaded i think i have to add something like "LevelBackground background = new LevelBackground()" I think i need to add new class then add it like the player.. But i don't realy know how cus when i added the player it says like "Player player = new Player(getWidth() / 2,getHeight() / 2);"
    I don't realy know when am adding background what i should type instead of getWidth and height.. Cus that didn't work..



    Where you have:
    public void render(GameContainer gc, StateBasedGame game, Graphics g) throws SlickException{
    super.render(gc, game, g);
    levelbackground.draw(0,0);
    }


    Try:
    public void render(GameContainer gc, StateBasedGame game, Graphics g) throws SlickException{
    levelbackground.draw(0,0);
    super.render(gc, game, g);
    }


    But also be sure to read my post above this.
    I know but problem is when i put the draw background on another place than render method it creates an error.. And the render method gets drawn in some way over everything else..
    public void render(GameContainer gc, StateBasedGame game, Graphics g) throws SlickException{
    levelbackground.draw(0,0);
    super.render(gc, game, g);
    }


    Wow.. That actualy worked.. Would you mind telling me how?? super doesn't draw anything.. It just make sure if you use the variable names in different methods there will be no errors?

    I don't understand please tell me.

    Wow.. That actualy worked.. Would you mind telling me how?? super doesn't draw anything.. It just make sure if you use the variable names in different methods there will be no errors?

    I don't understand please tell me.


    super.NameOfFunction() calls the parent class's function, so super.render() probably draws or does something.

    This topic is closed to new replies.

    Advertisement