File not found while loading Tiled map (libgdx) - java

I'm desperately seeking for help. Searched already everywhere, can't find an answer.
I'm trying to load tmx Tiled map with libgdx. I have no idea what's wrong, this is an exception:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.SerializationException: Error parsing file: level1.tmx
at com.badlogic.gdx.utils.XmlReader.parse(XmlReader.java:83)
at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:80)
at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:67)
at com.mygdx.game.PlayScreen.<init>(PlayScreen.java:35)
at com.mygdx.game.MyGdxGame.create(MyGdxGame.java:30)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:149)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: level1.tmx (Internal)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136)
at com.badlogic.gdx.files.FileHandle.reader(FileHandle.java:163)
at com.badlogic.gdx.utils.XmlReader.parse(XmlReader.java:81)
...
and this is my code:
public class PlayScreen implements Screen {
private MyGdxGame game;
private Texture texture;
private OrthographicCamera gamecam;
private Viewport gamePort;
private Hud hud;
private TiledMap map;
private TmxMapLoader mapLoader;
private OrthogonalTiledMapRenderer renderer;
public PlayScreen(MyGdxGame game) {
this.game = game;
gamecam = new OrthographicCamera();
gamePort = new FitViewport(MyGdxGame.V_WIDTH, MyGdxGame.V_HEIGHT, gamecam);
hud = new Hud(game.batch);
mapLoader = new TmxMapLoader();
map = mapLoader.load("level1.tmx"); //THIS file cannot be found...
renderer = new OrthogonalTiledMapRenderer(map);
gamecam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2, 0);
}
#Override
public void show() {
}
public void handleInput(float dt) {
if (Gdx.input.isTouched())
gamecam.position.x += 100 * dt;
}
public void update(float dt) {
handleInput(dt);
gamecam.update();
renderer.setView(gamecam);
}
#Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
}
//lines above are potentially bad
...
}
level1.tmx and proper tileset.png file sit together in assets folder.
Tried also remaking the map with another tileset - got the same error. I see 'level1.tmx' in the project view window, in assets folder, 100% sure they are there. Using Android Studio.
Exception above is thrown while I'm trying to run program in desktop. As I'm trying to do so in android, it's all fine, compiled and built, but there's no map rendered and I see nothing except the black screen.
Hope there's a person who could help me, regards:)

Related

TiledMap not rendering

I am trying to render an OrthogonalTiledMap I created using the map editor Tiled however for some reason nothing is showing up in my game screen; all I get is a black image being shown. I am using the Libgdx framework which has features for exactly these kinds of maps already built in however they won't work for me.
Libdgx also provides an example of rendering OrthogonalTiledMaps however it is outdated but I adjusted it to current Libdgx version but as already stated it doesn't work.
There are no errors nor exceptions being thrown. The .tmx file does also not contain any errors. All the used tileset are present and do not cause any errors.
This is my code:
`
public class My_Game extends ApplicationAdapter {
private TiledMap map;
private TiledMapRenderer renderer;
private OrthographicCamera camera;
private CameraInputController cameraController;
#Override
public void create () {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.setToOrtho(true, w/8f, h/8f);
camera.update();
cameraController = new CameraInputController(camera);
Gdx.input.setInputProcessor(cameraController);
map = new TmxMapLoader().load("map.tmx");
renderer = new OrthogonalTiledMapRenderer(map, 1f / 8f);
}
#Override
public void render () {
camera.update();
renderer.setView(camera);
renderer.render();
}
#Override
public void dispose () {
map.dispose();
}
}`
Was a while since I did Libgdx so I might be thinking of something else, but don't you have to do some clearing in the render() function?
Try adding:
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
as the first three lines there and let me know if that helps.

touch events not registering on libgdx sprites

