Hello, we’re a group of four students actually trying to develop a clone of Super Mario with the LibGDX due to a university project.
We’ve implemented all the basic features, such as movement, collisions (both with powerups, enemies and blocks/obstacles), enemies (they all extend Sprite) and so on, while using Tiled for the maps. We’ve followed DermetFan tutorials (“Java Tiled Map Game” series) so, generally, the scheme of our project is like this:
- A main calls a new LwjglApplication(new SuperMario(), cfg);
- The super mario class is defined this way:
public class SuperMario extends Game {
@Override
public void create() {
setScreen(new Play());
}
//the rest of the class is omitted 'cause it's just an overriding of all the the other metods of Game
- Play is our main screen (for the first -and only level- we’ve created yet) and it implements Screen.
THE PROBLEM
So our problem is:
While the game is running, sometimes (actually really often) ALL the sprites just completely disappear, resulting in the Mario Sprite “dying” (and respawning at the location we’ve set) as well as the enemies and the power-ups (mushrooms and Fire flowers), which won’t be rendered anymore.
The fact is that we have NO idea what the problem is. We assume it’s something related to the life cycle of the game, such as the fps skyrockets or gets at such a down value that the game doesn’t actually know WHERE to render the sprites and just… puts them elsewhere (?). We really have no idea how to explain the problem properly, as we are kinda new to the libgdx and this is our second uni year. What’s weirder/funnier is that in Windows Systems, this problem happens basically everytime. In Mac Systems, instead, it doesn’t actually happen (just sporadically). For these reasons, here we put an executable jar file:
Just try and open it more than once and problem should occurs.
This is how it should appear when started (actually there is one less Goomba 'cause it died ALONE before we could actually click on print/stamp):
and here is the render code of the class Play:
@Override
public void render(float delta) {
if (!isPaused){
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.setView(camera);
renderer.render();
//just some camera setting
if( player.getX()<=149 && player.getY()<200 )
camera.position.set((float)149, player.getHeight() /2 + 93, 0);
else if( player.getX()>149 )
if( player.getY()<=130)
camera.position.set(player.getX() + player.getWidth() /2, player.getHeight() /2 + 93, 0);
else
camera.position.set(player.getX() + player.getWidth()/2, player.getY()+ player.getHeight()/+60,0);
if( player.getX()>=640 && player.getY()<120 )
camera.position.set((float)640, player.getHeight()/2+93 ,0);
else if( player.getX()>640 && player.getY()>=120 )
camera.position.set((float)640,player.getY()+player.getHeight()/2+60,0);
camera.update();
//rendering "wallpaper", background, sprites in background, and then foreground
renderer.render(sfondo);
renderer.render (background);
renderer.getSpriteBatch().begin();
//GOOMBA NUMBER 1
if (! goomba.getDeadState() || !disappear){
goomba.draw (renderer.getSpriteBatch());
}//goomba still alive
if (goomba.getDeadState()){
if (goomba.getTime().getTime()<0){
goomba.setPosition(-2 * goomba.getCollisionLayer().getTileWidth(), -2 *player.getCollisionLayer().getTileHeight());
disappear = true;
}
}// gomba dead
//GOOMBA NUMERO 2
if (! goomba2.getDeadState() || !disappear2){
goomba2.draw (renderer.getSpriteBatch());
}//goomba still alive
if (goomba2.getDeadState()){
if (goomba2.getTime().getTime()<0){
goomba2.setPosition(-2 * goomba2.getCollisionLayer().getTileWidth(), -2 *goomba.getCollisionLayer().getTileHeight());
disappear2 = true;
}
}//goomba dead
//KOOPA
if(! koopa.getDeadState() ){
koopa.draw(renderer.getSpriteBatch());
}
//FUNGO 1UP
if (! fungo.getDeadState()){
fungo.draw(renderer.getSpriteBatch());
}
//FUNGO MUSH
if( !mush.getDeadState()){
mush.draw(renderer.getSpriteBatch());
}
//FIREFLOWER 1 & 2
if (! fireFlower.getDeadState()){
fireFlower.draw(renderer.getSpriteBatch());
}//flower1
if (! fireFlower2.getDeadState ()){
fireFlower2.draw(renderer.getSpriteBatch());
}//flower2
if (player.getDeadState()){
bgm.stop();
Time MDTime = player.getMarioDeathTime();
if (MDTime.getTime()<0){
bgm = Gdx.audio.newMusic(Gdx.files.internal("com/me/sounds/overworld.mp3"));
bgm.setLooping(true);
bgm.play();
player.setDeadState(false);
player.decreaseLife();
try {
player.setMarioDeathTime();
} catch (InterruptedException e) {}
//setposition
player.setPosition(10 * player.getCollisionLayer().getTileWidth(), 3 * player.getCollisionLayer().getTileHeight());
}
}
if (player.getInvulnerability()){
if (!player.getInvulnerableTime().isAlive()){
player.createInvulnerableTime();
}
if (player.getInvulnerableTime().getTime()<0){
player.setInvulnerability(false);
if(!player.getFireMario() && !player.getBigMario()){
player.setNormalAnimation();
}
player.setInvulnerableTime();
}
}
for( int i =0;i<3;i++ )
if (fireballs.get(i)!=null && player.getSpara() && !fireballs.get(i).getDeadState() ){
fireballs.get(i).draw(renderer.getSpriteBatch());
}
if (!player.getDeadState()){
player.draw (renderer.getSpriteBatch());
}
renderer.getSpriteBatch().end();
renderer.render(foreground);
renderer.getSpriteBatch().begin();
//the upper region where life number, time and coin number are shown
renderer.getSpriteBatch().draw(coin, camera.position.x+100, camera.position.y+77, 0, 0, 12, 12, 1,1, 0);
renderer.getSpriteBatch().draw(life, camera.position.x-140, camera.position.y+77, 0, 0, 12, 12, 1,1, 0);
renderer.getSpriteBatch().draw(time, camera.position.x, camera.position.y+77, 0, 0, 12, 12, 1,1, 0);
font.draw(renderer.getSpriteBatch(), ""+player.getCoin(), camera.position.x+120, camera.position.y+ 90);
font.draw(renderer.getSpriteBatch(), ""+player.getLife(), camera.position.x-130, camera.position.y+ 90);
if( timer.getTime()<0 ){
player.timedOut();
timer.setTime(200);
}
font.draw(renderer.getSpriteBatch(), ""+timer.getTime(), camera.position.x+20, camera.position.y+ 90);
//System.out.println("camera x: "+camera.position.x+" camera y: "+camera.position.y );
renderer.getSpriteBatch().end();
}//if(!isPaused)
}//render
In case you need the whole project, we can share it with you, but we really need your help, because this problem is driving us mad, and it may affects the result of our exam.
Thanks in advance.