Libgdx Desktop Launcher blank - java

Just starting Libgdx and java game development. Got a little bit of code to try and write a sprite sheet moving animation person, just started it so not finished by when i try an run it the single sprite drawing is not running, nothing is the desktop appliction is just black.
Desktop Launcher:
package com.mkgame.game1.desktop;
import com.MKgames.OptionScreen;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.mkgame.game1.Game1;
public class DesktopLauncher extends Game1{
public static void main (String[] arg) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "Game1";
cfg.width = 960;
cfg.height = 540;
new LwjglApplication(new Game1 (), cfg);
}
}
Main java class 'Game1':
package com.mkgame.game1;
import com.MKgames.FarmerAsset;
import com.MKgames.OptionScreen;
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;
public class Game1 extends ApplicationAdapter {
public OptionScreen game_screen;
public void create() {
FarmerAsset.load();
}
}
Game screen:
package com.MKgames;
import sun.java2d.loops.DrawGlyphListAA.General;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class OptionScreen implements Screen{
GameMain game;
OrthographicCamera camera;
SpriteBatch batch;
int farmerX;
public OptionScreen(GameMain game){
this.game = game;
camera = new OrthographicCamera();
camera.setToOrtho(true, 1920, 1080);
batch = new SpriteBatch();
farmerX = 960-85;
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0.95F, 0.95F, 0.95F, 0.95F);
camera.update();
generalUpdate();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(FarmerAsset.farmer1, farmerX, 200);
batch.end();
}
public void generalUpdate() {
if(Gdx.input.isKeyPressed(Keys.A)||Gdx.input.isKeyPressed(Keys.LEFT)){
farmerX -= 5;
}
else if(Gdx.input.isKeyPressed(Keys.D)||Gdx.input.isKeyPressed(Keys.RIGHT)){
farmerX += 5;
}
}
#Override
public void show() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
#Override
public void resize(int width, int height) {
}
#Override
public void hide() {
}
}

