I have been trying to render a tiled map to my LibGDX project but nothing shown on the screen, I followed this tutorial from here http://www.gamefromscratch.com/post/2014/04/16/LibGDX-Tutorial-11-Tiled-Maps-Part-1-Simple-Orthogonal-Maps.aspx.
Could the problem be that I didn't set the position of the camera right?
This is my code
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.maps.tiled.AtlasTmxMapLoader;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.scenes.scene2d.Stage;
public class Level extends ApplicationAdapter implements InputProcessor {
ActorDemo jet;
Stage stage;
TiledMap tiledMap;
OrthographicCamera camera;
TiledMapRenderer tiledMapRenderer;
AtlasTmxMapLoader test;
#Override
public void create () {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
jet = new ActorDemo();
stage = new Stage();
stage.addActor(jet);
camera = new OrthographicCamera();
camera.setToOrtho(false,w/2,h/2);
camera.update();
tiledMap = new TmxMapLoader().load("Level.tmx");
tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
Gdx.input.setInputProcessor(this);
}
#Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
tiledMapRenderer.setView(camera.combined,0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
tiledMapRenderer.render();
stage.draw();
}
#Override
public boolean keyDown(int keycode) {
return false;
}
#Override
public boolean keyUp(int keycode) {
if(keycode == Input.Keys.LEFT)
camera.translate(-32,0);
if(Gdx.input.isKeyPressed(Input.Keys.A)){
camera.position.x -=2;
}
if(keycode == Input.Keys.RIGHT)
camera.translate(32,0);
if(keycode == Input.Keys.UP)
camera.translate(0,-32);
if(keycode == Input.Keys.DOWN)
camera.translate(0,32);
if(keycode == Input.Keys.NUM_1)
tiledMap.getLayers().get(0).setVisible(!tiledMap.getLayers().get(0).isVisible());
if(keycode == Input.Keys.NUM_2)
tiledMap.getLayers().get(1).setVisible(!tiledMap.getLayers().get(1).isVisible());
return false;
}
#Override
public boolean keyTyped(char character) {
return false;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
return false;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
#Override
public boolean scrolled(int amount) {
return false;
}
}
Screenshot of the Desktop configuration
Here is where I put my tiled map
This is the tile set I used to create my tmx file
I added this tile set to to the tiled map editor and created a tile sheet and then put it in my project
It worked, the problem was that the program was not able to find the tile Set, so I changed the path(from the .tmx file) to where it actually located and it worked.
Related
I don't know how I can clear the buffer in libGDX, in this case,
GL30.GL_COLOR_BUFFER_BIT doesn't work.
package com.mygdx.game.Screens;
import Artifacts.Champ;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton.ImageButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.mygdx.game.MyGdxGame;
import java.util.ArrayList;
public class GameScreen implements Screen {
private Stage stage;
private Game game;
ArrayList <Champ> champs=new ArrayList();;
public GameScreen(Game aGame) {
game = aGame;
stage = new Stage(new ScreenViewport());
ImageButton pj;
Table camp;
champs.add(new Champ("Ximet",new Texture("Champ/Ximet.png")));
champs.add(new Champ("Jordi",new Texture("Champ/Jordi.jpg")));
champs.add(new Champ("Camilo",new Texture("Champ/Camilo.jpg")));
champs.add(new Champ("Vicent",new Texture("Champ/Vicent.jpg")));
int x=100, y=250;
for (final Champ champ : champs) {
camp=new Table();
pj=new ImageButton(new SpriteDrawable(new Sprite(champ.getLogo())));
pj.setPosition(x, y);
camp.add(pj).size(100, 100);
camp.setPosition(x, y);
x=x+100;
camp.addListener(new InputListener(){
#Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
Label name = new Label(champ.nom, MyGdxGame.gameSkin,"big-black");
name.setPosition(10,400);
stage.addActor(name);
}
#Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}
});
stage.addActor(camp);
}
Label title = new Label("Select Champ", MyGdxGame.gameSkin,"big-black");
title.setAlignment(Align.center);
title.setY(Gdx.graphics.getHeight()*2/3);
title.setWidth(Gdx.graphics.getWidth());
stage.addActor(title);
TextButton backButton = new TextButton("Back",MyGdxGame.gameSkin);
backButton.setWidth(Gdx.graphics.getWidth()/2);
backButton.setPosition(Gdx.graphics.getWidth()/2-backButton.getWidth()/2,Gdx.graphics.getHeight()/4-backButton.getHeight()/2);
backButton.addListener(new InputListener(){
#Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
game.setScreen(new Principal((MyGdxGame)game));
}
#Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}
});
stage.addActor(backButton);
}
#Override
public void show() {
Gdx.app.log("MainScreen","show");
Gdx.input.setInputProcessor(stage);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
stage.dispose();
}
}
That screen shows you all the players and when you pick one of them, on the topside will appear her name, it works, but when I pick another different, the name still there.
P.S.1 I'm begginer, just learning LigGDX picking code, and I'm trying to do a rpg game.
P.S.2 For-loop prints on the screen the arraylist of champions, adding them to a button for select one of them, if you know another way more easy and optimized, I'll appreciate.
P.S.3 I know that initialize in the loop the table isn't recommended. If I initialize outside the loop, when I pick a champion, in the topside shows the name of all the objects in the arraylist.
I recently started programming an app in LibGDX. With this app, one can only now press on boxes which are then filled in blue.
In principle everything works. The problem is only if I move the orthographic camera, or start to zoom, then my input processor still remains in the same place.
In short. Because the camera is moved or zoomed, the input does not work properly.
I have two times here Schreenshots attached so you can see what I mean.
The red dots are always where I pressed.
Regards Timux ;D
Here it works correctly
No more
No problem when I move or zoom my camera, I've tested, you can check this.
May be you're not using unproject() method of camera that translate a point given in screen coordinates to world space.
public class GdxText extends ApplicationAdapter implements InputProcessor {
OrthographicCamera cam;
Texture texture;
Sprite firstSprite,secondSprite;
SpriteBatch spriteBatch;
Vector3 vector3;
#Override
public void create() {
vector3=new Vector3();
cam=new OrthographicCamera();
texture=new Texture("badlogic.jpg");
float w=Gdx.graphics.getWidth();
float h=Gdx.graphics.getHeight();
spriteBatch=new SpriteBatch();
firstSprite=new Sprite(texture);
firstSprite.setSize(100,100);
firstSprite.setPosition(w/2-150,h/2-50);
secondSprite=new Sprite(texture);
secondSprite.setSize(100,100);
secondSprite.setPosition(w/2+50,h/2-50);
Gdx.input.setInputProcessor(this);
}
#Override
public void render() {
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
spriteBatch.setProjectionMatrix(cam.combined);
spriteBatch.begin();
firstSprite.draw(spriteBatch);
secondSprite.draw(spriteBatch);
spriteBatch.end();
}
#Override
public void resize(int width, int height) {
cam.setToOrtho(false,width,height);
cam.update();
}
#Override
public boolean keyDown(int keycode) {
if(keycode== Input.Keys.UP) {
cam.zoom -= cam.zoom * .1;
cam.update();
}else if(keycode==Input.Keys.DOWN) {
cam.zoom += cam.zoom * .1;
cam.update();
}
if(keycode== Input.Keys.LEFT) {
cam.position.add(2f,0,0);
cam.update();
}else if(keycode==Input.Keys.RIGHT) {
cam.position.add(-2f,0,0);
cam.update();
}
return false;
}
#Override
public boolean keyUp(int keycode) {
return false;
}
#Override
public boolean keyTyped(char character) {
return false;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
vector3.set(screenX,screenY,0);
Vector3 ori=cam.unproject(vector3);
if(firstSprite.getBoundingRectangle().contains(ori.x,ori.y))
System.out.println("Touch on First Sprite");
if(secondSprite.getBoundingRectangle().contains(ori.x,ori.y))
System.out.println("Touch on Second Sprite");
return false;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
#Override
public boolean scrolled(int amount) {
return false;
}
#Override
public void dispose() {
spriteBatch.dispose();
texture.dispose();
}
}
I'm developing a cross-plattform application in java (focusing on android right now) and I'm mainly using libgdx to achieve this. Right now I have a screen that consists of rows of clickable buttons where the rows extend below the screen. I want to allow the user to touch/flick scroll up and down in order for them to view all the different buttons available.
Some relevant information: I'm using an Orthographic Camera and an InputProcessor.
Right now I'm thinking that the camera is supposed to move up and down based on the touchDragged method in the InputProcessor.
I'll attach the relevant code in the GameRenderer and InputHandler classes below. (with a lot of unrelevant code removed.) Any help or suggestions would be great, I've googled like a mad man and been stuck for a day now.
GameRenderer
public class GameRenderer {
private GameWorld myWorld;
private OrthographicCamera cam;
private SpriteBatch batcher;
private ArrayList<Element> elements = new ArrayList<Element>();
private int gameHeight;
public GameRenderer(GameWorld world, int gameHeight) {
myWorld = world;
elements = myWorld.getElement();
this.gameHeight = gameHeight;
cam = new OrthographicCamera();
cam.setToOrtho(true, 136, gameHeight);
batcher = new SpriteBatch();
batcher.setProjectionMatrix(cam.combined);
}
public void render() {
Gdx.gl.glClearColor(0, 191, 255, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batcher.begin();
for (int i = 0; i < elements.size(); i++) {
Element e = (Element) elements.get(i);
batcher.draw(AssetLoader.element, e.getLocationX(), e.getLocationY(), e.getElementWidth(), e.getElementHeight());
AssetLoader.font.draw(batcher, e.getFormula(), e.getLocationX() + 4, e.getLocationY() + 3);
}
batcher.end();
}
}
InputHandler
public class InputHandler implements InputProcessor
{
private Element myElement;
private float scaleX, scaleY;
private ArrayList<Element> elements;
GameWorld myWorld;
GameRenderer myRenderer;
public InputHandler(GameWorld game, GameRenderer renderer, ArrayList elementList, float x, float y) {
myWorld = game;
myRenderer = renderer;
elements = elementList;
scaleX = x;
scaleY = y;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
return true;
}
#Override
public boolean keyDown(int keycode) {
return false;
}
#Override
public boolean keyUp(int keycode) {
return false;
}
#Override
public boolean keyTyped(char character) {
return false;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
#Override
public boolean scrolled(int amount) {
return false;
}
}
Anyone know why does it become red or error in android studio and how to fix it?
Here is the code
package com.appalyse.deliopblaster;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class MyGdxGame extends ApplicationAdapter implements InputProcessor {
private SpriteBatch batch;
private BitmapFont font;
private int screenWidth, screenHeight;
private String message = "Touch me";
#Override
public void create () {
batch = new SpriteBatch();
screenWidth = Gdx.graphics.getWidth();
screenHeight = Gdx.graphics.getHeight();
font = new BitmapFont();
font.setColor(Color.GREEN);
font.getData().scale(5);
Gdx.input.setInputProcessor(this);
}
#Override
public void dispose() {
batch.dispose();
font.dispose();
}
#Override
public void render () {strong text
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
BitmapFont.*TextBounds* = font.*getBounds*(message);
float x = screenWidth/2 - *textSize*.width/2;
float y = screenHeight/2 + *textSize*.height/2;
batch.begin();
font.draw(batch, message, x, y);
batch.end();
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
message = "Touch down at " + screenX + ", " + screenY;
return true;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
message = "Touch up at " + screenX + ", " + screenY;
return true;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
//message = "Dragging at" + screenX + ", " + screenY;
return true;
}
#Override
public boolean keyDown(int keycode) {
return false;
}
#Override
public boolean keyUp(int keycode) {
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;
}
}
It become error at
BitmapFont.*TextBounds* = font.*getBounds*(message);
float x = screenWidth/2 - *textSize*.width/2;
float y = screenHeight/2 + *textSize*.height/2;
Error at TextBounds , getBounds and textSize
I want to know the method for fixing it.
1.Javascript and Java are different languages. So your tag should be removed.
2.Java does not have * as valid syntax like you do it. * is just for multiplication purposes.
3.With libGDX 1.6 there were huge changes for font handling. The "getBounds()" method no longer exists. To get width and height of an text try this:
GlyphLayout glyphLayout = new GlyphLayout();
glyphLayout.setText(font, message);
float x = screenWidth/2 - glyphLayout.width/2;
float y = screenHeight/2 + glyphLayout.height/2;
I have written the following code. It is simple rectangle in middle of the screen. I want the rectangle to move -50 in y direction onClick (direction is just for illustration purposes only). Despite trying various methods. I cannot get the code to work. I am not sure if I am doing something fundamentally wrong. I would appreciate some direction from better learned minds.
package com.mygdx.gameobjects;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
public class Hero extends Rectangle implements InputProcessor{
private Rectangle hero;
private Vector2 position;
public Hero(){
position = new Vector2(x, y);
hero = new Rectangle(position.x+68-10, position.y+204-30-20, 20, 20);
}
public void update (float delta) {
}
public void onClick() {
position.y = -50;
}
public Rectangle getHero() {
return hero;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
onClick();
return true;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
#Override
public boolean keyDown(int keycode) {
return false;
}
#Override
public boolean keyUp(int keycode) {
return false;
}
#Override
public boolean keyTyped(char character) {
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
#Override
public boolean scrolled(int amount) {
return false;
}
}