Sign in to follow this  
TheCompBoy

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

Recommended Posts

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
[code]
public void render(GameContainer gc, StateBasedGame game, Graphics g) throws SlickException{
super.render(gc, game, g);
levelbackground.draw(0,0);
}[/code]
It is drawing the background for the level..

But here i have a method called loadlevel:
[code]
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);
}
}[/code]

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.

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
Your game should look roughly something like this:

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

(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)

Share this post


Link to post
Share on other sites
[quote name='TheCompBoy' timestamp='1311459953' post='4839384']
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..
[/quote]


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

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

But also be sure to read my post above this.

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
[code]public void render(GameContainer gc, StateBasedGame game, Graphics g) throws SlickException{
levelbackground.draw(0,0);
super.render(gc, game, g);
} [/code]

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.

Share this post


Link to post
Share on other sites
[quote name='TheCompBoy' timestamp='1311461135' post='4839390']
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.
[/quote]

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this