0
\$\begingroup\$

I am coding a game in libgdx. While adding the background I used the html module to watch the results fast and easy on my computer. But when I try to see it on my phone I just get a black screen. My project is on GitHub.

My Gamescreen.java

package com.joelbrun.jetskirider.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
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.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.badlogic.gdx.physics.box2d.Shape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.utils.Scaling;
import com.badlogic.gdx.utils.viewport.ScalingViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.joelbrun.jetskirider.buoyancy.Box2DFactory;
import com.joelbrun.jetskirider.buoyancy.BuoyancyController;
import com.joelbrun.jetskirider.utilities.ParallaxBackground;
import com.joelbrun.jetskirider.utilities.ParallaxLayer;

public class Gamescreen extends InputAdapter implements Screen, ContactListener {

    private static final float TIMESTEP = 1.0f / 60.0f;
    private static final int VELOCITY_ITERATIONS = 8;
    private static final int POSITION_ITERATIONS = 3;
    public static final float GAMESCREEN_WIDTH = 20;
    public static final float GAMESCREEN_HEIGHT = 20 * (Gdx.graphics.getHeight() / (float) Gdx.graphics.getWidth());
    public static final int OBSTACLE_TYPES = 5;
    public static final float WATER_HEIGHT = 5; //ToDo: Change to about 5
    public static final float WATER_WIDTH = GAMESCREEN_WIDTH;
    private static final float JETSKI_BOX_X = 1.5f;
    private static final float JETSKI_BOX_Y = 1;

    private World world;
    private Box2DDebugRenderer debugRenderer;
    public Texture jetski;
    public TextureRegion wave;
    public TextureRegion background;
    public SpriteBatch batch;
    public OrthographicCamera camera;
    public Label score;
    Viewport viewport;
    private BuoyancyController buoyancyController;
    private ParallaxBackground bkground;


    @Override
    public void show() {

        world = new World(new Vector2(0, -9.81f), true);
        debugRenderer = new Box2DDebugRenderer();

        //Textures
        Texture[] texture = new Texture[OBSTACLE_TYPES];

        for (int i=0; i<OBSTACLE_TYPES; i++){
            texture[i] = new Texture(Gdx.files.internal("game/obstacles/obstacle" + Integer.toString(i+1)+".png"));
        }
        jetski = new Texture("game/jetski.png");
        wave = new TextureRegion(new Texture("game/water1.png"));
        batch = new SpriteBatch();
        background = new TextureRegion(new Texture(Gdx.files.internal("game/background.png")));

        //Camera & Viewport
        camera = new OrthographicCamera(GAMESCREEN_WIDTH, GAMESCREEN_HEIGHT);

        viewport = new ScalingViewport(Scaling.fillY, GAMESCREEN_WIDTH, GAMESCREEN_HEIGHT, camera);
        viewport.apply();
        camera.position.set(GAMESCREEN_WIDTH / 2, GAMESCREEN_HEIGHT / 2, 0);

        //InputProcessor

        Gdx.input.setInputProcessor(this);

        //Water
        Shape shape = Box2DFactory.createBoxShape(WATER_WIDTH, WATER_HEIGHT / 2, new Vector2(0, 0), 0);
        FixtureDef fixtureDef = Box2DFactory.createFixture(shape, 2f, 0.1f, 0f, true);
        Body water = Box2DFactory.createBody(world, BodyDef.BodyType.StaticBody, fixtureDef, new Vector2(0, 0));

        buoyancyController = new BuoyancyController(world, water.getFixtureList().first());
        world.setContactListener(this);

        //Ground
        Shape groundShape = Box2DFactory.createChainShape(new Vector2[]{
                new Vector2(0, -1.8f),
                new Vector2(20, -1.8f)
        });
        FixtureDef fixtureDefGround = Box2DFactory.createFixture(groundShape, 2, 0, 0.2f, false);
        Box2DFactory.createBody(world, BodyDef.BodyType.StaticBody, fixtureDefGround, new Vector2(0, 2));


        //Jetski
        Shape jetskiShape = Box2DFactory.createBoxShape(JETSKI_BOX_X, JETSKI_BOX_Y /2, new Vector2(0, 0), 0);
        FixtureDef fixtureDef1 = Box2DFactory.createFixture(jetskiShape, 0.9f, 0.5f, 0.5f, false);
        Box2DFactory.createBody(world, BodyDef.BodyType.DynamicBody, fixtureDef1, new Vector2(2, WATER_HEIGHT/2));

        //Background Scrolling
        bkground = new ParallaxBackground(new ParallaxLayer[]{
                new ParallaxLayer(background, new Vector2(1,0), new Vector2(0, 0)),
                new ParallaxLayer(wave, new Vector2(1,0), new Vector2(0, 0))
        }, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), new Vector2(150, 0));
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        bkground.render(delta);

        debugRenderer.render(world, camera.combined);

        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        batch.end();
        camera.update();

        world.step(TIMESTEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS);

        buoyancyController.step();
    }

    @Override
    public void resize(int width, int height) {
        int SCREEN_WIDTH = width;
        int SCREEN_HEIGHT = height;

        viewport.setWorldSize(GAMESCREEN_WIDTH, GAMESCREEN_HEIGHT);
        viewport.update(SCREEN_WIDTH, SCREEN_HEIGHT,true);
        camera.update();
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {
        dispose();
    }

    @Override
    public void dispose() {
        batch.dispose();
        jetski.dispose();
        world.dispose();
        debugRenderer.dispose();
    }


    @Override
    public void beginContact(Contact contact) {
        Fixture fixtureA = contact.getFixtureA();
        Fixture fixtureB = contact.getFixtureB();

        if (fixtureA.isSensor()
                && fixtureB.getBody().getType() == BodyDef.BodyType.DynamicBody) {
            buoyancyController.addFixture(fixtureB);
        } else if (fixtureB.isSensor()
                && fixtureA.getBody().getType() == BodyDef.BodyType.DynamicBody) {
            buoyancyController.addFixture(fixtureA);
        }
    }

    @Override
    public void endContact(Contact contact) {
        Fixture fixtureA = contact.getFixtureA();
        Fixture fixtureB = contact.getFixtureB();

        if (fixtureA.isSensor()
                && fixtureB.getBody().getType() == BodyDef.BodyType.DynamicBody) {
            buoyancyController.removeFixture(fixtureB);
        } else if (fixtureB.isSensor()
                && fixtureA.getBody().getType() == BodyDef.BodyType.DynamicBody) {
            if (fixtureA.getBody().getWorldCenter().y > -1) {
                buoyancyController.removeFixture(fixtureA);
            }
        }
    }

    @Override
    public void preSolve(Contact contact, Manifold oldManifold) {

    }

    @Override
    public void postSolve(Contact contact, ContactImpulse impulse) {

    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ It's not actually related to your question but your code is such a mess. Don't use one class for everything from physics to rendering. It's a clear example of Blob antipattern sourcemaking.com/antipatterns/the-blob \$\endgroup\$ – nikoliazekter Aug 27 '15 at 18:35
0
\$\begingroup\$

Allright I found the solutions. My phone was only capable of processing pictures with a size up to 4096*4096px and my Sprite was over 5000px...

\$\endgroup\$
0
\$\begingroup\$

Here goes some useful information for your problem related to texture resolutions: http://answers.unity3d.com/questions/563094/mobile-max-texture-size.html

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.