Libgdx: Draw a texture with tilemap - java

I have very simple question. But i couldnt solve. I just want to draw a texture on the tilemap. With theese codes i can see the map and move, but i cant see texture.
Here is the screen:
package com.adsiz.areyoualive.screen;
import com.adsiz.areyoualive.game.Player;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.*;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
public class PlayScreen implements Screen, InputProcessor{
public ScreenManager screenManager;
public static float speed = 32;
//tilemap & camera
TiledMap tiledMap;
OrthographicCamera camera;
TiledMapRenderer renderer;
//player
Player player;
//spritebatch
SpriteBatch spriteBatch;
public PlayScreen(){
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.setToOrtho(false, w, h);
camera.update();
tiledMap = new TmxMapLoader().load("map/untitled.tmx");
renderer = new OrthogonalTiledMapRenderer(tiledMap);
spriteBatch = new SpriteBatch();
camera.position.set(0, 3200, 0);
Gdx.input.setInputProcessor(this);
player = new Player(0, 3200);
spriteBatch.setTransformMatrix(camera.combined);
}
#Override
public void render(float delta) {
Gdx.graphics.getGL20().glClearColor( 1, 1, 1, 1 );
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.graphics.getGL20().glClear( GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT );
camera.update();
renderer.setView(camera);
renderer.render();
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.begin();
player.draw(spriteBatch);
spriteBatch.end();
}
#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
}
#Override
public boolean keyDown(int keycode) {
switch (keycode) {
case Keys.DOWN:
camera.translate(0, -1*speed);
player.moveDown();
break;
case Keys.UP:
camera.translate(0, speed);
player.moveUp();
break;
case Keys.LEFT:
camera.translate(-1*speed, 0);
player.moveLeft();
break;
case Keys.RIGHT:
camera.translate(speed, 0);
player.moveRight();
break;
}
return false;
}
#Override
public boolean keyUp(int keycode) {
return false;
}
#Override
public boolean keyTyped(char character) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean scrolled(int amount) {
// TODO Auto-generated method stub
return false;
}
}
and here is my player.draw function:
public void draw(SpriteBatch batch){
batch.draw(playerTexture, getPosX(), getPosY(), playerTexture.getWidth(), playerTexture.getHeight());
}
i just want to show a simple texture and move it on the screen. What is wrong above there? May you help me?
Regards Guys!

