Error with libGdx Screens - java

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)

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"));
...
...
}

LibGDX - what do the console errors mean and how can I fix this console error?

Heres the console error I'm getting:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.dakotapederson.slingshotsteve.SlingshotSteve.render(SlingshotSteve.java:30)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
And here is the code:
package com.dakotapederson.slingshotsteve;
import com.badlogic.gdx.ApplicationListener;
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.graphics.g2d.TextureRegion;
public class SlingshotSteve implements ApplicationListener {
// Creates our 2D images
private SpriteBatch batch;
private TextureRegion backgroundTexture;
private Texture texture;
#Override
public void create() {
Texture texture = new Texture(Gdx.files.internal("background.jpg"));
backgroundTexture = new TextureRegion(texture, 20, 20, 50, 50);
}
#Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(backgroundTexture, 0, 0);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
batch.dispose();
texture.dispose();
}
}
What do the console errors exactly mean? Because if theirs an error in the code I need to know what it is. Also, since I'm new at this, please describe the error in the code with as much clarity as possible.
You should read basic tutorial: https://github.com/libgdx/libgdx/wiki/A-simple-game
You are missing this line in create():
batch = new SpriteBatch();

Libgdx Desktop Launcher blank

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.

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