Black Screen when I run endless runner game on libgdx

Anything libgdx related goes here!

Black Screen when I run endless runner game on libgdx

Postby paulrobert2019 » Thu Sep 12, 2019 11:48 am

Hello everyone,

I'm newbie on libgdx. I'm trying to create a simple endless runner game with libgdx, and just to test, I started following a tutorial, using and adapting some codes below:

GAME MAIN
Code: Select all
package com.mygdx.texturepackage;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class GameMain extends Game {

   
   @Override
   public void create () {
      setScreen(new GameScreen());
   }

   @Override
   public void render () {
        super.render();

   }
   
   @Override
   public void dispose () {

   }
}



GAME SCREEN

Code: Select all
package com.mygdx.texturepackage;


import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;

public class GameScreen implements Screen {

    private GameStage stage;

    public GameScreen(){
        stage = new GameStage();
    }

    @Override
    public void show() {

    }

    @Override
    public void render(float delta) {

        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.draw();
        stage.act(delta);

    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {

    }
}



GAME STAGE
Code: Select all
package com.mygdx.texturepackage;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Scaling;
import com.badlogic.gdx.utils.viewport.ScalingViewport;

import helpers.GameInfo;

public class GameStage extends Stage{

    private static final float VIEWPORT_WIDTH = GameInfo.WIDTH;
    private static final float VIEWPORT_HEIGHT = GameInfo.HEIGHT;

    private World world;
    private Body ground;

    private OrthographicCamera camera;
    private Box2DDebugRenderer renderer;

    private final float TIME_STEP = 1 / 300f;
    private float acumulator =0f;

    public GameStage(){
        super(new ScalingViewport(Scaling.stretch, VIEWPORT_WIDTH, VIEWPORT_HEIGHT,
                new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT)));
       world = BodyWorld.createWorld();
       ground = BodyWorld.createBody(world, GameInfo.GROUND_X, GameInfo.GROUND_Y, GameInfo.GROUND_DENSITY);
       renderer = new Box2DDebugRenderer();
       setUpCamera();
    }

    private void setUpCamera(){
       camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
       camera.position.set(camera.viewportWidth/2, camera.viewportHeight/2, 0f);
       camera.update();
    }

    @Override
    public void draw() {
        super.draw();
        renderer.render(world, camera.combined);
    }

    @Override
    public void act(float delta) {
        super.act(delta);

        acumulator += delta;

        while (acumulator >=delta){
            world.step(TIME_STEP, 6, 2);
            acumulator -=TIME_STEP;
        }
    }
}



BODY AND WORLD TO PHISICS

Code: Select all
package com.mygdx.texturepackage;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;

import helpers.GameInfo;
import sun.security.pkcs11.wrapper.Constants;

public class BodyWorld {

    public static World createWorld(){
        return  new World(GameInfo.WORLD_GRAVITY, true);
    }

    public static Body createBody(World world, float x, float y, float density) {
        BodyDef bodyDef = new BodyDef();
        bodyDef.position.set(new Vector2(x, y));
        Body body = world.createBody(bodyDef);
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(x / 2, y / 2);
        body.createFixture(shape, density);
        shape.dispose();
        return body;
    }
}



When I run the app the screen load and it's black and exit automaticaly. What's happining? Thank you so much in advance for your time.
paulrobert2019
 
Posts: 3
Joined: Mon Aug 26, 2019 8:10 pm

Re: Black Screen when I run endless runner game on libgdx

Postby evilentity » Thu Sep 12, 2019 2:41 pm

Check logs for error message.
Looking for a freelancer? PM me!
Check out libgdx discord server!
evilentity
 
Posts: 4851
Joined: Wed Aug 24, 2011 11:37 am

Re: Black Screen when I run endless runner game on libgdx

Postby yobowargames » Thu Sep 12, 2019 7:26 pm

You have things reversed in your render method

stage.act(delta)
stage.draw()
yobowargames
 
Posts: 11
Joined: Sat Oct 27, 2018 4:25 pm

Re: Black Screen when I run endless runner game on libgdx

Postby QuiIIraven » Fri Sep 13, 2019 10:31 am

Also your accumulator does not make sense.
Check the while condition again.
QuiIIraven
 
Posts: 79
Joined: Sat Jun 02, 2018 4:59 pm


Return to Libgdx

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest

cron