I solved my own problem, I just changed map renderer to OrthogonalTileMapRenderer, and changed SpriteBatch's with Batch of OrthogonalTileMapRenderer.
package com.adsiz.areyoualive.screen;
import com.adsiz.areyoualive.game.Player;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.tiled.*;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
public class PlayScreen implements Screen, InputProcessor{
public ScreenManager screenManager;
public static float speed = 32;
//tilemap & camera
TiledMap tiledMap;
OrthographicCamera camera;
OrthogonalTiledMapRenderer renderer;
//player
Player player;
public PlayScreen(){
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.setToOrtho(false, w, h);
camera.update();
tiledMap = new TmxMapLoader().load("map/untitled.tmx");
renderer = new OrthogonalTiledMapRenderer(tiledMap);
camera.position.set(0, 3200, 0);
Gdx.input.setInputProcessor(this);
player = new Player(0, 3200);
}
#Override
public void render(float delta) {
Gdx.graphics.getGL20().glClearColor( 1, 1, 1, 1 );
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.graphics.getGL20().glClear( GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT );
camera.update();
renderer.setView(camera);
renderer.render();
renderer.getSpriteBatch().begin();
player.draw(renderer.getSpriteBatch());
renderer.getSpriteBatch().end();
}

Related

Libgdx/ Java cameras for multiple players?

I am trying to make a little multiplayer rpg game.
It all worked fine, until I implemented cameras for each player.
Now I got the problem, that if one player joines, he can't walk alone. It seems that he is stuck on the Client players cam. I have created a camera for each of them. Did I miss something?
Here's my "Main" class
public class LauncherScreen implements Screen{
//-----------------------------------------------------------
//-----------------idle Animation----------------------------
//-----------------------------------------------------------
Texture texture;
AnimatedSprite animationForMultiplayer;
SpriteBatch spriteBatch;
Player mySelf;
OrthographicCamera playerCam;
OrthographicCamera mpPlayerCam;
static Client client = new Client();
Launcher launcher = new Launcher();
#Override
public void render(float delta) {
// TODO Auto-generated method stub
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
launcher.update();
for(MPPlayer mpPlayer : launcher.getPlayersValue()){
//System.out.println("mpPlayerXandY : "+mpPlayer.state);
animationForMultiplayer.setState(mpPlayer.state);
animationForMultiplayer.createAnimation();
mpPlayerCam.update();
spriteBatch.setProjectionMatrix(mpPlayerCam.combined);
spriteBatch.begin();
spriteBatch.draw(animationForMultiplayer.convertAnimationTOframes(), mpPlayer.x, mpPlayer.y,Gdx.graphics.getWidth()/25,Gdx.graphics.getHeight()/15); // #6
spriteBatch.end();
mpPlayerCam.position.set(mpPlayer.x,mpPlayer.y,0);
System.out.println("mpPlayer : "+mpPlayer.x+" "+mpPlayer.y);
}
mySelf.update();
mySelf.draw(launcher.getPlayerX(), launcher.getPlayerY(), playerCam);
//System.out.println(Gdx.graphics.getFramesPerSecond());
System.out.println("player : "+launcher.getPlayerX()+" "+launcher.getPlayerY());
}
#Override
public void pause() {
// TODO Auto-generated method stub
//super.pause();
}
#Override
public void resume() {
// TODO Auto-generated method stub
//super.resume();
}
#Override
public void dispose() {
// TODO Auto-generated method stub
//super.dispose();
}
#Override
public void show() {
// TODO Auto-generated method stub
texture = new Texture(Gdx.files.internal("EnemyAnimations/BugIdleStand.png"));
animationForMultiplayer = new AnimatedSprite();
spriteBatch = new SpriteBatch();
mySelf = new Player();
mySelf.doSetup();
playerCam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
playerCam.setToOrtho(false);
playerCam.position.set(mySelf.getX(), mySelf.getY(), 0);
mpPlayerCam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
mpPlayerCam.setToOrtho(false);
mpPlayerCam.position.set(0, 0, 0);
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
}
And here's the "Main" player update
public void draw(float f, float g, OrthographicCamera camera){
position.x = f;
position.y = g;
//System.out.println("In beforeSetState : "+currentState);
animatedSprite.setState(state);
//System.out.println("In after : "+currentState);
animatedSprite.createAnimation();
camera.position.set(f,g,0);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(animatedSprite.convertAnimationTOframes(),f,g, Gdx.graphics.getWidth()/25,Gdx.graphics.getHeight()/15); // #17
batch.end();
//batch.setProjectionMatrix(null);
//System.out.println(currentState);
}
Found a solution for it, didnt knew that it was so easy ^^ heres my updated code
public class LauncherScreen implements Screen{
//-----------------------------------------------------------
//-----------------idle Animation----------------------------
//-----------------------------------------------------------
Texture texture;
AnimatedSprite animationForMultiplayer;
SpriteBatch spriteBatch;
Player mySelf;
OrthographicCamera playerCam;
OrthographicCamera mpPlayerCam;
OrthographicCamera camera;
ShapeRenderer shapeRenderer;
static Client client = new Client();
Launcher launcher = new Launcher();
int[][] map = {{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}};
#Override
public void render(float delta) {
// TODO Auto-generated method stub
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
launcher.update();
for(int i = 0; i < map.length; i++){
for(int j = 0; j < map[0].length; j++){
if(map[i][j] == 1){
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeType.Line);
shapeRenderer.setColor(0, 0, 0, 0);
shapeRenderer.rect(i*50, j*50, 50, 50);
shapeRenderer.end();
}
}
}
for(MPPlayer mpPlayer : launcher.getPlayersValue()){
//System.out.println("mpPlayerXandY : "+mpPlayer.state);
animationForMultiplayer.setState(mpPlayer.state);
animationForMultiplayer.createAnimation();
//mpPlayerCam.update();
//spriteBatch.setProjectionMatrix(mpPlayerCam.combined);
camera.position.set(mpPlayer.x,mpPlayer.y,0);
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.begin();
spriteBatch.draw(animationForMultiplayer.convertAnimationTOframes(), mpPlayer.x, mpPlayer.y,Gdx.graphics.getWidth()/25,Gdx.graphics.getHeight()/15); // #6
spriteBatch.end();
//mpPlayerCam.position.set(mpPlayer.x,mpPlayer.y,0);
System.out.println("mpPlayer : "+mpPlayer.x+" "+mpPlayer.y);
}
mySelf.update();
mySelf.draw(launcher.getPlayerX(), launcher.getPlayerY(), camera);
//System.out.println(Gdx.graphics.getFramesPerSecond());
camera.update();
System.out.println("player : "+launcher.getPlayerX()+" "+launcher.getPlayerY());
}
#Override
public void pause() {
// TODO Auto-generated method stub
//super.pause();
}
#Override
public void resume() {
// TODO Auto-generated method stub
//super.resume();
}
#Override
public void dispose() {
// TODO Auto-generated method stub
//super.dispose();
}
#Override
public void show() {
// TODO Auto-generated method stub
texture = new Texture(Gdx.files.internal("EnemyAnimations/BugIdleStand.png"));
animationForMultiplayer = new AnimatedSprite();
spriteBatch = new SpriteBatch();
mySelf = new Player();
mySelf.doSetup();
playerCam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
playerCam.setToOrtho(false);
playerCam.position.set(mySelf.getX(), mySelf.getY(), 0);
mpPlayerCam = new OrthographicCamera(0,0);
mpPlayerCam.setToOrtho(false);
camera = new OrthographicCamera(0, 0);
camera.setToOrtho(false);
shapeRenderer = new ShapeRenderer();
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
}

Libgdx NullPointerException when trying to update viewport

I'm trying to update the game's viewport when the game resizes, but when I start the game, I get a java.lang.NullPointerException:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.utils.viewport.Viewport.apply(Viewport.java:49)
at com.badlogic.gdx.utils.viewport.ExtendViewport.update(ExtendViewport.java:90)
at com.badlogic.gdx.utils.viewport.Viewport.update(Viewport.java:57)
at me.chrisjosten.testgame.screens.MainScreen.resize(MainScreen.java:82)
at com.badlogic.gdx.Game.setScreen(Game.java:62)
at me.chrisjosten.testgame.create(MainScreen.java:13)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
In the class, that implements Screen, I have in the resize method, the place where the exception occurs, the following code:
#Override
public void resize(int w, int h) {
viewport.update(w, h);
}
where the viewport is an ExtendViewport created in the constructor of the class. I've tried putting that in the show method to, but then I get the same result.
The full code of the class:
package me.chrisjosten.testgame.screens;
import me.chrisjosten.testgame.MyGame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.badlogic.gdx.utils.viewport.FillViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
public class MainScreen implements Screen{
private MyGame game;
private BitmapFont font;
private SpriteBatch batch;
private OrthographicCamera camera;
private Viewport viewport;
private boolean goingUp = false;
private float alpha = 1;
private int gameWidth = 100;
private int gameHeight = 100;
private int screenWidth;
private int screenHeight;
public MainScreen(MyGame g) {
game = g;
System.out.println("screen created");
}
#Override
public void show() {
System.out.println("show");
font = new BitmapFont(Gdx.files.internal("fonts/monospace.fnt"));
font.setColor(1, 1, 1, 1);
batch = new SpriteBatch();
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.translate(gameWidth / 2, gameHeight / 2);
viewport = new ExtendViewport(gameWidth, gameHeight, camera);
}
#Override
public void render(float delta) {
camera.update();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (goingUp) {
font.setColor(1, 1, 1, alpha);
alpha += 0.025;
} else {
font.setColor(1, 1, 1, alpha);
alpha -= 0.025;
}
if (alpha <= 0) {
goingUp = true;
alpha = 0;
} else if (alpha >= 1) {
goingUp = false;
alpha = 1;
}
batch.setProjectionMatrix(camera.combined);
batch.begin();
String press = "Press start";
font.draw(batch, press, gameWidth / 2 - font.getBounds(press).width / 2, 1);
batch.end();
}
#Override
public void resize(int w, int h) {
viewport.update(w, h);
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
font.dispose();
}
}
Does anyone know what I have done wrong?
By moving the code from the show method to the constructor, it works.

Get Coordinates before and after touch released

I am trying to make slingshot type thing(like in angry birds) using LIBGDX(currently learning). So, how do I get the coordinates of the point where user touched the screen and dragged finger to other point n released . I want both co-ordinates initial and released point.
I also want to know how to achieve the above in Android SDK/NDK.
I have not tested this but this should be around about what you need
#Override
Public boolean onTouchEvent (MotionEvent event) {
int downx;
int downy;
int upx;
int upy;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downy = (int)event.gety();
downx = (int)event.getx();
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
upy = (int)event.gety();
upx = (int)event.getx();
}
return false;
}
public class TempScreen extends Screen
{
Game game; // the class which is implementing the InputProcessor
SpriteBatch batcher;
Vector3 touchPoint;
OrthographicCamera cam;
InputMultiplexer multiplexer;
GestureListener listener=new GestureListener()
{
#Override
public boolean zoom(float initialDistance, float distance)
{
// TODO Auto-generated method stub
return false;
}
#Override
public boolean touchDown(float x, float y, int pointer, int button)
{
cam.unproject(touchPoint.set(x, y, 0));
System.out.println(touchPoint);
return false;
}
#Override
public boolean tap(float x, float y, int count, int button)
{
// TODO Auto-generated method stub
return false;
}
#Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2,
Vector2 pointer1, Vector2 pointer2)
{
// TODO Auto-generated method stub
return false;
}
#Override
public boolean panStop(float x, float y, int pointer, int button)
{
// TODO Auto-generated method stub
return false;
}
#Override
public boolean pan(float x, float y, float deltaX, float deltaY)
{
// TODO Auto-generated method stub
return false;
}
#Override
public boolean longPress(float x, float y)
{
// TODO Auto-generated method stub
return false;
}
#Override
public boolean fling(float velocityX, float velocityY, int button)
{
// TODO Auto-generated method stub
return false;
}
};
GestureDetector detector=new GestureDetector(listener)
{
public boolean touchUp(float x, float y, int pointer, int button)
{
cam.unproject(touchPoint.set(x, y, 0));
System.out.println(touchPoint);
return false;
}
};
public TempScreen(Game game,SpriteBatch batcher)
{
super(game);
this.batcher=batcher;
this.game=game;
touchPoint = new Vector3();
cam = new OrthographicCamera(GameConstants.CAMERA_WIDTH,GameConstants.CAMERA_HEIGHT);
cam.position.set(GameConstants.CAMERA_WIDTH / 2,GameConstants.CAMERA_HEIGHT / 2, 0);
multiplexer = new InputMultiplexer();
multiplexer.addProcessor(game);
multiplexer.addProcessor(detector);
Gdx.input.setInputProcessor(multiplexer);
}
#Override
public void render(float deltaTime)
{
update(deltaTime);
GLCommon gl = Gdx.gl;
gl.glClearColor(0, 0f, 1f, 0.1f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
cam.update();
batcher.setProjectionMatrix(cam.combined);
batcher.enableBlending();
batcher.begin();
draw(deltaTime);
batcher.end();
}
#Override
public void draw(float deltaTime)
{
// TODO Auto-generated method stub
}
#Override
public void update(float deltaTime)
{
// TODO Auto-generated method stub
}
#Override
public void backKeyPressed()
{
// TODO Auto-generated method stub
}
}
Make an InputProcessor and a Vector3:
MyInputprocessor myInputprocessor;
Vector3 touchPoint = new Vector3();
Define the InputProcessor like this:
public class MyInputprocessor implements InputProcessor{
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button){
guicam.unproject(touchPoint.set(screenX, screenY, 0)); //Initial point coordinates
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer){
guicam.unproject(touchPoint.set(screenX, screenY, 0)); //current point coordinates (when you are dragging it)
return false;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button){
guicam.unproject(touchPoint.set(screenX, screenY, 0)); //final/release point coordinates
return false;
}
#Override public boolean keyTyped(char character){return false;}
#Override public boolean mouseMoved(int screenX, int screenY){return false;}
#Override public boolean scrolled(int amount){return false;}
#Override public boolean keyDown(int keycode){return false;}
#Override public boolean keyUp(int keycode){return false;}
}
Create it and set it in your ini code:
myInputprocessor = new MyInputprocessor();
Gdx.input.setInputProcessor(myInputprocessor);

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.

