Libgdx & Android Studio: Label doesn't show any text - java

I am trying to add a Label text to the screen but nothing seems to be showing up: This is the code that I have coded and below has the screen that I ended up with, I could still change all colours but nothing shows up
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
public class FiraMain extends ApplicationAdapter {
SpriteBatch batch;
private Skin mySkin;
private Stage stage;
#Override
public void create () {
batch = new SpriteBatch();
stage = new Stage(new ScreenViewport());
int guides = 12;
int rowHeight = Gdx.graphics.getWidth() / 12;
int colWidth = Gdx.graphics.getHeight() / 12;
Label.LabelStyle labelStyle = new Label.LabelStyle();
Skin mySkin = new Skin(Gdx.files.internal("skin/glassy-ui.json"));
Label label = new Label("Text", mySkin);
label.setSize(Gdx.graphics.getWidth() / guides, rowHeight);
label.setPosition(colWidth * 2, Gdx.graphics.getHeight() - rowHeight * 6);
stage.addActor(label);
}
#Override
public void render () {
Gdx.gl.glClearColor(1, 1, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.end();
}
#Override
public void dispose () {
}
}
Image

I think by mistake you forgot to call draw method of stage.
#Override
public void render () {
Gdx.gl.glClearColor(1, 1, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw(); //<-- Call draw method of stage inside render, it calls draw on every actor in the stage
stage.act();
}
Also I like to recommend you to dispose all disposable element in your game.
#Override
public void dispose () {
stage.dispose();
mySkin.dispose();
batch.dispose();
}

Related

Using the Sprite class for Animation in LibGDX

I'm making a simple game in LibGDX. I want to use the Sprite classes methods for my main player, while using the Animation class in order to animate the player as well. I've gotten Animation to work, I just haven't been able to integrate it with the Sprite class.
Here is my code:
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.Array;
public class Game extends ApplicationAdapter {
SpriteBatch batch;
private TextureAtlas runAtlas;
private Animation runAnimation;
private TextureAtlas idleAtlas;
private Animation idleAnimation;
private TextureRegion currentIdleFrame;
private Sprite player;
private float elapsedTime = 0;
private float playerX = 10;
private float playerY = 10;
#SuppressWarnings({ "unchecked", "rawtypes" })
#Override
public void create () {
batch = new SpriteBatch();
runAtlas = new TextureAtlas(Gdx.files.internal("runSpritesheet.atlas"));
runAnimation = new Animation(1/12f, runAtlas.getRegions());
idleAtlas = new TextureAtlas(Gdx.files.internal("idleSpritesheet.atlas"));
idleAnimation = new Animation(1/3f, idleAtlas.getRegions());
player = new Sprite();
}
#Override
public void render () {
currentIdleFrame = (TextureRegion) idleAnimation.getKeyFrame(elapsedTime, true);
elapsedTime += Gdx.graphics.getDeltaTime();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
if(Gdx.input.isKeyPressed(Keys.DPAD_RIGHT)) {
batch.draw((TextureRegion) runAnimation.getKeyFrame(elapsedTime, true), playerX, playerY);
} else if (Gdx.input.isKeyPressed(Keys.DPAD_LEFT)) {
batch.draw((TextureRegion) runAnimation.getKeyFrame(elapsedTime, true), playerX, playerY);
} else {
player.setRegion(currentIdleFrame);
player.draw(batch);
}
batch.end();
}
#Override
public void dispose () {
batch.dispose();
runAtlas.dispose();
idleAtlas.dispose();
}
}
The focus is on the idle animation of the player in the "else" statement. I try setting the player sprite region to the current key frame of the idle animation sprite sheet. However, when I launch the game, no sprite with the idle animation comes up. I don't get any errors either. I am hoping someone knows how to fix this problem. Thanks for reading.

Black Screen when using Tiled in LibGDX

For the game I'm making I use Tiled to make and edit maps. However, these maps don't load in the sample I've made so far:
package com.bluezamx.magillion.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Rectangle;
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.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FillViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.bluezamx.magillion.Magillion;
import com.bluezamx.magillion.utils.Constants;
public class WorldScreen implements Screen {
private Magillion game;
private OrthographicCamera gameCam;
private Viewport gamePort;
private Stage stage;
//Tiled variables
private TmxMapLoader mapLoader;
private TiledMap map;
private OrthogonalTiledMapRenderer maprenderer;
// Box2D variables
private World world;
private Box2DDebugRenderer debug;
public WorldScreen (Magillion game) {
this.game = game;
gameCam = new OrthographicCamera();
mapLoader = new TmxMapLoader();
map = mapLoader.load("TestWorld.tmx");
maprenderer = new OrthogonalTiledMapRenderer(map, 1 / Constants.PPM);
gamePort = new FillViewport(Constants.WIDTH / Constants.PPM, Constants.HEIGHT / Constants.PPM, gameCam);
gameCam.position.set((gamePort.getWorldWidth() / 2), (gamePort.getWorldHeight() / 2), 0);
world = new World(new Vector2(0,0), true);
debug = new Box2DDebugRenderer();
debug.SHAPE_STATIC.set(1, 0, 0, 1);
for(MapObject object : map.getLayers().get(1).getObjects().getByType(RectangleMapObject.class)) {
Rectangle rect = ((RectangleMapObject) object).getRectangle();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set(rect.getX() + rect.getWidth() / 2, rect.getY() + rect.getHeight() / 2);
body = world.createBody(bdef);
shape.setAsBox(rect.getWidth() / 2, rect.getHeight() / 2);
fdef.shape = shape;
body.createFixture(fdef);
}
}
#Override
public void show() {}
private void update(float dt) {
handleInput(dt);
world.step(1/60f, 6, 2);
player.update(dt);
gameCam.update();
maprenderer.setView(gameCam);
}
#Override
public void render(float dt) {
update(dt);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debug.render(world, gameCam.combined);
game.batch.setProjectionMatrix(gameCam.combined);
maprenderer.render();
game.batch.begin();
player.draw(game.batch);
game.batch.end();
}
#Override
public void resize(int width, int height) {}
#Override
public void pause() {}
#Override
public void resume() {}
#Override
public void hide() {}
#Override
public void dispose() {
map.dispose();
maprenderer.dispose();
world.dispose();
debug.dispose();
}
}
When I run my file (which automatically sets the game screen to this file), I only get a black screen. The map itself doesn't show. The map is 640 * 480 big and placed in the standard android/assets folder.
I tried running my map in other code I found online and it worked there. I couldn't figure out what's wrong with mine, however.
You need to update your viewport with device screen width and height.
#Override
public void resize(int width, int height) {
// use true here to center the camera
gamePort.update(width,height,false);
}
Take a look of this answer, recently I added as solution.
It seems like your map is actually rendered, but simply your Viewport is out of map position.
Try using:
gamecam.setToOrtho(false, gamePort.getWorldWidth()/2, gamePort.getWorldHeight()/2);
instead of
gameCam.position.set((gamePort.getWorldWidth() / 2), (gamePort.getWorldHeight() / 2), 0);

libgdx background image not displaying

I have in the constructor:
backgroundTexture = new Texture(Gdx.files.internal("Pantalla.jpg"));
And in the render():
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
myGame.batch.setProjectionMatrix(camera.combined);
myGame.batch.begin();
myGame.batch.draw(backgroundTexture, 0, 0);
stage.draw();
myGame.batch.end();
}
The full code of the class is:
package states;
import com.badlogic.gdx.Gdx;
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.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import mx.itesm.mty.MyGame;
public class MenuState implements Screen{
final MyGame myGame;
Skin skin;
Skin skinPant;
Stage stage;
SpriteBatch batch;
TextButton.TextButtonStyle textButtonStyle1;
TextButton.TextButtonStyle textButtonStyle2;
TextButton.TextButtonStyle textButtonStyle3;
TextButton.TextButtonStyle textButtonStyle4;
TextureAtlas buttonAtlas;
BitmapFont font;
Texture backgroundTexture;
Sprite backgroundSprite;
OrthographicCamera camera;
public MenuState(final MyGame game) {
myGame = game;
camera = new OrthographicCamera();
camera.setToOrtho(false,800,480);
batch = new SpriteBatch();
skin = new Skin();
font = new BitmapFont();
stage = new Stage();
Gdx.input.setInputProcessor(stage);
buttonAtlas = new TextureAtlas(Gdx.files.internal("boton.atlas"));
skin.addRegions(buttonAtlas);
backgroundTexture = new Texture(Gdx.files.internal("Pantalla.jpg"));
backgroundSprite = new Sprite(backgroundTexture);
textButtonStyle1 = new TextButton.TextButtonStyle();
textButtonStyle1.font = font;
textButtonStyle1.down = skin.getDrawable("Inicio");
textButtonStyle1.up = skin.getDrawable("Inicio");
textButtonStyle1.checked = skin.getDrawable("Inicio");
TextButton buttonPlay = new TextButton("", textButtonStyle1);
textButtonStyle2 = new TextButton.TextButtonStyle();
textButtonStyle2.font = font;
textButtonStyle2.down = skin.getDrawable("Instrucciones");
textButtonStyle2.up = skin.getDrawable("Instrucciones");
textButtonStyle2.checked = skin.getDrawable("Instrucciones");
TextButton buttonInst = new TextButton("",textButtonStyle2);
textButtonStyle3 = new TextButton.TextButtonStyle();
textButtonStyle3.font = font;
textButtonStyle3.down = skin.getDrawable("Informacion");
textButtonStyle3.up = skin.getDrawable("Informacion");
textButtonStyle3.checked = skin.getDrawable("Informacion");
TextButton buttonInfo = new TextButton("",textButtonStyle3);
textButtonStyle4 = new TextButton.TextButtonStyle();
textButtonStyle4.font = font;
textButtonStyle4.down = skin.getDrawable("Salir");
textButtonStyle4.up = skin.getDrawable("Salir");
textButtonStyle4.checked = skin.getDrawable("Salir");
TextButton buttonSalir = new TextButton("",textButtonStyle4);
buttonPlay.setWidth(300f);
buttonPlay.setHeight(100f);
buttonPlay.setPosition(Gdx.graphics.getWidth() / 2 - 150f, Gdx.graphics.getHeight() / 2);
buttonInst.setWidth(300f);
buttonInst.setHeight(100f);
buttonInst.setPosition(Gdx.graphics.getWidth() / 2 - 150f, Gdx.graphics.getHeight() / 2 - 120f);
buttonInfo.setWidth(300f);
buttonInfo.setHeight(100f);
buttonInfo.setPosition(Gdx.graphics.getWidth() / 2 - 150f, Gdx.graphics.getHeight() / 2 - 240f);
buttonSalir.setWidth(300f);
buttonSalir.setHeight(100f);
buttonSalir.setPosition(Gdx.graphics.getWidth() / 2 - 150f, Gdx.graphics.getHeight() / 2 - 360f);
buttonPlay.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
myGame.setScreen(new PlayMenuState(myGame));
}
});
buttonInst.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
myGame.setScreen(new PlayMenuState(myGame));
}
});
buttonInfo.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
myGame.setScreen(new PlayMenuState(myGame));
}
});
buttonSalir.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
myGame.setScreen(new PlayMenuState(myGame));
}
});
stage.addActor(buttonPlay);
stage.addActor(buttonInst);
stage.addActor(buttonInfo);
stage.addActor(buttonSalir);
}
#Override
public void show() {
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
myGame.batch.setProjectionMatrix(camera.combined);
myGame.batch.begin();
myGame.batch.draw(backgroundTexture, 0, 0);
stage.draw();
myGame.batch.end();
}
#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 add several buttons and the background image. The image is in the assets folder but it juts doesn't show up.
And I had a doubt about rectangles that if there is a way to add an image to a Rectangle object the same way you set the x, y, height, width?
A Stage has it's own batch which will call begin() and end(), unless you specifically set the stage to use the same batch, the begin() and end() calls will conflict.
However you also might want to clean up your code, because you create a background sprite but not use it. I would recommend using this in stead of directly drawing the texture though (because a sprite can also have a position and other attributes which you can manipulate).

