LibGDX Sprite Screens not loading graphics, or loading up screens - java

I have this issue in my LibGDX project, I essentially can't load up my gamescreen, I am following a YouTube tutorial (link to particular tutorial I'm on: https://www.youtube.com/watch?v=LSblkR4K1LU), and I have found that my sprite that is supposed to go on my screen, just it won't load up at all, it just opens, closes then pops this up: http://puu.sh/coxUv/d877d08a83.png, I will admit the only thing I have changed from the video was this:
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1F, 1F, 1F, 1F);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
I changed GL10.GL_COLOR_BUFFER_BIT (that was in the video), to GL20.GL_COLOR_BUFFER_BIT, because it gave me an error: GL10 cannot be resolved to a variable, does anyone have an idea why, tell me if there is any needed information.
Thanks in advance.
EDIT:
Gamescreen Class:
package com.edac.unforgivingunderground;
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.g2d.SpriteBatch;
public class GameScreen implements Screen{
UnforgivingUnderground game;
OrthographicCamera camera;
SpriteBatch batch;
public GameScreen(UnforgivingUnderground 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 resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void show() {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
Assets Class:
package com.edac.unforgivingunderground;
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("logo"));
texture_back.setFilter(TextureFilter.Linear, TextureFilter.Linear);
sprite_back = new Sprite(texture_back);
sprite_back.flip(false, true);
}
}
Desktop Launcher:
package com.edac.unforgivingunderground.desktop;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.edac.unforgivingunderground.UnforgivingUnderground;
public class DesktopLauncher {
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
new LwjglApplication(new UnforgivingUnderground(), config);
}
}
Main Class:
package com.edac.unforgivingunderground;
import com.badlogic.gdx.Game;
public class UnforgivingUnderground extends Game{
public GameScreen game_screen;
#Override
public void create() {
Assets.load();
game_screen = new GameScreen(this);
setScreen(game_screen);
}

Not sure why it crashed (you should post the stack dump) but possibly it's because this line: Gdx.files.internal("logo"). Maybe you mean logo.png or logo.jpg. Also make sure that the image file does really exist on your assets folder.
Also, I belive your render method is missing one line.:
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1F, 1F, 1F, 1F);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
Assets.sprite_back.draw(batch);
}
That way you'll render your sprite to the screen through the SpriteBatch. In your code you can see that you're not using it.

Related

libGDX actor draw() not called