Overlay only draws line between first 2 GPS points in Android

I am experiencing an unusual error using ItemizedOverlay in Android.
I am creating a GPS tracking device that plots a route between waypoints stored in a database.
When I provide the first two sets of longitude and latitude points through the emulator in Eclipse, it draws a red line just how I want it, but if I send another GPS point, it animates to the point, but does not draw a line from the last point.
public class MyOverlay extends ItemizedOverlay<OverlayItem>
{
// private Projection projection;
private Paint linePaint;
private Vector<GeoPoint> points;
public MyOverlay(Drawable defaultMarker) {
super(defaultMarker);
points = new Vector<GeoPoint>();
//set colour, stroke width etc.
linePaint = new Paint();
linePaint.setARGB(255, 255, 0, 0);
linePaint.setStrokeWidth(3);
linePaint.setDither(true);
linePaint.setStyle(Style.FILL);
linePaint.setAntiAlias(true);
linePaint.setStrokeJoin(Paint.Join.ROUND);
linePaint.setStrokeCap(Paint.Cap.ROUND);
}
public void addPoint(GeoPoint point) {
points.addElement(point);
}
public void draw(Canvas canvas, MapView view, boolean shadow) {
int size = points.size();
Point lastPoint = new Point();
if(size == 0) return;
view.getProjection().toPixels(points.get(0), lastPoint);
Point point = new Point();
for(int i = 1; i<size; i++){
view.getProjection().toPixels(points.get(i), point);
canvas.drawLine(lastPoint.x, lastPoint.y, point.x, point.y, linePaint);
lastPoint = point;
}
}
#Override
protected OverlayItem createItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
}
In your code the draw() methhod is called only once and then it is never recalled. The populate method will repopulate all the overlayes points which is there in your list each time. So it will again call the draw method which will draw a line for you.
package com.example.mymap;
import java.util.ArrayList;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class ItemOverLay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
GeoPoint prePoint=null,currentPoint=null;
MapView mapView=null;
Paint paint=new Paint();
public ItemOverLay(GeoPoint prePoint,GeoPoint currentPoint, Drawable defaultMarker,MapView mapview) {
super(boundCenterBottom(defaultMarker));
this.currentPoint=currentPoint;
this.prePoint = prePoint;
mapView=mapview;
// TODO Auto-generated constructor stub
}
public ItemOverLay(Drawable defaultMarker) {
super(defaultMarker);
// TODO Auto-generated constructor stub
}
public void addOverlay(OverlayItem item){
mOverlays.add(item);
populate();
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
Paint paint=new Paint();
Point screenCoords=new Point();
Point screenCoords1=new Point();
mapView.getProjection().toPixels(prePoint, screenCoords);
int x1=screenCoords.x;
int y1=screenCoords.y;
mapView.getProjection().toPixels(currentPoint, screenCoords1);
int x2=screenCoords1.x;
int y2=screenCoords1.y;
paint.setStrokeWidth(1);
canvas.drawLine(x1, y1, x2, y2, paint);
}
#Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
}
Change this line
view.getProjection().toPixels(points.get(i), point);
to
point = view.getProjection().toPixels(points.get(i), null);
then it should work.

Categories

Resources