2 Borders of Screen Flickers

Im trying to make a simple game in LibGdx, using Tiled MapEditor and I have a little problem with rendering, left and bottom borders flickers whenever i move a camera.
Pic Related: https://gyazo.com/63b9e364cd4b2e8154c1bd177c9ee990
MyGdxGame.java
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
AssetLoad load;
Player player;
TiledMapRenderer tiledMapRenderer;
TiledMap tiledMap;
float unitScale = 1 / 32f;
#Override
public void create () {
load = new AssetLoad();
load.manager.finishLoading();
if(load.manager.update()){
batch = new SpriteBatch();
player = new Player();
tiledMap = new TmxMapLoader().load("tilemaps/321.tmx");
tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap,unitScale);
batch.setProjectionMatrix(player.cam.combined);
}
}
#Override
public void render () {
update();
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
tiledMapRenderer.setView(player.cam);
tiledMapRenderer.render();
batch.end();
}
public void update(){
player.playerMove();
player.cam.update();
player.cam.position.set(player.x,player.y,0);
}
public void dispose(){
}
Player.java
package com.mygdx.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.OrthographicCamera;
public class Player {
public OrthographicCamera cam = new OrthographicCamera(1024,720);
int x = 10;
int y = 12;
public Player(){
cam.setToOrtho(false,30,20);
}
public void playerMove()
{
if(Gdx.input.isKeyPressed(Keys.W))
{
y += 1;
}
if(Gdx.input.isKeyPressed(Keys.A))
{
x -= 1;
}
if(Gdx.input.isKeyPressed(Keys.S))
{
y -= 1;
}
if(Gdx.input.isKeyPressed(Keys.D))
{
x += 1;
}
}
}
You update the cam before your set it's position rather than after, and the projection matrix is never updated. Hopefully these two things fix your problem.
public void update(){
player.playerMove();
player.cam.update();
player.cam.position.set(player.x,player.y,0);
}
should be
public void update(){
player.playerMove();
player.cam.position.set(player.x,player.y,0);
player.cam.update();
batch.setProjectionMatrix(player.cam.combined);
}