I am using libgdx to make a card game but I have hit a deadlock.
Touches or clicks on sprites are not being detected.
There is extra space that appears on my board which is not there in the
tiled map file.
I made a simple project to illustrate my problem.
A compilable project is here that shows the code in its enterity https://dl.dropboxusercontent.com/u/51343591/card.zip
public class Card extends ApplicationAdapter {
SpriteBatch batch;
TmxMapLoader tmxMapLoader;
TiledMap tiledMap;
OrthogonalTiledMapRenderer mapRenderer;
OrthographicCamera camera;
Viewport gamePort;
Sprite cardSprite;
Sprite dotSprite;
Vector3 screenPointTouched;
#Override
public void create () {
batch = new SpriteBatch();
tmxMapLoader=new TmxMapLoader();
tiledMap= tmxMapLoader.load("level1.tmx");
mapRenderer=new OrthogonalTiledMapRenderer(tiledMap);
float worldwidth=19*16;//19tiles*16pixelspertile. This is what its like on the level1.tmx file
float worldheight=12*16;//12tiles*16pixelspertile. This is what its like on the level1.tmx file
gamePort= new StretchViewport(worldwidth,worldheight,camera);//new StretchViewport(,
camera= new OrthographicCamera(10,10*(Gdx.graphics.getWidth()/Gdx.graphics.getHeight()));
camera.position.set(gamePort.getWorldWidth()/2,gamePort.getWorldHeight()/2,0);//center the camera
cardSprite=new Sprite(new Texture("badlogic.jpg"));
dotSprite=new Sprite(new Texture("dot.png"));
screenPointTouched=new Vector3();
positionCardSpriteFromFirstRectangle();
}
#Override
public void render () {
update();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
mapRenderer.render();
batch.begin();
cardSprite.draw(batch);
dotSprite.draw(batch);
batch.end();
}
private void positionCardSpriteFromFirstRectangle() {
MapObject mo=tiledMap.getLayers().get("cardlayer").getObjects().get(0);//get the firstone onlys
Rectangle rect=((RectangleMapObject)mo).getRectangle();
cardSprite.setBounds(rect.x,rect.y,rect.width,rect.height);
}
public void update() {
camera.update();
mapRenderer.setView(camera);
if(Gdx.input.isTouched()){//handle touch event
screenPointTouched.set(Gdx.input.getX(),Gdx.input.getY(),0);
screenPointTouched=camera.unproject(screenPointTouched);
if(cardSprite.getBoundingRectangle().contains(screenPointTouched.x,screenPointTouched.y)){
System.out.println("being touched");
dotSprite.setPosition(screenPointTouched.x,screenPointTouched.y);
}
}
}
}

Error on Java libgdx with AssetsManager

So I start to try to do some game in java with libgdx, and I am trying to use the AssetsManager. but it keeps giving an error. I will put the code (I will try to resume the code) and the output.
The game goes through the loading screen fine but when it reaches the game screen it stops and gives an error, I do believe it's because the assets.
If someone knows why I am getting this error.
DesktopLauncher.java
public class DesktopLauncher {
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
new LwjglApplication(new pressme(), config);
}
}
pressme.java
public class pressme extends Game {
LoadingScreen loadingscreen;
AssetsManager assetsmanager;
Assets assets;
#Override
public void create() {
assets = new Assets();
assetsmanager = new AssetsManager();
assetsmanager.load();
assets.other();
loadingscreen = new LoadingScreen(this);
setScreen(loadingscreen);
}
}
LoadingScreen.java
public class LoadingScreen implements Screen {
Assets assets;
AssetsManager assetsmanager;
private final pressme game;
public GameScreen game_screen;
public LoadingScreen(final pressme game){
assets = new Assets();
assetsmanager = new AssetsManager();
this.game = game;
}
#Override
public void show() {
assetsmanager.load();
}
private void update(float delta){
System.out.println(progress);
if(assetsmanager.manager.update()) {
game.setScreen(new GameScreen(game));
}
progress = assetsmanager.manager.getProgress();
}
}
GameScreen.java
public class GameScreen implements Screen {
pressme game;
Assets assets;
AssetsManager assetsmanager;
public GameScreen(pressme game){
this.game = game;
assets = new Assets();
assetsmanager = new AssetsManager();
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor( 0.5F, 0.5F, 0.5F, 0.5F);
Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT );
camera.update();
generalUpdate(touch, camera);
batch.setProjectionMatrix(camera.combined);
assets.load();
batch.begin();
batch.draw(assets.back, 0, 0);
batch.end();
}
}
Assets.java
public class Assets {
AssetsManager assetsmanager;
public Sprite back;
public void load(){
assetsmanager = new AssetsManager();
assetsmanager.load();
back = new Sprite(assetsmanager.manager.get(assetsmanager.back, Texture.class));
}
}
AssetsManager.java
public class AssetsManager {
public AssetManager manager = new AssetManager();
public String back = "back.png";
public void load(){
manager.load(back, Texture.class);
}
}
Output
LOADINGGG
0.0
0.0
0.015151516
0.015151516
0.030303031
.
.
.
0.969697
0.9848485
0.9848485
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.graphics.g2d.SpriteBatch.draw(SpriteBatch.java:586)
at com.david.pressme.GameScreen.render(GameScreen.java:244)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:223)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)
EDIT
RC Thank you for the tip
So After I put the "assets.load()" still with some error. I dont know if the "assets.load()" it is on the right place
LOADINGGG
0.0
0.0
0.015151516
0.015151516
0.030303031
.
.
.
0.969697
0.969697
0.9848485
0.9848485
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Asset not loaded: back.png
at com.badlogic.gdx.assets.AssetManager.get(AssetManager.java:144)
at com.david.pressme.Assets.load(Assets.java:254)
at com.david.pressme.GameScreen.render(GameScreen.java:172)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:223)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)
EDIT2
RC, Spylot ty for the tips, it was what you said
So its resolved, i puting
"assets.load" on "public void show()"
this way when the screen its load he loads the assest on the begin, and i take it off from the
"public void render(){"
because he was always loading the assests because its a loop (I think so), then on
"Assets.java public load(){" i put "assetsmanager.load();"
Thanks Rc for the tips. Then on the same place I put the
"assetsmanager.manager.finishLoading();"
so it loads all assets before do something. Thank you spylot for the tip.
Thank you all who help or try to help much appreciate.
You should be aware that when you're loading assets, you cannot use them until you call finishLoading. finishLoading blocks all calls trying to grab onto the asset until they are fully loaded in the memory.
I highly suggest you to read the AssetManager API.
So in short, after each time you use load() call, you should finish it off using manager.finishLoading()