Change
public class Game1 extends ApplicationAdapter {
to
public class Game1 extends Game {
Create an instance of OptionScreen in Game:
public OptionScreen game_screen;
public void create() {
FarmerAsset.load();
game_screen = new OptionScreen(this);
setScreen(game_screen);
}
And it should be good to go.

Related

Libgdx button onclick not working

I have created a button and I want to change its appearance on hover and on click. I get no errors, but it isn't working. It doesn't change the image when it is clicked or when it is hovered. The only image that is displayed is the one from playButtonStyle.up.
Here is my code:
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.starships.MainClass;
import com.sun.prism.paint.Color;
import helpers.Info;
public class MainMenuScreen implements Screen {
MainClass game;
Stage stage;
private Texture background;
private AssetManager assets;
private TextureAtlas atlas;
private Skin skin;
private Table table;
private Button playButton;
public MainMenuScreen(MainClass mainClass) {
game = mainClass;
Gdx.input.setInputProcessor(stage);
stage = new Stage();
background = new Texture(Gdx.files.internal("Background.png"));
assets = new AssetManager();
assets.load("Buttons/PlayButtonAtlas.atlas", TextureAtlas.class);
assets.finishLoading();
atlas = assets.get("Buttons/PlayButtonAtlas.atlas");
skin = new Skin(atlas);
table = new Table(skin);
table.setBounds(0, 0, Info.WIDTH, Info.HEIGHT);
Button.ButtonStyle playButtonStyle = new Button.ButtonStyle();
playButtonStyle.up = skin.getDrawable("PlayButton");
playButtonStyle.over = skin.getDrawable("PlayButtonHover");
playButtonStyle.down = skin.getDrawable("PlayButtonPressed");
playButton = new Button(playButtonStyle);
table.add(playButton);
stage.addActor(table);
}
#Override
public void show() {
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0.7f, 0.8f, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
stage.dispose();
}
}
Set InputProcessor after initialisation of Stage like this :
public MainMenuScreen(MainClass mainClass) {
game = mainClass;
stage = new Stage();
Gdx.input.setInputProcessor(stage); // This call should be after initialisation of stage.
background = new Texture(Gdx.files.internal("Background.png"));
...
...
}

Java libGdx: FreeTypeFontGenerator is not working (Black screen)

I added in a custom font Aller_light.ttf and when I try to run the program with the font drawn with no errors the screen is black, so I'm not sure if its rendering somewhere else off screen or its not rendering at all.
If you could give your 2 cents that would be of great value to me.
EDIT
Now it's saying i have a nullPointerExeption
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.mygdx.finis.states.MenuState.draw(MenuState.java:43)
at com.mygdx.finis.states.GameStateManager.draw(GameStateManager.java:31)
at com.mygdx.finis.Main.render(Main.java:36)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
This is what should be happening:
MenuState Class
package com.mygdx.finis.states;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.mygdx.finis.Assets;
import com.mygdx.finis.Main;
public class MenuState extends GameState{
private SpriteBatch sb;
private BitmapFont font;
private int currentItem;
private String[] menuItems;
protected MenuState(GameStateManager gsm) {
super(gsm);
}
public void init() {
sb = new SpriteBatch();
FreeTypeFontGenerator gen = new FreeTypeFontGenerator(
Gdx.files.internal(null));
font = gen.generateFont(20);
menuItems = new String[]{
"Play"
};
}
public void update(float dt) {
handleInput();
}
public void draw() {
sb.setProjectionMatrix(Main.cam.combined);//*** This is where the null pointer starts *********
sb.begin();
font.draw(sb, "Quack", Main.WIDTH / 2, Main.HEIGHT / 2);//*** This does not work *******************
sb.end();
}
public void handleInput() {
}
public void dispose() {
}
}
Main Class
package com.mygdx.finis;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.mygdx.finis.states.GameStateManager;
public class Main implements ApplicationListener{
public static int WIDTH;
public static int HEIGHT;
public static OrthographicCamera cam;
private GameStateManager gsm;
public void create(){
WIDTH = Gdx.graphics.getWidth();
HEIGHT = Gdx.graphics.getHeight();
cam = new OrthographicCamera(WIDTH, HEIGHT);
cam.translate(WIDTH / 2, HEIGHT / 2);
cam.update();
gsm = new GameStateManager();
}
public void render(){
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
gsm.update(Gdx.graphics.getDeltaTime());
gsm.draw();//****** THIS IS WHERE THE ISSUE ENDS ******
}
public void resize(int width, int height){}
public void pause(){}
public void resume(){}
public void dispose(){}
}
It seems most likely that your SpriteBatch sb is null. Are you absolutely sure init() is being called?
The only other alternative is that Main.cam is null.
It has to be one of those.
Incidentally, this looks odd to me...
Gdx.files.internal(null));
Why are you doing that?

Error with libGdx Screens

I have a problems when I try to compiled this is supuse that will a game.
Exception in thread "LWJGL Application" java.lang.NullPointerException at com.waflesgames.spaceinvader.Screens.PlayScreen.render(PlayScreen.java:35)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.waflesgames.spaceinvader.SpaceInvader.render(SpaceInvader.java:24)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
This is the PlayScreen.java
package com.waflesgames.spaceinvader.Screens;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.waflesgames.spaceinvader.SpaceInvader;
import javax.xml.soap.Text;
/**
* Created by Diego on 03/10/2015.
*/
public class PlayScreen implements Screen{
public SpaceInvader game;
Texture texture;
private OrthographicCamera gamecam;
private Viewport gamePort;
public PlayScreen(SpaceInvader Game){
this.game = game;
texture = new Texture("badlogic.jpg");
gamecam = new OrthographicCamera();
gamePort = new ScreenViewport(gamecam);
}
#Override
public void show() {
}
#Override
public void render(float delta) {
game.batch.setProjectionMatrix(gamecam.combined);
game.batch.begin();
game.batch.draw(texture,0,0);
game.batch.end();
}
#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() {
}
}
and this is the SpaceInvader.java
package com.waflesgames.spaceinvader;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.waflesgames.spaceinvader.Screens.PlayScreen;
public class SpaceInvader extends Game {
public SpriteBatch batch;
#Override
public void create () {
batch = new SpriteBatch();
setScreen(new PlayScreen(this));
}
#Override
public void render () {
super.render();
}
}
you have error here:
public PlayScreen(SpaceInvader Game){
this.game = game;
...
it should be game instead of Game - you are get SpaceInvader instance to Game variable so below you are assigning the this.game to itself what causes nullpointer exception in render method (game is null so you cannot acces its batch)

Exception in thread "LWJGL Application" youtube tut with link to video

i been following this tutorial : http://www.youtube.com/watch?v=LSblkR4K1LU and
i have a problem everytime i run this background image here is what comes up in the console
if someone can help me out... i dont know if im missing something or what it is. i thought it was the image that couldnt be found but i tried to fix it by making the file again and making sure i typed everything correctly.
error i get:
`Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Texture width and height must be powers of two: 1920x1080
at com.badlogic.gdx.graphics.GLTexture.uploadImageData(GLTexture.java:241)
at com.badlogic.gdx.graphics.Texture.load(Texture.java:145)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:133)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:112)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:104)
at com.universal.game.Assets.load(Assets.java:15)
at com.universal.game.MyGame.create(MyGame.java:11)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)`
GameScreen.java
package com.universal.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class GameScreen implements Screen {
MyGame game;
OrthographicCamera camera;
SpriteBatch batch;
public GameScreen(MyGame game){
this.game = game;
camera = new OrthographicCamera();
camera.setToOrtho(false,1920,1080);
batch = new SpriteBatch();
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1F, 1F, 1F, 1F);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
}
#Override
public void show() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
#Override
public void resize(int width, int height) {
}
#Override
public void hide() {
}
}
MyGame.java
package com.universal.game;
import com.badlogic.gdx.Game;
public class MyGame extends Game{
public GameScreen game_screen;
#Override
public void create() {
Assets.load();
game_screen = new GameScreen(this);
setScreen(game_screen);
}
}
Assets.java
package com.universal.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
public class Assets {
public static Texture texture_back;
public static Sprite sprite_back;
public static void load(){
texture_back = new Texture(Gdx.files.internal("menu/back.png"));
texture_back.setFilter(TextureFilter.Linear, TextureFilter.Linear);
sprite_back = new Sprite(texture_back);
sprite_back.flip(false, true);
}
}
Main.java
package com.universal.game;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
public class Main {
public static void main(String[] args) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "Z-Angel";
cfg.useGL20 = true;
cfg.width = 480;
cfg.height = 320;
new LwjglApplication(new MyGame(), cfg);
}
}
"Texture width and height must be powers of two"
In GameScreen.java,
camera.setToOrtho(false,1920,1080);
The 1080 and 1920 is not a legitimate value. You must use one of these values: 1024, 2048, 4096

libgdx rendering a spite

I am trying to render a simple texture with 4 different color squares.There is no error in my syntax.
Exception:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.bracco.thrive.ThriveGame.create(ThriveGame.java:21)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:132)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:112)
Code
package com.bracco.thrive;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.Game;
import com.bracco.thrive.world.WorldGeneration;
public class ThriveGame extends Game {
WorldGeneration wGenerate;
#Override
public void create() {
wGenerate.createWorld();
}
#Override
public void dispose() {
}
#Override
public void render() {
wGenerate.render();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
package com.bracco.thrive.world;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
public class WorldGeneration {
private SpriteBatch spriteBatch;
private int textureWidth,textureHeight;
private boolean debug = false;
private Texture texture;
public void createWorld(){
spriteBatch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("data/textures/basictextures.png"));
}
public void render(){
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
spriteBatch.begin();
drawTexture();
spriteBatch.end();
}
private void drawTexture()
{
spriteBatch.draw(texture, 0, 16, 0, 16, 16, 16, 1, 1);
}
}
The object of class WorldGeneration is not made.
WorldGeneration wGenerate;
#Override
public void create() {
wGenerate=new WorldGeneration();
wGenerate.createWorld();
}

Categories

Resources