Please forgive me for my english.
Began to explore libGDX and have a problem. When I add actor on stage, method draw() not called.
Tried to apply the method to draw a straight line, texture successfully drawn but it is not an actor, and this method is not correct.
Help please.
SpiderHunt.java
package com.spiderhunt;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.spiderhunt.screens.MainMenu;
public class SpiderHunt extends Game{
private SpriteBatch batch;
public MainMenu mainMenu;
private static SpiderHunt instance = new SpiderHunt();
public SpiderHunt(){
}
public static SpiderHunt getInstance() {
return instance;
}
public void create () {
//load textures
Assets.load();
batch = new SpriteBatch();
mainMenu = new MainMenu(batch);
this.setScreen(mainMenu);
}
public void showMainMenu(){
setScreen(mainMenu);
}
public void render (float delta) {
}
public void resize(int width, int height) {
}
public void pause() {
}
public void resume() {
}
public void dispose() {
}
}
MainMenu.java
package com.spiderhunt.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.spiderhunt.Assets;
import com.spiderhunt.buttons.btnPlay;
public class MainMenu implements Screen {
public btnPlay playButton;
public Stage stage;
public SpriteBatch batch;
class GoToGameListener extends ClickListener {
#Override
public void clicked(InputEvent event, float x, float y) {
//some code for click or push
}
}
public MainMenu(SpriteBatch batch_1) {
batch = batch_1;
stage = new Stage(new StretchViewport( Gdx.graphics.getWidth(), Gdx.graphics.getHeight()), batch);
Gdx.input.setInputProcessor(stage);
playButton = new btnPlay(); //make actor
stage.addActor(playButton);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//make background
batch.begin();
batch.draw(Assets.bgMenuRegion, 0, 0, 540, 960);
batch.end();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw(); //this action must do method draw() from actor but it not called !!!!!!!!!!
//batch.begin();
//playButton.draw(batch, 0); THIS CODE DRAW BUTTON, BUT IT NOT CORRECTLY ACTOR
//batch.end();
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void show() {
Gdx.input.setInputProcessor(stage);
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
}
btnPlay.java
package com.spiderhunt.buttons;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.spiderhunt.Assets;
public class btnPlay extends Actor {
public btnPlay(){
setSize(100, 40);
setPosition(100, 100);
}
public void draw(SpriteBatch batch, float parentAlpha) {
//!!!!!!!!!!!!!!!Error in this place. Draw() not called from stage
batch.setColor(getColor());
batch.draw(Assets.btnPlayRegion, 0, 0);
}
}
Assets.java
package com.spiderhunt;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
public class Assets {
public static Texture atlas;
//backgrounds
public static TextureRegion bgMenuRegion;
public static TextureRegion bgSelectLevelRegion;
//buttons
public static TextureRegion btnPlayRegion;
//objects
public static TextureRegion objFlyRegion;
public static void load(){
atlas = new Texture("atlas.png");
bgMenuRegion = new TextureRegion(atlas, 0, 0, 540, 960);
btnPlayRegion = new TextureRegion(atlas, 1111, 1244, 418, 112);
}
}
Thanks for the help.
And now i find my bug.
I replace SpriteBatch to Batch and it work.
change
public void draw(SpriteBatch batch, float parentAlpha) {
//!!!!!!!!!!!!!!!Error in this place. Draw() not called from stage
batch.setColor(getColor());
batch.draw(Assets.btnPlayRegion, 0, 0);
}
to
#Override
public void draw(Batch batch, float parentAlpha) {
//!!!!!!!!!!!!!!!Error in this place. Draw() not called from stage
batch.setColor(getColor());
batch.draw(Assets.btnPlayRegion, 0, 0);
}
The problem I believe is at this line:
new Stage(new StretchViewport( Gdx.graphics.getWidth(), Gdx.graphics.getHeight()), batch);
Remove batch from the Stage Initialization change it to this:
new Stage(new StretchViewport( Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
You are passing the batch from the Screen to the stage and you are calling batch.end() before stage.draw(). However stage has its own batch so you do not have to pass a batch for the stage to draw. If for your own reasons (e.g. batch projection matrix configuration) you still want your Screen batch passed to the stage do not call batch.end() before stage.draw() since your batch of the stage and your main batch are the same. Call batch.end() after stage.draw()

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();

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 MainMenuScreen

I am Trying to learn to use the libgdx framework. Like the tutorial (https://code.google.com/p/libgdx/wiki/ExtendedSimpleApp) on the website of libgdx describes, i tried to set up a little main menu. I have imported all classes that are needed, but at line 29 there is always the error: "MainMenuScreen can not be resolved to a type".
Here is the source code:
package com.me.mygdxgame;
import java.awt.SplashScreen;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Game;
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.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class MyGdxGame implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
private BitmapFont font;
#Override
public void create() {
batch = new SpriteBatch();
//Use LibGDX's default Arial font.
font = new BitmapFont();
this.setScreen(new MainMenuScreen(this));
}
#Override
public void dispose() {
batch.dispose();
texture.dispose();
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
MainMenuScreen is not a standard-class, if you read further in the tutorial you have linked you see that they created an additional class "MainMenuScreen", just create another class named "MainMenuScreen" and add the code from the tutorial there.
If this is your first time with java you might want to google some java tutorials to the learn the fundementals of java and oop first before you use some external libraries ;)
you need to make a class which extends game class of libgdx.
public class MainClass extends Game
then in your create methodof mainclass you have to set the screen.
setScreen(new MainMenuScreen(this));
and in your mainmenuclass you have to implement screen class of libgdx.
public class MainMenuScreen implements Screen
and you have to make a constructor like this
public MainMenuScreen(Game game) {
// TODO Auto-generated constructor stub
this.game = game;
camera = new OrthographicCamera();
camera.setToOrtho(false, scrw, scrh);//scrw is your screen width,scrh is screen height
camera.update();
batch = new SpriteBatch();
Gdx.input.setInputProcessor(MainMenuScreen.this);}
i think this will work ...
To start learning libGDX check out this project: https://github.com/edesdan/libgdx-playground. Maybe it could help you to do the first steps into this world.
Your basic concept is not clear.
To use different screens in a single application, you have to extend the Game class.
public class MyGdxGame extends Game implements ApplicationListener{}
To choose a different screen, call:
setScreen(new XYZScreen(this));
this refer to object of MyGdxGame.
Now you have to make a new class of XYZ name. Then do whatever you want on your new screen.
if you want use of class Screen you should extend mainclass from class Game
if you use screen u can't use applicationlistener no more because in Screen you have all application Listener functions overrided ...
all you have to do is move your code in application listener to "MainMenuScreen"
then you set the screen when creating class Mainclass to "MainMenuScreen"
public class MainClass extends Game {
#Override
public void create() {
setScreen(new MainMenuScreen());
}}
I've actually used Screens in a small project that I've done, here's the code for one of my screens.
package com.sample.Main;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Mainmenu implements Screen {
MainGame game;
SpriteBatch batch;
Mainmenu(MainGame g){
game = g;
batch = g.batch;
}
#Override
public void show() {
}
#Override
public void render(float delta) {
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
}
}
And here for the main game code that runs the screen:
package com.sample.Main;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class MainGame extends Game {
SpriteBatch batch;
Screen mainscreen;
#Override
public void create () {
batch = new SpriteBatch();
mainscreen = new Mainmenu(this);
setScreen(mainscreen);
}
#Override
public void render () {
super.render();
}
#Override
public void dispose () {
batch.dispose();
}
}

NullPointerException with libgdx using the gdx render() method

Started making a game.
Here's some of my code.
package games.tribe.screens;
import games.tribe.model.World;
import games.tribe.view.WorldRenderer;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
public class GameScreen implements Screen {
private World world;
private WorldRenderer renderer;
/** This was the bit I'd missed --------------------------------------**/
#Override
public void show() {
world = new World();
renderer = new WorldRenderer(world);
}
/**------------------------------------------------------------------**/
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
renderer.render();
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
}
here's the WorldRenderer Class:
package games.tribe.view;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import games.tribe.model.Block;
import games.tribe.model.TribalHunter;
import games.tribe.model.World;
public class WorldRenderer {
private World world;
private OrthographicCamera cam;
/**for debug rendering**/
ShapeRenderer debugRenderer = new ShapeRenderer();
public WorldRenderer(World world) {
this.world = world;
this.cam = new OrthographicCamera(10, 7);
this.cam.position.set(5, 3.5f, 0);
this.cam.update();
}
public void render() {
//render blocks
debugRenderer.setProjectionMatrix(cam.combined);
debugRenderer.begin(ShapeType.Rectangle);
for(Block block : world.getBlocks()) {
Rectangle rect = block.getBounds();
float x1 = block.getPosition().x + rect.x;
float y1 = block.getPosition().y + rect.y;
debugRenderer.setColor(new Color(1, 0, 0, 1));
debugRenderer.rect(x1, y1, rect.width, rect.height);
}
//render hunter
TribalHunter hunter = world.getHunter();
Rectangle rect = hunter.getBounds();
float x1 = hunter.getPosition().x + rect.x;
float y1 = hunter.getPosition().y + rect.y;
debugRenderer.setColor(new Color(0, 1, 0, 1));
debugRenderer.rect(x1, y1, rect.width, rect.height);
debugRenderer.end();
}
}
This is the exception I'm getting when I run it as a desktop application:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at games.tribe.screens.GameScreen.render(GameScreen.java:19)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:202)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:131)
AL lib: ReleaseALC: 1 device not closed
Line 46 of gdx.Game.render is this method:
#Override
public void render () {
if (screen != null) screen.render(Gdx.graphics.getDeltaTime());
}
Any help will be appreciated
Thanks in advance
In the GameScreen's render() method, has the renderer been initialized? That could be causing the problem if it hasn't.
Edit: The problem you're having, according to the top two lines of the error, is a NullPointerException on line 19 of the class GameScreen. The NullPointerException only occurs when an object is used for some action when the object itself is null because it likely hasn't been initialized.
Line 19 of the GameScreen is:
renderer.render();
...but the object renderer has not been initialized anywhere, so it is currently null which is the default. To avoid getting this error, you'll need to initialize the renderer object before you run that line of code. Perhaps with something like this:
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
renderer = new WorldRenderer();
renderer.render();
}
I am not familiar with libgdx, so I can't be sure that's exactly how a WorldRenderer is initialized, but you need to do something of the sort. I hope this helps.

Categories

Resources