the code below keeps rendering a black screen...any ideas why? I put the base.tmx in the desktop folder and created it using tiled. did i put the .tmx in the wrong folder? its driving me nuts.
public class GameScreen extends ScreenAdapter {
OrthographicCamera camera;
TiledMap tiledmap;
TiledMapRenderer tiledMapRenderer;
public void show()
{
camera=new OrthographicCamera();
camera.setToOrtho(false);
camera.update();
tiledmap= new TmxMapLoader().load("base.tmx");
tiledMapRenderer=new OrthogonalTiledMapRenderer(tiledmap);
}
public void render()
{
Gdx.gl.glClearColor(1, 1, 1, 1);
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);
tiledMapRenderer.render();
}
}
You are not setting width and height of your camera. Also you should override render method of ScreenAdapter correctly with delta parameter. This is the updated version of your code :
public class GameScreen extends ScreenAdapter {
OrthographicCamera camera;
TiledMap tiledmap;
TiledMapRenderer tiledMapRenderer;
public void show()
{
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera=new OrthographicCamera();
camera.setToOrtho(false,w,h);
camera.update();
tiledmap= new TmxMapLoader().load("base.tmx");
tiledMapRenderer=new OrthogonalTiledMapRenderer(tiledmap);
}
public void render(float delta)
{
Gdx.gl.glClearColor(1, 1, 1, 1);
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);
tiledMapRenderer.render();
}
}
Related
This is my code:
Gdx.input.setInputProcessor(this);
bagImage = new Image(new Texture("bag.png"));
bagButton = new ImageButton(bagImage.getDrawable());
bagButton.setSize(125, 125);
bagButton.addListener(new ClickListener() {
public void clicked(InputEvent event, float x, float y) {
Gdx.app.debug("DEBUG", "clicked");
}
});
}
If I click on the button nothing happens. Why?
You should have stage and add ImageButton to this stage then setInputProcessor to this stage and you can use only Image instead of ImageButton since you don't use imageUp, imageDown.....
you code should be like this :
Stage stage = new Stage();
Gdx.input.setInputProcessor(stage);
bagImage = new Image(new Texture("bag.png"));
bagImage.setSize(125, 125);
stage.addActor(bagImage);
bagImage.addListener(new ClickListener() {
public void clicked(InputEvent event, float x, float y){
Gdx.app.debug("DEBUG", "clicked");
}
});
#Override
public void render(float delta) {
stage.act(delta);
stage.draw();
}
I'm new to LibGDX and I am trying to get my screen resolution sizes set up first before I get into the actual game itself. Before I added extendViewport I had an orthographic Camera and an Image that displayed. But when I got rid of the orthographic camera and added the Viewport the image disappeared. Here is the code i have for the the screen.
public class GameScreen implements Screen {
SlingshotSteve game;
private ExtendViewport viewport;
PerspectiveCamera camera;
public void Menu(SlingshotSteve game){
this.game = game;
}
SpriteBatch batch;
TextureRegion backgroundTexture;
Texture texture;
GameScreen(final SlingshotSteve gam) {
this.game = gam;
batch = new SpriteBatch();
Texture texture = new Texture(Gdx.files.internal("background.jpg"));
backgroundTexture = new TextureRegion(texture, 0, 0, 500, 500);
Music mp3Sound = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
mp3Sound.setLooping(true);
mp3Sound.play();
}
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(backgroundTexture, 0, 0, SlingshotSteve.WIDTH, SlingshotSteve.HEIGHT);
batch.end();
}
#Override
public void resize(int width, int height) {
viewport.update(width, height);
}
#Override
public void dispose() {
batch.dispose();
texture.dispose();
}
#Override
public void show() {
camera = new PerspectiveCamera();
viewport = new ExtendViewport(800, 480, camera);
}
}
The other question I have is that this screen is the main game screen where Level 1 is going to occur. Since I added a main menu I had to switch from the "Application Listener" to the "Implement Screen". Is this correct, or do I have to go back to the Application Listener to get the Create() method? I'm not completely sure what everything means so If someone could please explain this to me that would be great!
This is the java file created by gdx-setup-ui.jar of v0.9.7
sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);
Why we need to set position to negative value in order to center the picture?
Where is the reference base point of the libgdx picture? (left-bottom corner?)
I was told that the origin of libgdx is left-bottom corner. Given the above values, part of the picture should have been outside the screen....It turns out not! I am very confused.
Thanks in advance
Complete listing:
package com.packtpub.libgdx.basic;
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;
public class Basic implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
#Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, h/w);
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("data/libgdx.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);
sprite = new Sprite(region);
sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);
}
#Override
public void dispose() {
batch.dispose();
texture.dispose();
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
It's because the default center point for the orthographic camera is (0,0,0), so if you just draw your image at (0,0) its bottom left cornet will be in the center of the screen.
You can change this center point by using cam.position.set(w / 2, h / 2, 0) method
See here for more detailed example of OrthographiCamera use: https://github.com/libgdx/libgdx/wiki/Orthographic-camera
I'm developping games with LIBGDX on ANDROID. Today, i've tried to generate one of my project in a HTML5 version. I put the content of the WAR folder on my server. All is fine except 2 things. I'll present you here just one of these 2 issues.
The problem : when a texture has to be disposed (by the call of its method dispose()), i get a BufferUnderflowException. It happens everytime.
Here is the sample code which is automatically generated when you create a new project :
public class TexDispose implements ApplicationListener
{
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
#Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, h/w);
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("data/libgdx.png"));
Gdx.input.setInputProcessor(this);
}
#Override
public void dispose() {
batch.dispose();
texture.dispose(); // HERE IS THE ERROR
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.end();
}
#Override
public void resize(int width, int height) {
}
}
Has one of you already met this issue..? If yes, how can i avoid that (except by not disposing anything lol) ?
Thank you ! ;)
I am new to lib gdx and facing problem of proper rendering. While I am moving Actor , it is moving fine but last position frame is not cleared from screen , so whole path is appearing while moving .
It is like if object 1 move then it appear like 11111111111111111111111111111 while it should be only at last point like 1.
My actor
public class SinglePipe extends Actor {
#Override
public void draw(SpriteBatch batch, float parentAlpha) {
batch.setColor(getColor().r, getColor().g, getColor().b, getColor().a);
batch.draw(Assets.car, this.getX(), this.getY());
}
#Override
public void act(float delta) {
// TODO Auto-generated method stub
super.act(delta);
}
}
Screen class
public class GameScreen implements Screen, GestureListener {
private Stage stage;
private SinglePipe singlePipe;
Logger logger = new Logger("");
public GameScreen() {
// Gdx.graphics.setContinuousRendering(false);
logger.setLevel(5);
stage = new Stage();
singlePipe = new SinglePipe();
stage.addActor(singlePipe);
}
#Override
public void render(float delta) {
// camera.update();
handleInput();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_GREEN_BITS);
stage.act();
// stage.draw();
}
private void handleInput() {
if (Gdx.input.isKeyPressed(Input.Keys.A)) {
// singlePipe.addAction(Actions.moveBy(5, 5));
// singlePipe.addAction(Actions.rotateBy(180));
singlePipe.setX(singlePipe.getX() + 5);
singlePipe.setY(singlePipe.getY() + 5);
}
}
}
In your render method use:
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);`