Mouse inputs make the picture drag and copy rather than just drag

Here is an example of what it looks like when I drag a picture with the mouse
http://i.stack.imgur.com/4RQDU.jpg
And here is the code for it.
When I load the project on the desktop application, the picture displays perfectly in the middle, but when I move it with the mouse it drags itself like in Windows xp.
public class SlingshotSteve implements ApplicationListener{
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
#Override
public void create() {
camera = new OrthographicCamera(1280, 720);
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("image.png"));
sprite = new Sprite(texture);
sprite = new Sprite(texture);
sprite.setOrigin(0,0);
sprite.setPosition(-sprite.getWidth()/2,-sprite.getHeight()/2);
}
#Override
public void dispose() {
batch.dispose();
texture.dispose();
}
#Override
public void render() {
// Make the background color Black
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
// Mouse imput
if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
sprite.setPosition(Gdx.input.getX() - sprite.getWidth()/2,
Gdx.graphics.getHeight() - Gdx.input.getY() - sprite.getHeight()/2);
}
if(Gdx.input.isButtonPressed(Input.Buttons.RIGHT)){
sprite.setPosition(Gdx.graphics.getWidth()/2 -sprite.getWidth()/2,
Gdx.graphics.getHeight()/2 - sprite.getHeight()/2);
}
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.end();
}
I have no idea what I'm doing wrong

Moving a Sprite using a Scene2d button

I am creating an Android Game using LibGdx. It is a platformer and the map is tiled based. To test the movements of the player I used Key inputs and the desktop version of the game works fine. I created some buttons in scene2d and added them as an actor to the scene so that the game has movement buttons when played on Android devices. The buttons work as "System.out.print" shows. Problem is: the buttons and the player are each created in a different class. I can't seem to modify the velocity (and so the movement) of the Player from the class that holds the buttons. For that I need to change the velocity and speed etc. to static, which gives me strange errors on an Android device (Player won't show, or disappears after a frame). I am not sure how to fix this and what is the actual cause of this error. Here is some of the code of the different Classes:
Main Class (MyGdxGame) only included one button as an example.
public class MyGdxGame extends Game implements ApplicationListener {
private Skin skin;
private Stage stage;
#Override
public void create() {
setScreen(new Play());
skin = new Skin(Gdx.files.internal("ui/defaultskin.json"));
stage = new Stage();
Gdx.input.setInputProcessor(stage);
//Button Right
TextButton buttonRight = new TextButton("Right", skin, "default");
buttonRight.setWidth(50f);
buttonRight.setHeight(50f);
buttonRight.setPosition(Gdx.graphics.getWidth() /2 - 250f, Gdx.graphics.getHeight()/2 - 200f);
buttonRight.addListener(new ClickListener(){
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
System.out.println("Hold");
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
System.out.print("Released");
}
});
stage.addActor(buttonRight);
}
Play Class
public class Play implements Screen {
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private OrthographicCamera camera;
private Player player;
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.position.set(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2, 0);
camera.update();
renderer.setView(camera);
renderer.render();
renderer.getSpriteBatch().begin();
player.draw(renderer.getSpriteBatch());
renderer.getSpriteBatch().end();
}
#Override
public void resize(int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
}
#Override
public void show() {
map = new TmxMapLoader().load("maps/map.tmx");
renderer = new OrthogonalTiledMapRenderer(map);
camera = new OrthographicCamera();
player = new Player(new Sprite(new Texture("data/jack2.png")), (TiledMapTileLayer) map.getLayers().get(0));
player.setPosition(2 * player.getCollisionLayer().getTileWidth(), 10 * player.getCollisionLayer().getTileHeight());
}
Player Class
public class Player extends Sprite implements InputProcessor{
// the movement velocity //
public Vector2 velocity = new Vector2();
public float speed = 60 * 2, gravity = 60 * 1.8f;
private boolean canJump;
private TiledMapTileLayer collisionLayer;
private String blockedKey = "blocked";
public Player(Sprite sprite, TiledMapTileLayer collisionLayer){
super(sprite);
this.collisionLayer = collisionLayer;
}
#Override
public void draw(SpriteBatch spriteBatch) {
update(Gdx.graphics.getDeltaTime());
super.draw(spriteBatch);
}
So the button has a working ClickListener, but I don't know how it can modify the players velocity. Any help is welcome.
You just need to have some way to access that player instance from the class containing the button. In my games I make a static World class which can be accessed from anywhere in the program. Through the world class I can access the player instance. That's just how I do it, you may find another way. You say your program has errors on android device when making certain parameters static? Do you mean it compiles but it just doesn't work the way you expect? If that's the case, there is probably just a bug in how you're modifying the position or velocity of the player.

Categories