For some reason, my android phone will not load a table even though I have it being drawn. It shows up on the desktop version, but not on the android version. You can see my adding it in the render() method in the if statement. It loads perfectly on my desktop. This is only showing the code I used for the table. The desktop version and phone version has the same camera size. Everything else appears on my phone except for this table.
public class game implements Screen {
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
tManager.update(delta);
backBatch.begin();
splash.draw(backBatch);
backBatch.end();
stage.act(delta);
stage.draw();
world.step(TIMESTEP, VITERATIONS, PITERATIONS);
table2 = new Table(skin);
table2.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
int numContacts = world.getContactCount();
if (numContacts > 0) {
table2.add(heading3).spaceBottom(35).row();
table2.add(replayButton).spaceBottom(15).row();
table2.add(menuButton);
movement.x = -sSpeed;
movement.y = 10;
movement.rotate(720);
stage.addActor(table2);
Gdx.input.setInputProcessor(stage);
}
debugRenderer.render(world, camera.combined);
}
public void resize(int width, int height) {
camera.viewportWidth = 450;
camera.viewportHeight = 250;
camera.update();
stage.setViewport(450, 250, true);
stage.getCamera().translate(-stage.getGutterWidth(), -stage.getGutterHeight(), 0);
if (table != null){
table.setSize(450, 250);
}
}
#Override
public void show()
{
stage = new Stage();
world = new World(new Vector2(0, 0), true);
debugRenderer = new Box2DDebugRenderer();
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
//replay button
replayButton = new TextButton("REPLAY", skin);
replayButton.addListener(new ClickListener() {
public void clicked(InputEvent event, float x, float y)
{
((Game) Gdx.app.getApplicationListener()).setScreen(new game());
}
});
replayButton.pad(15);
//main menu button
menuButton = new TextButton("MAIN MENU", skin);
menuButton.addListener(new ClickListener() {
public void clicked(InputEvent event, float x, float y)
{
((Game) Gdx.app.getApplicationListener()).setScreen(new MainMenu());
}
});
menuButton.pad(10);
//game over
heading3 = new Label("game over", skin);
heading3.setFontScale(2);
}
private void createCollisionListener() {
//collisions in world
world.setContactListener(new ContactListener() {
public void beginContact(Contact contact) {
}
public void endContact(Contact contact) {
}
public void preSolve(Contact contact, Manifold oldManifold) {
}
public void postSolve(Contact contact, ContactImpulse impulse) {
}
});
}
Related
I get Fatal signal 11 on my Samsung Galaxy S6 Edge when I try to enter the playscreen. On the desktop emulator it works fine and I can enter the playscreen class. I also tested with a Nexus 6 and it worked even there. What's wrong with my phone?
When I try to debug the app crashes when I'm reading this line:
app.getApplicationListener().render();
PlayScreen class:
public class PlayScreen2 implements Screen {
private TextureAtlas atlas;
private OrthographicCamera gamecam;
private Viewport gamePort;
private Hud2 hud;
private TmxMapLoader maploader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private World world;
public Box2DDebugRenderer b2dr;
public static Runner player;
private float dt;
private boolean keyPressed = false;
private boolean keyPressed2 = false;
public PlayScreen2(){
atlas = new TextureAtlas("Runner_And_Enemies.pack");
gamecam = new OrthographicCamera();
gamePort = new FitViewport(RunningGame.V_WIDTH / RunningGame.PPM, RunningGame.V_HEIGHT / RunningGame.PPM, gamecam);
hud = new Hud2(Globals.game.batch);
maploader = new TmxMapLoader();
map = maploader.load("level2.tmx");
renderer = new OrthogonalTiledMapRenderer(map, 1 / RunningGame.PPM);
gamecam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2 , 0);
world = new World(new Vector2(0, -10), true);
b2dr = new Box2DDebugRenderer();
new B2WorldCreator(world, map);
player = new Runner(world, this);
}
public TextureAtlas getAtlas(){
return atlas;
}
#Override
public void show() {
}
public void handleInput(float dt){
if (Gdx.input.isKeyPressed(Input.Keys.ANY_KEY)) {
if (!keyPressed) {
keyPressed = true;
if (player.b2body.getLinearVelocity().x <= 4) {
player.b2body.applyLinearImpulse(new Vector2(0.3f, 0), player.b2body.getWorldCenter(), true);
}
Hud2.addscore(1);
}
} else {
keyPressed = false;
}
if (Gdx.input.isKeyPressed(Input.Keys.SPACE)) {
if (!keyPressed2) {
keyPressed2 = true;
if (player.b2body.getLinearVelocity().y <= 4.0f) {
player.b2body.applyLinearImpulse(new Vector2(0, 3.0f), player.b2body.getWorldCenter(), true);
}
}
} else {
keyPressed2 = false;
}
if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && player.b2body.getLinearVelocity().x >= -2)
player.b2body.applyLinearImpulse(new Vector2(-5.2f, 0), player.b2body.getWorldCenter(), true);
}
public void update(float dt){
handleInput(dt);
world.step(1 / 60f, 6, 2);
player.update(dt);
hud.update(dt);
gamecam.position.x = player.b2body.getPosition().x;
gamecam.update();
renderer.setView(gamecam);
}
#Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
b2dr.render(world, gamecam.combined);
Globals.game.batch.setProjectionMatrix(gamecam.combined);
Globals.game.batch.begin();
player.draw(Globals.game.batch);
Globals.game.batch.end();
Globals.game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
}
#Override
public void resize(int width, int height) {
gamePort.update(width, height);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
map.dispose();
renderer.dispose();
world.dispose();
b2dr.dispose();
hud.dispose();
}
}
My game works fine on desktop, but when I load it on an android device or the emulator I get a black screen. The game is working; I can hear the sounds and click the UI buttons. When testing on my android phone, I can turn the screen off and on again, and then the game renders fine. I would appreciate an explanation of what I'm doing wrong.
public class TestProject extends Game {
GameScreen gameScreen;
EndScreen endScreen;
MenuScreen menuScreen;
#Override
public void create () {
Gdx.app.log("TestProject", "created");
AssetLoader.load();
gameScreen = new GameScreen(this);
endScreen = new EndScreen(this);
menuScreen = new MenuScreen(this);
setScreen(menuScreen);
}
#Override
public void dispose() {
super.dispose();
AssetLoader.dispose();
}
}
public class MenuScreen implements Screen{
private SpriteBatch spriteBatch;
final static float GAME_WIDTH = 200;
final static float GAME_HEIGHT = 100;
public static final float UNIT_SCALE = 1/8f;
static float aspectRatio;
private OrthographicCamera cam;
Viewport viewport;
Stage stage;
TestProject project;
public MenuScreen(TestProject theProject){
project = new TestProject();
this.project = theProject;
aspectRatio = (float)Gdx.graphics.getHeight() / (float)Gdx.graphics.getWidth();
spriteBatch = new SpriteBatch();
stage = new Stage();
Gdx.input.setInputProcessor(stage);
cam = new OrthographicCamera();
viewport = new ExtendViewport(GAME_WIDTH * aspectRatio, GAME_HEIGHT, cam);
viewport.apply();
cam.setToOrtho(false, GAME_WIDTH, GAME_HEIGHT);
cam.position.set(GAME_WIDTH / 2, GAME_HEIGHT / 2, 0);
stage.addActor(AssetLoader.playButton);
}
#Override
public void render(float delta){
//Clear screen
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
spriteBatch.setProjectionMatrix(cam.combined);
AssetLoader.font.getData().setScale(.25f, -.25f);
AssetLoader.playButton.setPosition((Gdx.graphics.getWidth() / 2) - AssetLoader.playButton.getWidth() / 2,
(Gdx.graphics.getHeight() / 3) - AssetLoader.playButton.getHeight() / 2);
spriteBatch.begin();
spriteBatch.draw(AssetLoader.menuBG, 0, 0, 200, 100);
AssetLoader.font.draw(spriteBatch, "Shark Run", 55, 85);
spriteBatch.end();
stage.draw();
}
#Override
public void show() {
AssetLoader.playButton.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
project.setScreen(project.gameScreen);
}
});
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
}
}
I have a project on libgdx and I'm using a splash screen. The problem is, every time I block my phone on main menu and unlock it again, game shows splash screen again and I'd like it not to "restart" the game but stay where it is
EDIT: to avoid publishing all unnecessary code, I'll explain how I made it:
simple class extending game calls setScreen to splash screen, then after 3 seconds splash screen calls main menu, and I'd like the splash screen not to appear again unless the game is DESTROYED and opened again
code:
public MainMenuScreen(final TowerConquest gam) {
game = gam;
}
#Override
public void show() {
stage = new Stage(new FillViewport(800, 480));
Gdx.input.setInputProcessor(stage);
//((OrthographicCamera)stage.getCamera()).setToOrtho(false, 800, 480);
atlas = new TextureAtlas("skins/userInterface.pack");
skin = new Skin(atlas);
table = new Table(skin);
table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
white = new BitmapFont(Gdx.files.internal("font/white.fnt"), false);
black = new BitmapFont(Gdx.files.internal("font/black.fnt"), false);
TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.up = skin.getDrawable("normalbutton");
textButtonStyle.down = skin.getDrawable("pressedbutton");
textButtonStyle.disabled = skin.getDrawable("dissabledbutton");
textButtonStyle.pressedOffsetX = 1;
textButtonStyle.pressedOffsetY = -1;
textButtonStyle.font = black;
buttonPlay = new TextButton("PLAY", textButtonStyle);
buttonPlay.pad(20);
buttonPlay.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
game.setScreen(new testGame(game));
//Gdx.input.vibrate(150);
dispose();
}
});
buttonExit = new TextButton("EXIT", textButtonStyle);
buttonExit.pad(20);
buttonExit.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
dispose();
Gdx.app.exit();
}
});
LabelStyle headingStyle = new LabelStyle(white, Color.WHITE);
heading = new Label("Tower Conquest", headingStyle);
table.add(heading);
table.getCell(heading).padBottom(50);
table.row();
table.add(buttonPlay);
table.getCell(buttonPlay).spaceBottom(15);
table.row();
table.add(buttonExit);
table.debug();
table.setOrigin(table.getWidth()/2, table.getHeight()/2);
table.setFillParent(true);
stage.addActor(table);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
#Override
public void resize(int width, int height) {
my resizes...
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
all disposes..
}
I found the answer, add this on manifest file
android:configChanges="keyboard|keyboardHidden|screenSize|orientation">
it is because when the screen gets locked it changes orientation to portrait so it restarts the app, with that option on, it won't restart the application
I am trying to display a TextField on the screen. It works perfectly when the desktop version is run but when I run the android version the textfield is duplicated and the background is messed up.
screenshots
Code of Screen class:
public class SetupScreen implements Screen
{
private Stage stage;
private Table table;
private TextField paymentField;
#Override
public void show()
{
stage = new Stage();
Gdx.input.setInputProcessor(stage);
table = new Table();
table.setFillParent(true);
stage.addActor(table);
Pixmap bgPixmap = new Pixmap(1, 1, Format.RGBA8888);
bgPixmap.setColor(Color.GRAY);
bgPixmap.fill();
Texture bgTexture = new Texture(bgPixmap);
Pixmap cursorPixmap = new Pixmap(1, 1, Format.RGBA8888);
cursorPixmap.setColor(Color.WHITE);
cursorPixmap.fill();
Texture cursorTexture = new Texture(cursorPixmap);
Pixmap selPixmap = new Pixmap(1, 1, Format.RGBA8888);
selPixmap.setColor(Color.BLUE);
selPixmap.fill();
Texture selTexture = new Texture(selPixmap);
TextFieldStyle textFieldStyle = new TextFieldStyle();
textFieldStyle.background = new SpriteDrawable(new Sprite(bgTexture));
textFieldStyle.cursor = new SpriteDrawable(new Sprite(cursorTexture));
textFieldStyle.selection = new SpriteDrawable(new Sprite(selTexture));
textFieldStyle.font = new BitmapFont();
textFieldStyle.fontColor = Color.WHITE;
paymentField = new TextField("", textFieldStyle);
paymentField.setMessageText("Pay Rate");
table.add(paymentField);
}
#Override
public void render(float delta)
{
stage.act(delta);
stage.draw();
}
#Override
public void resize(int width, int height)
{
stage.getViewport().update(width, height);
}
#Override
public void pause()
{
}
#Override
public void resume()
{
}
#Override
public void hide()
{
}
#Override
public void dispose()
{
stage.dispose();
}
}
Your screen is completely messed up because you do not clear the screen at the beginning of each frame.
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
Another thing to note for UI is that you should center your camera. Otherwise the bottom-left corner of your UI will be in the middle of your screen.
You do that with the last parameter of the Viewport.update() method.
#Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
This is the code for my gamescreen where i want burst my ballon when its touched .
orientation is portrait.
but it does not seem to work for me.
public class GameScreen implements Screen {
final BB game;
private BitmapFont font;
private static final int no_of_frames = 2;
Texture ballonFrames;
TextureRegion[] burstFrames = new TextureRegion[no_of_frames];
Animation burstAnimation;
Array<Rectangle> ballons;
TextureRegion currentFrame;
long lastBallonTime;
int ballonBursted;
OrthographicCamera camera;
int ballonMissed;
Sound ballonBursting;
public GameScreen(final BB gam) {
this.game = gam;
ballonFrames = new Texture(Gdx.files.internal("ballon_burst.png"));
font = new BitmapFont(Gdx.files.internal("font.fnt"), false);
ballonBursting = Gdx.audio.newSound(Gdx.files
.internal("BallonBursting.wav"));
TextureRegion[][] tmp = TextureRegion.split(ballonFrames,
ballonFrames.getWidth() / 2, ballonFrames.getHeight());
burstFrames[0] = tmp[0][0];
burstFrames[1] = tmp[0][1];
burstAnimation = new Animation(3.0f, burstFrames);
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
ballons = new Array<Rectangle>();
spawnBallon();
}
private void spawnBallon() {
Rectangle ballon = new Rectangle();
ballon.x = MathUtils.random(0, 800 - 64); //
ballon.y = 0;
ballon.width = 40;
ballon.height = 80;
ballons.add(ballon);
lastBallonTime = TimeUtils.nanoTime();
}
private boolean ballonBursted(Rectangle ballon) {
Vector2 touch = new Vector2(Gdx.input.getX(), Gdx.input.getY());
if (ballon.contains(touch))
return true;
else
return false;
}
#Override
public void render(float delta) {
// TODO Auto-generated method stub
Gdx.gl.glClearColor(0, 0, 0.3f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
InputProcessor processor;
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
font.draw(game.batch, "Ballon Bursted :" + ballonBursted, 0, 700);
font.draw(game.batch, "Ballon Missed:" + ballonMissed, 275, 700);
for (Rectangle ballon : ballons) {
game.batch.draw(burstFrames[0], ballon.x, ballon.y);
}
if (TimeUtils.nanoTime() - lastBallonTime > 1000000000) {
spawnBallon(); // a ballon every second
}
Iterator<Rectangle> iter = ballons.iterator();
while (iter.hasNext()) {
Rectangle ballon = iter.next();
ballon.y = ballon.y + 100 * Gdx.graphics.getDeltaTime();
if (ballonBursted(ballon) == true) {
ballonBursted++;
game.batch.draw(burstFrames[1], ballon.x, ballon.y);
ballonBursting.play();
iter.remove();
}
else if (ballon.y + 64 > 800) {
iter.remove();
ballonMissed++;
}
}
if (ballonMissed > 5) {
game.setScreen(new ScoreScreen(game, ballonBursted));
}
game.batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
ballonFrames.dispose();
ballonBursting.dispose();
game.batch.dispose();
}
I am using animation class of libgdx to change my image of ballon to the one where its bursted .
I am fairly new to libgdx and unable to figure out what wrong am i doing here .
Should i create a table and layout my ballon elements as actor?
Try something like this:
private boolean ballonBursted(Rectangle ballon) {
Vector3 touchPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
if (ballon.contains(touchPos.x, touchPos.y))
return true;
else
return false;
}
please read this https://stackoverflow.com/a/18555705/2158970
If I understand it right you just want to know if the touchpoint is contained in the Rectangle ballon. Then you could use Rectangle#contains() method:
ballon.contains(Gdx.input.getX(), Gdx.input.getY());
see also the source code of Rectangle class