libGDX actor draw() not called - java

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

Related

LibGDX Fade Out / Fade In Screen Transition

I'm trying to get a nice fade in/ fade out between two of my screens, and after hours of searching online I have come up empty handed. I've attempted various solutions involving Actions to no avail, and I am not to keen on using TweenEngine, but I would appreciate any help!
Below is the closest solution i've found. This one simply delay the time before the screens switch, yet you don't see a fade in any way.
package com.aidanstrong.game;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.ColorAction;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Timer;
public class FadingGame extends Game {
GameScreen gameScreen;
UpgradeScreen upgradeScreen;
private Actor fadeActor = new Actor();
private ShapeRenderer fadeRenderer;
#Override
public void create() {
gameScreen = new GameScreen(this);
upgradeScreen = new UpgradeScreen(this);
setScreen(gameScreen);
fadeRenderer = new ShapeRenderer(8);
}
public void setScreenWithFade (final Screen screen, float duration) {
fadeActor.clearActions();
fadeActor.setColor(Color.CLEAR);
fadeActor.addAction(Actions.sequence(
Actions.color(Color.BLACK, duration/2f, Interpolation.fade),
Actions.run(new Runnable(){public void run(){setScreen(screen);}}),
Actions.color(Color.CLEAR, duration/2f, Interpolation.fade)
));
}
#Override
public void render (){
super.render();
fadeActor.act(Gdx.graphics.getDeltaTime());
float alpha = fadeActor.getColor().a;
if (alpha != 0){
fadeRenderer.begin(ShapeRenderer.ShapeType.Filled);
fadeRenderer.setColor(0, 0, 0, alpha);
fadeRenderer.rect(-1, -1, 2, 2); //full screen rect w/ identity matrix
fadeRenderer.end();
}
}
}
Add topLayer as an Image on your stage and add Action on that Actor. Try this test case:
public class GdxTest extends Game {
FirstScreen firstScreen;
SecondScreen secondScreen;
#Override
public void create() {
firstScreen=new FirstScreen();
secondScreen=new SecondScreen();
setScreen(firstScreen);
}
public static Texture getTexture(){
Pixmap pixmap;
try {
pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
}catch (GdxRuntimeException e)
{
pixmap=new Pixmap(1,1, Pixmap.Format.RGB565);
}
pixmap.setColor(Color.WHITE);
pixmap.drawRectangle(0,0,1,1);
return new Texture(pixmap);
}
}
FirstScreen
public class FirstScreen extends ScreenAdapter {
Stage stage;
Texture texture,white;
#Override
public void show() {
stage=new Stage();
final Image image = new Image(texture=new Texture("badlogic.jpg"));
image.setSize(200, 200);
image.setPosition(stage.getWidth()/2, stage.getHeight()/2, Align.center);
stage.addActor(image);
final Image topLayer = new Image(new TextureRegion(white=GdxTest.getTexture()));
topLayer.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
topLayer.setColor(Color.BLACK);
stage.addActor(topLayer);
stage.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
topLayer.addAction(Actions.sequence(Actions.color(Color.BLACK,2),Actions.run(new Runnable() {
#Override
public void run() {
GdxTest gdxTest=((GdxTest)Gdx.app.getApplicationListener());
gdxTest.setScreen(gdxTest.secondScreen);
}
})));
super.clicked(event, x, y);
}
});
topLayer.addAction(Actions.fadeOut(2));
Gdx.input.setInputProcessor(stage);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
#Override
public void hide() {
dispose();
}
#Override
public void dispose() {
stage.dispose();
texture.dispose();
white.dispose();
}
}
SecondScreen
public class SecondScreen extends ScreenAdapter{
Stage stage;
Texture texture,white;
#Override
public void show() {
stage=new Stage();
final Image image = new Image(texture=new Texture("badlogic.jpg"));
image.setSize(200, 200);
image.setPosition(stage.getWidth() / 2, stage.getHeight() / 2, Align.center);
image.setOrigin(Align.center);
image.rotateBy(90);
stage.addActor(image);
final Image topLayer = new Image(new TextureRegion(white=GdxTest.getTexture()));
topLayer.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
topLayer.setColor(Color.BLACK);
stage.addActor(topLayer);
topLayer.addAction(Actions.fadeOut(2));
Gdx.input.setInputProcessor(stage);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0,0,0,0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();
stage.act();
}
#Override
public void hide() {
dispose();
}
#Override
public void dispose() {
stage.dispose();
texture.dispose();
white.dispose();
}
}
Or
You can use FBO, at the top of your stage.
EDIT
public class GdxTest extends ApplicationAdapter {
Stage stage;
FrameBuffer frameBuffer;
float delta;
#Override
public void create() {
stage=new Stage();
{
final Image image = new Image(new Texture("badlogic.jpg"));
image.setSize(200, 200);
image.setPosition(stage.getWidth() / 2, stage.getHeight() / 2, Align.center);
stage.addActor(image);
}
{
final Image image = new Image(new Texture("badlogic.jpg"));
image.setSize(200, 200);
image.setPosition(stage.getWidth()/3, stage.getHeight()/3, Align.center);
stage.addActor(image);
}
{
final Image image = new Image(new Texture("badlogic.jpg"));
image.setSize(200, 200);
image.setPosition(stage.getWidth()*.75f, stage.getHeight()*.75f, Align.center);
stage.addActor(image);
}
Gdx.input.setInputProcessor(stage);
delta=1;
}
#Override
public void render() {
if(delta>0)delta-=.01f;
frameBuffer.begin();
Gdx.gl.glClearColor(0,0,0,delta);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
frameBuffer.end();
stage.act();
stage.draw();
stage.getBatch().setProjectionMatrix(stage.getBatch().getProjectionMatrix().idt());
stage.getBatch().begin();
stage.getBatch().draw(frameBuffer.getColorBufferTexture(),-1,1,2,-2);
stage.getBatch().end();
}
#Override
public void dispose() {
stage.dispose();
frameBuffer.dispose();
}
#Override
public void resize(int width, int height) {
if(frameBuffer !=null && (frameBuffer.getWidth()!=width || frameBuffer.getHeight()!=height )) {
frameBuffer.dispose();
frameBuffer=null;
}
if(frameBuffer==null){
try {
frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, width, height, false);
}catch (GdxRuntimeException e){
frameBuffer=new FrameBuffer(Pixmap.Format.RGB565,width,height,false);
}
}
}
}

