I'm using LibGDX for the first time and I'm getting a NullPointerException when trying to implement an InputAdapter into the multiplexer. The message seems to be less than helpful but I'm fairly rusty with my Java. I have tried using a regular class that implements the InputProcessor interface and I've attempted to log any thing that would throw a Null but I haven't been able to locate the cause.
Here's my code
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
public class Game implements ApplicationListener {
public PerspectiveCamera cam;
public Model model;
public ModelInstance instance;
public ModelBatch modelBatch;
public Environment environment;
public InputMultiplexer input;
public static int gWidth = 800;
public static int gHeight = 600;
#Override
public void create() {
this.modelBatch = new ModelBatch();
this.environment = new Environment();
this.environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
this.environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
/**
* Add input adapters.
*/
this.cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
this.cam.position.set(10f, 20f, 0);
this.cam.lookAt(0, 0, 0);
this.cam.rotate(67f, 0f, 0f, 0f);
this.cam.near = 1f;
this.cam.far = 300f;
this.input = new InputMultiplexer(Gdx.input.getInputProcessor());
this.input.addProcessor(new MouseCamController());
Gdx.input.setInputProcessor(this.input);
this.cam.update();
ModelBuilder modelBuilder = new ModelBuilder();
model = modelBuilder.createBox(5f, 5f, 5f,
new Material(ColorAttribute.createDiffuse(Color.GREEN)),
Usage.Position | Usage.Normal);
instance = new ModelInstance(model);
}
#Override
public void resize(int width, int height) {
}
#Override
public void render() {
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// if (this.cam.fieldOfView < 120) {
// this.cam.fieldOfView += 10;
// }
this.cam.update();
this.modelBatch.begin(this.cam);
this.modelBatch.render(instance, environment);
this.modelBatch.end();
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
this.modelBatch.dispose();
this.model.dispose();
}
public class MouseCamController extends InputAdapter {
#Override
public boolean scrolled(int amount) {
// Gdx.app.log("INPUT", "A: " + amount);
return true;
}
}
}
And here are the exception details:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.InputMultiplexer.mouseMoved(InputMultiplexer.java:107)
at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:303)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:200)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
The issue was getting the current input processor. Since there was none it did actually hit a NULL when it started going through each adapter. Stupid problem, simple answer. Problem solved and available for posterity.
The problem arises when InputMultiplexer reaches a null InputProcessor in it's list. I've tried replicating it by adding the null processor after an InputProcessor which returns, and the error does not occur.
Simply remove any null InputProcessor from the InputMultiplexer and it should work fine. If you are unsure which one is null, try listing all the processors.
InputMultiplexer plexer;
// Add processors to plexer...
System.out.println(plexer.getProcessors().toString());
Related
I am trying to put a button using a play image. I have followed some tutorials and cannot find the documentation to complete my project. The game is actually done, and I just need to connect the gameover screen to the game screen so I can replay the game by pushing the play image. I got stuck in this longer than I expected. I cannot change the methods because I will mess up my code. Code and images below.
package com.mygdx.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.viewport.FitViewport;
public class GameOverScreen extends BaseScreen {
private Image background;
private Stage stage;
private Image gameOver;
private Image replay;
public GameOverScreen(final MainGame game) {
super(game);
stage = new Stage(new FitViewport(640, 320));
//Preparing actors
background = new Image(game.getManager().get("bg.png", Texture.class));
gameOver = new Image(game.getManager().get("gameover.png", Texture.class));
replay = new Image(game.getManager().get("plybtn.png", Texture.class));
replay.addCaptureListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
game.setScreen(game.gameScreen);
}
});
//Giving their positions and sizes
background.setPosition(0,0);
gameOver.setPosition(320 - gameOver.getWidth()/2, 180 - gameOver.getHeight()/2);
replay.setPosition(320 - replay.getWidth()/2, 60 -replay.getHeight()/2);
//Set them in stage
stage.addActor(background);
stage.addActor(gameOver);
stage.addActor(replay);
}
#Override
public void show() {
Gdx.input.setInputProcessor(stage);
}
#Override
public void hide() {
Gdx.input.setInputProcessor(null);
}
#Override
public void dispose() {
stage.dispose();
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0.4f, 0.5f, 0.8f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
}
[This is the screen that I get, it does not change the screen when I touch the button][1]
[1]: https://i.stack.imgur.com/GxVLM.png
Update:
This is the solution to my problem in case you need it.
// Declare statements at the beginning of the class
private Texture myTexture;
private TextureRegion myTextureRegion;
private TextureRegionDrawable myTexRegionDrawable;
private ImageButton pbutton;
// Make a texture out of the image in the MenuScreen constructor
myTexture = new Texture(Gdx.files.internal("plybtn.png"));
myTextureRegion = new TextureRegion(myTexture);
myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
pbutton = new ImageButton(myTexRegionDrawable);
The event doesn't look like its being received. Change
replay.addCaptureListener(new ChangeListener() {
to
replay.addListener(new ChangeListener() {
or even use ClickListener instead. CaptureListener is to intercept events to disable them, not for handling and you need to return true in the capture handler to not break event handling down the tree.
https://gamedev.stackexchange.com/questions/151505/what-is-the-difference-between-a-listener-and-a-capturelistener-in-libgdx-scene2
I am trying to start my game from a Main Menu class that implements NiftyGUI. I am using OpenGL (LWJGL) and Java. The problem is that I am never able to get the main menu to disappear and then start the game class, which is World.java.
The following is what I would usually do when I have no main menu.
package com.dev.voxy;
import com.dev.voxy.utilities.GLScreen;
import com.dev.voxy.world.World;
public class Main extends GLScreen {
public static int WIDTH = 1280;
public static int HEIGHT = 720;
private World world;
public static void main(String[] args) {
Main main = new Main();
main.GLScreen(WIDTH, HEIGHT, false, 60, "Voxy");
}
#Override
public void init() {
world = new World();
}
#Override
public void update() {
world.update();
}
#Override
public void dispose() {
world.dispose();
}
That is my main class and extends GLScreen
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.gluPerspective;
public abstract class GLScreen {
protected static int SCREEN_WIDTH;
protected static int SCREEN_HEIGHT;
protected boolean FULL_SCREEN;
protected static int FPS = 60;
protected static String SCREEN_TITLE;
private static long lastFPS;
private static int lfps;
public static int fps;
public void GLScreen(int width, int height, boolean fullscreen, int sync, String title) {
this.SCREEN_HEIGHT = height;
this.SCREEN_WIDTH = width;
this.FULL_SCREEN = fullscreen;
this.FPS = sync;
this.SCREEN_TITLE = title;
InitScreen();
}
public void InitScreen() {
createWindow();
InitGL();
init();
Run();
}
void createWindow() {
try {
Display.setFullscreen(FULL_SCREEN);
Display.setTitle(SCREEN_TITLE);
DisplayMode displayMode = new DisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT);
Display.setDisplayMode(displayMode);
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
}
void InitGL(){
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glViewport(0, 0, Display.getWidth(), Display.getHeight());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(67.0f, SCREEN_WIDTH / SCREEN_HEIGHT, 0.001f, 1000f); //TODO watch coding universe video
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
lastFPS = getTime();
}
public abstract void init();
public abstract void update();
public static int getFPS() {
int cfps;
if (getTime() - lastFPS > 1000 && lfps != fps) {
lfps = fps;
fps = 0; //reset the FPS counter
lastFPS += 1000; //add one second
cfps = fps;
} else {
cfps = lfps;
}
fps++;
return cfps;
}
public static long getTime() {
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
void Run(){
while(!Display.isCloseRequested()){
try{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0, 0, 0f, 0f);
glLoadIdentity();
update();
Display.update();
Display.sync(FPS);
} catch (Exception e){
e.printStackTrace();
}
}
dispose();
destroy();
}
public abstract void dispose();
public void destroy() {
Display.destroy();
System.exit(0);
}
}
And finally my World Class is below.
import com.dev.voxy.Main;
import org.lwjgl.opengl.Display;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
public class World extends WorldManager {
private Chunk chunk;
public World() {
super(Main.WIDTH, Main.HEIGHT);
init();
}
#Override
public void init() {
chunk = new Chunk(0, 0, 0);
setRenderStats(true);
}
public void update() {
input();
render();
}
public void render() {
render3D();
Translate();
chunk.render();
glLoadIdentity();
if (isRenderStats()) {
render2D();
glColor3f(1, 1, 1);
renderStats();
}
}
#Override
public void dispose() {
chunk.dispose();
Display.destroy();
System.exit(0);
}
}
The problem is that when I try and incorporate my Menu into this it never works. First I tried GameStates so that whenever the game state is GAME the nifty should no longer render but this causes the Window to close because nifty no longer keeps it open, so then I tried to have the loop be outside the MainMenu Class but that also did not work because I kept running into static/non-static errors which I could not figure out. Then my last idea was why not try and put the gameloop inside the MainMenu Class but try to stop the nifty render while I try to start the game, but that left elements of the nifty on the screen and the rest was blank and the World class did not render. The following are my current Main, MainMenu, GLScreen and MyScreenController and MainMenu.xml files The MyscreenController controls what happens when the respective buttons are pressed and the MainMenu.xml is the nifty xml file that lays out everything.
All relevant files and classes may be found at the following link, they are the current ones where I am trying to get the main menu working. The classes shown above show what I did to get ONLY the game part working in the first place, without a menu.
LINK
To sum up, I can get my game working when there is not main menu and I can get my main menu working when there is no game, I have tried as many things I can think of but after 3 days I'm simply lost and would like any suggestion/recommendation or idea. I think I just can't figure out how to manage game states and the game loop when there is a main menu and a game, I was thinking maybe if there was a way to stop nifty completely then clear the screen then set OpenGL to 3D I can get my game rendered, this is what I was trying to do near the end but could not figure it out.
Thank you.
project: I am making a simple project in which i want to be able to make a kinematic body and move it from point x to y. There doesn't seem to be much directly on "Creating kinematic bodies". i've been following a few different tutorials on implementing kinematic bodies because i couldnt find one that specified how to do it correctly.
Problem: It seems to be a simple fix but i just can't figure out what the problem is because i don't fully understand how to implement a kinematic body. i believe the problem lies in this code kinematicBody.setLinearVelocity(0.0f, 1.0f); because i get an error in "setLinearVelocity"stating "The method setLinearVelocity(float, float) is undefined for the type BodyDef" i feel that i am doing something wrong here
Outcome i want: i want to be able to create a kinematic body that moves from point x to point y.
here is my relevant code:
package com.mohamed.JungleFighter;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Game;
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.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.sun.xml.internal.ws.wsdl.writer.document.soap.Body;
public class JungleFighter extends Game {
World world = new World(new Vector2(0, -100), true);
Box2DDebugRenderer debugRenderer;
OrthographicCamera camera;
static final float STEP=1/60f;
static final int VELOCITYITERATIONS=6;
static final int POSITIONITERATIONS=2;
static final float WORLD_TO_BOX=0.01f;
static final float BOX_WORLD_TO=100f;
#Override
public void create() {
camera = new OrthographicCamera();
camera.viewportHeight = 320;
camera.viewportWidth = 480;
camera.position.set(camera.viewportWidth * .5f, camera.viewportHeight * .5f, 0f);
camera.update();
//test platform 3
BodyDef kinematicBody =new BodyDef();
kinematicBody.type = BodyType.KinematicBody;
kinematicBody.position.set(100, 250);
PolygonShape shape = new PolygonShape();
shape.setAsBox(50f, 25f);
fixtureDef.shape = shape;
fixtureDef.density = 0;
fixtureDef.friction = 0;
fixtureDef.restitution = 0;
kinematicBody.setLinearVelocity(0.0f, 1.0f);
// Create our body in the world using our body definition
world.createBody(kinematicBody).createFixture(shape, 50.0f);
shape.dispose();
debugRenderer = new Box2DDebugRenderer();
}
#Override
public void dispose() {
}
#Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debugRenderer.render(world, camera.combined);
world.step(STEP, VELOCITYITERATIONS, POSITIONITERATIONS);
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
It appears as if you aren't using the fixtureDef object anywhere. You are supposed to set it to the body once it is created.
The way you create a Kinematic Body isn't much different from a Static Body. The only difference would be how you define the type of the BodyDef.
Create a Kinematic Body
It could be a StaticBody or DynamicBody too. Depends on what you want to accomplish.
BodyDef tempBodyDef = new BodyDef();
tempBodyDef.type = BodyType.KinematicBody;
Create the FixtureDefinition (The one that describes the properties of the fixture)
Also create a shape and assign it to the fixture.
You can modify the firction, isSensor, etc, properties of the tempFD to get the results you want.
FixtureDef tempFD = new FixtureDef();
PolygonShape tempShape = new PolygonShape();
tempFD.shape = tempShape;
The center of the shape for the body will be at 0,0
Vector2 tempVector = new Vector2();
tempVector.x = 0;
tempVector.y = 0;
Setting the size and position of the body's shape.
Notice I'm using half the width and height, as that is what the setAsBox function requires.
tempShape.setAsBox(myWidth/2f, myHeight/2f, tempVector, myAngle);
tempBodyDef.position.set(myXPosition, myYPosition);
Create the body and assign the fixture to it
Body testBody = world.createBody(tempBodyDef);
testBody.createFixture(tempFD);
Don't forget to dispose of the Shape once you are done with it.
tempShape.dispose();
And you are done.
I'm currently trying my hand at LibGDX to create a simple game. Currently I have finished working on the main menu and have tried to program the Level Selection menu. I have the following code in my LevelSelection class but my application crashes when I try to click on the "start" button in my main menu to bring up the Level Selection menu:
import java.util.ArrayList;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
public class LevelSelect implements Screen {
private Stage stage;
private TextureAtlas atlas;
private Table table;
private ArrayList<TextButton> buttons;
private Skin skin;
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
public void initButtons(){
buttons = new ArrayList<TextButton>();
for(int i=1;i<=5;i++){
TextButton b = new TextButton(("" + i),skin);
buttons.add(b);
}
}
#Override
public void resize(int width, int height) {
/*stage.getViewport().update(width, height, false);
table.invalidateHierarchy();*/
}
#Override
public void show() {
stage = new Stage();
Gdx.input.setInputProcessor(stage);
atlas = new TextureAtlas(Gdx.files.internal("ui/button.pack"));
skin = new Skin(Gdx.files.internal("ui/menuSkin.json"),atlas);
table = new Table(skin);
this.initButtons();
}
#Override
public void hide() {
dispose();
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
stage.dispose();
skin.dispose();
}
}
It seems that the initButtons method isn't the issue either as even without that the application crashes. The application reports no errors, rather I get a Java Platform SE binary has stopped working issue when I click the 'start' button that loads this new screen class.
Thank you for your help!
I am following a tutorial on creating a game with LibGdx from some ebook. the tutorial has steps for creating a game called "Canyon Bunny". Its a simple 2D game. but i keep getting this annoying error! (i also used to get the error on a different tutorial of the same genre)
i am in the early stages of the development for this game. and i am doing some test (of which i follow to the letter from the tutorial). I use a MAC and a i have tried many solutions with no luck at all.
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.Adel.CanyonBunny.game.WorldUpdater.updateTestObjects(WorldUpdater.java:83)
at com.Adel.CanyonBunny.game.WorldUpdater.update(WorldUpdater.java:76)
at com.Adel.CanyonBunny.CanyonBunnyMain.render(CanyonBunnyMain.java:39)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
It is truly one of the most frustrating things a striving programmer can face.
ill get the code of all the classes in case that's related somehow...
This is CanyonBunnyMain in the general program:
package com.Adel.CanyonBunny;
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.Adel.CanyonBunny.game.*;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.Application;
public class CanyonBunnyMain implements ApplicationListener {
private static final String TAG = CanyonBunnyMain.class.getName();
private WorldUpdater worldUpdater;
private WorldRenderer worldRenderer ;
private boolean paused ;
public void create() {
//I'll set the log to debug for the developing process
Gdx.app.setLogLevel(Application.LOG_DEBUG) ;
worldUpdater = new WorldUpdater();
worldRenderer = new WorldRenderer() ;
// since, upon creation, the game is not paused, then:
paused = false ;
}
public void render() {
if (paused = true) {
//update the game by the time passed since the last update
worldUpdater.update(Gdx.graphics.getDeltaTime()) ;
}
//sets the screen color to: CornFlower Blue
Gdx.gl.glClearColor(0x64 / 255.0f, 0x95 / 255.0f, 0xed / 255.0f, 0xff / 255.0f);
//clears the screen to prevent flickering
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT) ;
//Render the game to the screen
worldRenderer.render();
}
public void resize (int w, int h) {
worldRenderer.resize(w, h) ;
}
public void pause () {
paused = true ;
}
public void resume() {
paused = false ;
}
public void dispose() {
worldRenderer.dispose() ;
} }
this is the WorldRenderer (general program too) :
package com.Adel.CanyonBunny.game;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
public class WorldRenderer {
private OrthographicCamera cam;
private SpriteBatch batch ;
private WorldUpdater updater;
public void WorldRenderer(WorldUpdater worldUpdater) { }
public void init() { }
public void render() { }
public void resize(int w, int h) { }
public void dispose() { }
}
this is the main class (from the desktop project: the one that i run on my MAC) :
package com.Adel.CanyonBunny;
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 = "CanyonBunny";
cfg.useGL20 = false;
cfg.width = 800;
cfg.height = 480;
new LwjglApplication(new CanyonBunnyMain(), cfg);
}
}
Any help will be wonderful.
tell me should you need extra data
this is the WorldUpdater class for those who asked:
package com.Adel.CanyonBunny.game;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.MathUtils;
public class WorldUpdater {
private final String TAG = WorldUpdater.class.getName();
public Sprite[] testSprites;
public int selectedSprite;
public WorldRenderer worldRenderer;
public void worldUpdater() {
init() ;
}
public void init() {
initTestObjects() ;
}
private void initTestObjects() {
// create new array of 5 sprites
testSprites = new Sprite[5] ;
// Create empty POT-sized Pixmap with 8 bit RGBA pixel data
int w = 32;
int h = 32;
Pixmap pixmap = createProceduralPixmap(w, h) ;
//create a new texture from Pixmap data
Texture texture = new Texture(pixmap) ;
//create sprites using the just created texture
for (int i = 0; i < testSprites.length; i++) {
Sprite spr = new Sprite(texture) ;
spr.setSize(1,1) ;
//set origin to sprite's center
spr.setOrigin(spr.getWidth() / 2.0f, spr.getHeight() / 2.0f) ;
float randomX = MathUtils.random(-2.0f, 2.0f) ;
float randomY = MathUtils.random(-2.0f, 2.0f) ;
spr.setPosition(randomX, randomY) ;
//put new sprite into array
testSprites[i] = spr ;
}
//set first sprite as the selected one
selectedSprite = 0 ;
}
private Pixmap createProceduralPixmap(int width, int height) {
Pixmap pixmap = new Pixmap(width, height , Format.RGBA8888) ;
//fill the square with red color at 50% opacity
pixmap.setColor(1, 0, 0, 0.5f) ;
pixmap.fill() ;
//draw a yellow X in the pixmap
pixmap.setColor(1, 1, 0 , 1) ;
pixmap.drawLine(0, 0, width, height) ;
pixmap.drawLine(width, 0, 0, height);
//draw a cyan-colored border around the square
pixmap.setColor(0, 1, 1, 1) ;
pixmap.drawRectangle(0, 0, width, height) ;
return pixmap;
}
public void update(float deltaTime) {
updateTestObjects(deltaTime);
}
private void updateTestObjects(float deltaTime) {
//get current rotation from the selected sprite
float rotation = testSprites[selectedSprite].getRotation();
//rotate sprite by 90 degrees per second
rotation += 90 * deltaTime;
//wrap around at 360 degrees
rotation %= 360 ;
testSprites[selectedSprite].setRotation(rotation);
}
}
Also, when i check this line out in Debugging mode:
testSprites = new Sprite[5] ;
"testSprites" keeps showing null.
i hope this clears up some details!
thanks again.
The problem is with your "constructors", mainly in the updater (as the renderer does nothing):
public void worldUpdater() { ... }
Constructors should not specify return types - that's part of how the compiler recognizes them as constructors. As it is in your code, it's just a method you could call on an existing object instance. Change it like so:
public WorldUpdater() { ... }
Note the lack of a return type and the uppercase W.
You can change the renderer the same way. (But then you will have to pass the updater to its constructor in the main class.)
Also, Nine Magics is right that the way you store renderer and updater references in each other doesn't make much sense, even if it's not related to this problem. I see no reason why an updater class would need to know about its renderer, I'd remove that field.
In your WorldRenderer you specify this:
public void WorldRenderer(WorldUpdater worldUpdater) { }
And WorldRendere also carries an instance of an worldUpdater?
private WorldUpdater updater;
But on your main file you create an instance of both renderer and updater?
worldUpdater = new WorldUpdater();
worldRenderer = new WorldRenderer() ;
I don't know, I might have tired eyes or something but this seems too complex. Can it be that you are refering to a wrong instance of WorldUpdater? Might edit this if I can wrap my head around it better.