Error:(11, 24) error: package javafx.animation does not exist. (Android Studio, Libgdx, Javafx)

I am trying to create a Space Shooter game using LibGDX in Android Studio.
Everything else was working fine until I tried to create animation using javafx.animation.Animation class. While compiling my code, I get the following 3 errors and 1 warning !
Warning:[options] bootstrap class path not set in conjunction with -source 1.6
Error:(11, 24) error: package javafx.animation does not exist
Error:(22, 13) error: cannot find symbol class Animation
Error:(44, 26) error: cannot find symbol class Animation
I have checked that my android studio does have a plugin for javafx !
I have two classes in my code Shootergame.java and AnimatedSprite.java
The problem lies in AnimatedSprite.java class and the code for that class is below !
package com.ahmed.nullapointershooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import javafx.animation.Animation;
/**
* Created by Hafeez ur Rehman on 8/30/2015.
**/
public class AnimatedSprite {
private static final int FRAMES_COL = 2;
private static final int FRAMES_ROW = 2;
private Sprite sprite;
private Animation animation;
private TextureRegion[] frames;
private TextureRegion currentFrame;
private float stateTime;
public AnimatedSprite(Sprite sprite) {
this.sprite = sprite;
Texture texture = sprite.getTexture();
TextureRegion[][] temp = TextureRegion.split(texture, texture.getWidth() / FRAMES_COL,
texture.getHeight() / FRAMES_ROW);
frames = new TextureRegion[FRAMES_COL * FRAMES_ROW];
int index = 0;
for (int i = 0; i < FRAMES_ROW; i++) {
for (int j = 0; j < FRAMES_COL; j++) {
frames[index++] = temp[i][j];
}
}
/**
* THE PROBLEM LIES HERE . I WANT TO DO THIS ----
*
*/
//animation = new Animation(0.1f, frames);
animation = new Animation() {
#Override
public void impl_playTo(long l, long l1) {
}
#Override
public void impl_jumpTo(long l, long l1) {
}
};
stateTime = 0f;
}
public void setPosition(float x, float y){
float widthOffset = sprite.getWidth() / FRAMES_COL;
sprite.setPosition(x - widthOffset / 2, y);
}
public void draw(SpriteBatch spriteBatch){
stateTime += Gdx.graphics.getDeltaTime();
//currentFrame = animation.getKeyFrame(stateTime, true);
spriteBatch.draw(currentFrame, sprite.getX(), sprite.getY());
}
}
and also code for Shootergame.java just for reference for what I am doing!
package com.ahmed.nullapointershooter;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class ShooterGame extends ApplicationAdapter {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture background;
private AnimatedSprite spaceshipAnimated;
#Override
public void create() {
//Texture.setEnforcePotImages(false);
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
batch = new SpriteBatch();
background = new Texture(Gdx.files.internal("NullapointerBackground.png"));
Texture spaceshipTexture = new Texture(Gdx.files.internal("Spaceshipcanvas.png"));
Sprite spaceshipSprite = new Sprite(spaceshipTexture);
spaceshipAnimated = new AnimatedSprite(spaceshipSprite);
spaceshipAnimated.setPosition(800 / 2, 0);
}
#Override
public void dispose() {
batch.dispose();
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(background, 0, 0);
spaceshipAnimated.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
}
}
My problem solved . I was using Javafx to import animation class which was wrong at the first place because there is no javafx package in android. Instead I had to import
import com.badlogic.gdx.graphics.g2d.Animation; Now its working !

Categories