LibGDX basic stage draw error

I'm trying to create a game, but my code keeps giving me error:
Exception in thread "LWJGL Application" java.lang.IllegalArgumentException: no uniform with name 'u_projModelView' in shader
at com.badlogic.gdx.graphics.glutils.ShaderProgram.fetchUniformLocation(ShaderProgram.java:287)
at com.badlogic.gdx.graphics.glutils.ShaderProgram.fetchUniformLocation(ShaderProgram.java:277)
at com.badlogic.gdx.graphics.glutils.ShaderProgram.setUniformMatrix(ShaderProgram.java:507)
at com.badlogic.gdx.graphics.glutils.ShaderProgram.setUniformMatrix(ShaderProgram.java:498)
at com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer20.flush(ImmediateModeRenderer20.java:147)
at com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer20.end(ImmediateModeRenderer20.java:160)
at com.badlogic.gdx.graphics.glutils.ShapeRenderer.end(ShapeRenderer.java:1104)
at com.badlogic.gdx.graphics.glutils.ShapeRenderer.check(ShapeRenderer.java:1092)
at com.badlogic.gdx.graphics.glutils.ShapeRenderer.rect(ShapeRenderer.java:389)
at com.badlogic.gdx.scenes.scene2d.ui.Table.drawDebugRects(Table.java:1224)
at com.badlogic.gdx.scenes.scene2d.ui.Table.drawDebug(Table.java:1204)
at com.badlogic.gdx.scenes.scene2d.Group.drawDebugChildren(Group.java:156)
at com.badlogic.gdx.scenes.scene2d.Group.drawDebug(Group.java:139)
at com.badlogic.gdx.scenes.scene2d.Stage.drawDebug(Stage.java:169)
at com.badlogic.gdx.scenes.scene2d.Stage.draw(Stage.java:132)
at net.lukshe.lukshegame2.screens.MainMenu.render(MainMenu.java:71)
at com.badlogic.gdx.Game.render(Game.java:46)
at net.lukshe.lukshegame2.LuksheGame.render(LuksheGame.java:23)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
MainMenu.java:
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
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.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
public class MainMenu implements Screen {
private Stage stage; //done
private TextureAtlas atlas; //done
private Skin skin; //done
private Table table; //done
private TextButton buttonplay, buttonexit;
private BitmapFont white; //done
private Label heading;
#Override
public void show() {
stage = new Stage();
atlas = new TextureAtlas("ui/button.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);
//Set the button style
TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.up = skin.getDrawable("buttonup");
textButtonStyle.down = skin.getDrawable("buttondown");
textButtonStyle.pressedOffsetX = 1;
textButtonStyle.pressedOffsetY = -1;
textButtonStyle.font = white;
textButtonStyle.fontColor = Color.BLACK;
//Creating exit button
buttonexit = new TextButton("Exit", textButtonStyle);
buttonexit.pad(20);
//Add button to the table
table.add(buttonexit);
//Debug
table.debug();
//Add table to the stage
stage.addActor(table);
}
#Override
public void render(float delta) {
//OpenGL settings
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
//Render at the speed of delta
stage.act(delta);
//Draw stage
stage.draw();
}
LuksheGame.java:
public class LuksheGame extends Game {
public static final String NAME = "Lukse game (LibGDX)", VERSION = "Pre-Alpha 0.0.0.2";
#Override
public void create() {
setScreen(new Splash());
}
#Override
public void dispose() {
super.dispose();
}
#Override
public void render() {
super.render();
}
#Override
public void resize(int width, int height) {
super.resize(width, height);
}
#Override
public void pause() {
super.pause();
}
#Override
public void resume() {
super.resume();
}
}
If someone can help me, please help.
EDIT:
MainMenu.java is called from Splash.java. In Splash class I'm using Tween engine to increase and decrease opacity of the splash screen.
Splash.java:
public class Splash implements Screen {
private SpriteBatch batch;
private Sprite splash;
private TweenManager twm;
#Override
public void show() {
batch = new SpriteBatch();
twm = new TweenManager();
Tween.registerAccessor(Sprite.class, new SpriteAccessor());
Texture splashtexture = new Texture(Gdx.files.internal("images/splash.png"));
splash = new Sprite(splashtexture);
splash.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Tween.set(splash, SpriteAccessor.alpha).target(0).start(twm);
Tween.to(splash, SpriteAccessor.alpha, 2).target(1).repeatYoyo(1, 2).setCallback(new TweenCallback() {
#Override
public void onEvent(int type, BaseTween<?> source) {
((Game) Gdx.app.getApplicationListener()).setScreen(new MainMenu());
}
}).start(twm);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
twm.update(delta);
batch.begin();
splash.draw(batch);
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() {
batch.dispose();
splash.getTexture().dispose();
}
}

LibGDX Sprite Screens not loading graphics, or loading up screens

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.

Sprite flicker while moving libgdx

This code makes image Border flicker(Flash) while moving left,right,down or up. Why image border flash wile moving even if I use Screen class render() method delta value.
package com.me.mygdxgame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
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.math.Vector3;
public class MoveSpriteExample extends GdxTest implements InputProcessor {
Texture texture;
SpriteBatch batch;
OrthographicCamera camera;
Vector3 spritePosition = new Vector3();
Sprite sprite;
public void resize (int width, int height) {
}
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
batch = new SpriteBatch();
camera= new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.setToOrtho(false, w, h);
texture = new Texture(Gdx.files.internal("data/grasswall.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
sprite = new Sprite(texture);
sprite.setSize(32, 32);
spritePosition.y=100;
sprite.setPosition(spritePosition.x,spritePosition.x);
lastUpdateTime = System.nanoTime();
}
public void Update(float Delta)
{
if (Gdx.input.isKeyPressed(Keys.D)==true)
{
spritePosition.x += 150*(Delta / 1000000000.0);
}else if (Gdx.input.isKeyPressed( Keys.A)==true)
{
spritePosition.x -= 150*(Delta / 1000000000.0);
}
else if (Gdx.input.isKeyPressed( Keys.Z)==true)
{
spritePosition.y -= 150*(Delta / 1000000000.0);
}
else if (Gdx.input.isKeyPressed( Keys.W)==true)
{
spritePosition.y += 150*(Delta / 1000000000.0);
}
}
float lastUpdateTime;
float currentTime;
public void render() {
currentTime = System.nanoTime();
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
Update(currentTime - lastUpdateTime);
sprite.setPosition(spritePosition.x,spritePosition.y);
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.end();
lastUpdateTime = currentTime;
}
}
pls provide code with sample
As i said in the last post of you, you should take a look at the Tutorial!
First of all you do not need to calculate the delta time yourself. I already postet how you get it in the last post for you.
Ill give you a super short example with a moving sprite without flickering.
At first here is the ApplicationListener. (not a GDXtest)
public class MainClass implements ApplicationListener {
private Screen currentScreen = null;
#Override
public void create() {
Texture.setEnforcePotImages(false);
this.currentScreen = new TestScreen();
}
#Override
public void dispose() {
this.currentScreen.dispose();
}
#Override
public void render() {
this.currentScreen.render(Gdx.graphics.getDeltaTime());
}
#Override
public void resize(int width, int height) {
this.currentScreen.resize(width, height);
}
#Override
public void pause() {
this.currentScreen.pause();
}
#Override
public void resume() {
this.currentScreen.resume();
;
}
}
It's preaty simple and as you can see it does call the render of the screen automatically with the delta time! this.currentScreen.render(Gdx.graphics.getDeltaTime()).
The next thing you need is a simple Screen. So something that does implement the Screeninterface. Id really recommand that you use a Scene2D setup with a stage. But here is an example without.
public class TestScreen implements Screen {
private Sprite mySprite;
private SpriteBatch batch = new SpriteBatch();
public TestScreen() {
this.mySprite = new Sprite(new Texture(
Gdx.files.internal("data/appicon.png")));
this.mySprite.setPosition(50f, 50f);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
this.update(delta);
this.batch.begin();
this.mySprite.draw(batch);
this.batch.end();
}
public void update(float delta) {
if (Gdx.input.isKeyPressed(Keys.D) == true) {
mySprite.setX(mySprite.getX() + 150 * delta);
} else if (Gdx.input.isKeyPressed(Keys.A) == true) {
mySprite.setX(mySprite.getX() - 150 * delta);
} else if (Gdx.input.isKeyPressed(Keys.Z) == true) {
mySprite.setY(mySprite.getY() - 150 * delta);
} else if (Gdx.input.isKeyPressed(Keys.W) == true) {
mySprite.setY(mySprite.getY() + 150 * delta);
}
}
#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() {
}
}
Regardas and do take a look at the tutorial. (Here is again the beginning of it)

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