在 phone 上启动时出现黑屏
Black screen when launching on phone
我正在 libgdx 编写游戏代码。在添加背景时,我使用 html 模块在我的计算机上快速轻松地查看结果。但是当我试图在我的 phone 上看到它时,我只是看到黑屏。我的项目在 GitHub.
我的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) {
}
}
好的,我找到了解决方案。我的phone只能处理最大4096*4096px的图片,而我的Sprite超过5000px...
我正在 libgdx 编写游戏代码。在添加背景时,我使用 html 模块在我的计算机上快速轻松地查看结果。但是当我试图在我的 phone 上看到它时,我只是看到黑屏。我的项目在 GitHub.
我的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) {
}
}
好的,我找到了解决方案。我的phone只能处理最大4096*4096px的图片,而我的Sprite超过5000px...