So I have a splash image that is not rendering and I'm not really sure why. I have done the correct process, creating anew sprite and then in my render method rendering it with the batch like so:
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
splashSprite.draw(batch);
batch.end();
}
#Override
public void show() {
splashSprite = new Sprite(new Texture("data/xidstudios_splash.png"));
batch = new SpriteBatch();
}
As you can see, I have done the correct process of steps, am I missing one?
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
splashSprite.draw(batch);
batch.end();
}
You must provide the viewports to opengl either using a camera or directly calling the opengl func so that your projection are currectly set to view the image
Related
When I try to rotate an object around its axis around its axis a sprite of game objects I using the rotate() and setRotation() commands, the sprite does not rotate.
Also, when I try to rotate the actor I using the rotateBy() and setRotation() commands, nothing happens too.
In my project, drawing game objects (actors) occurs via the draw() method in Stage.
I noticed that if I draw an object using the batch.draw(sprite, x, y) method, the object will not rotate.
code from public class MyGdxGame extends ApplicationAdapter
#Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(sprite, 0, 0);
sprite.rotate(1);
batch.end();
}
And if I draw an object using the sprite.draw(batch) method, the object rotates calmly.
#Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
sprite.draw(batch);
sprite.rotate(1);
batch.end();
}
Perhaps this is the reason for my problem.
I'm developing a game and in the render function I have:
(Square extends Sprite)
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
backgroundSprite.draw(batch);
endTime = System.currentTimeMillis();
if((endTime-startTime) > 1000) {
squareList.add(squarePositionFactory.getSquare());
for(Square square : squareList) {
square.setSize(80, 80);
square.draw(batch);
square.updatePosition(square.posX, square.posY + 100);
startTime = System.currentTimeMillis();
Gdx.app.log("[Playing time]", square.toString());
}
}
batch.end();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
The problem is the render method only works when I click repeatdly on the screen so the images are displayed blinking. How can I fix this? Thanks!
You may find this page useful.
you haven't add the actor to the stage.
stage.addActor("your actor");
stage.draw();
Android Studio-libgdx making a 2d game.
I have a Main which creates the MenuState, in the render method I made this
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
myGame.batch.setProjectionMatrix(camera.combined);
batch = new SpriteBatch();
skin = new Skin(Gdx.files.internal("data/uiskin.json"));
stage = new Stage();
final TextButton button = new TextButton("Click me", skin, "default");
button.setWidth(400f);
button.setHeight(100f);
button.setPosition(Gdx.graphics.getWidth() / 2 - 100f, Gdx.graphics.getHeight() / 2 - 10f);
button.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
});
stage.addActor(button);
Gdx.input.setInputProcessor(stage);
myGame.batch.begin();
stage.draw();
myGame.batch.end();
}
I checked some tutorials on how to make a menu and that's pretty much what I'm testing in here.
But when I run the app the button appears and nothing happens if I click it.
I've tried testing with the ClickListener but still doesn't work. Any ideas? Or am I doing it wrong or it should be in another method?
I'm new with libgdx. Thanks in advance.
You are creating new instances of the stage, skin and batch in render method? Move them away from your render method ;)
Render should look more like this:
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
myGame.batch.setProjectionMatrix(camera.combined);
myGame.batch.begin();
stage.draw();
myGame.batch.end();
}
And in constructor:
batch = new SpriteBatch();
skin = new Skin(Gdx.files.internal("data/uiskin.json"));
stage = new Stage();
final TextButton button = new TextButton("Click me", skin, "default");
button.setWidth(400f);
button.setHeight(100f);
button.setPosition(Gdx.graphics.getWidth() / 2 - 100f, Gdx.graphics.getHeight() / 2 - 10f);
button.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
});
stage.addActor(button);
Gdx.input.setInputProcessor(stage);
http://badlogicgames.com/forum/viewtopic.php?f=11&t=2447
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/OrthographicCamera.html
There's hundreds of other links I could show you that I've looked at, but it's just not worth it because they all say the same thing.
public class InGame implements Screen {
SpriteBatch batch;
GameWorld world;
OrthographicCamera camera;
#Override
public void show() {
batch = new SpriteBatch();
world = new GameWorld();
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
world.render(batch, delta);
batch.end();
}
}
What am I doing wrong? WHY is my world still being rendered with the 0,0 being at the bottom right. The math behind this while trying to work on my Tile-System is driving me absolutely insane.
World->Render
public void render(SpriteBatch batch, float delta) {
for(int xAxis = 0; xAxis < worldColumns; xAxis++) {
for(int yAxis = 0; yAxis < worldRows; yAxis++) {
tiles[xAxis][yAxis].render(batch, delta);
}
}
}
WorldTile->Render
public void render(SpriteBatch batch, float delta) {
myShape.begin(ShapeType.Filled);
myShape.setColor(tileColor);
myShape.rect(pos.getX(), pos.getY(), TILE_WIDTH, TILE_HEIGHT);
myShape.end();
}
The "pos" is the Position(x, y) that was passed in the World class.
If you are drawing a Sprite or TextureRegion using your SpriteBatch the code should work fine. However, you pass your SpriteBatch 'batch' all the way down to WorldTile.render and never use it?! Instead you use myShape which I assume is a ShapeRenderer. You need to set the projection matrix for the ShapeRenderer as well otherwise it will draw 'upside-down'.
Try calling myShape.setProjectionMatrix(camera.combined); before you use your myShape.
Its probably best to declare and initialise myShape in your InGame class, usemyShape.setProjectionMatrix(camera.combined);, and then pass myShape down to tiles.render() as you did with your SpriteBatch.
Hope this helps.
How can I move Sprite fluently?I have tried in this way.
#Override
public void render(float delta) {
Gdx.graphics.getGL20().glClearColor( 1F, 1F, 1F, 1F );
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
mySprite.draw(batch);
mySprite.setX(BladeAnimation.bladeFalling());
batch.end();
}
I have changed xPosition with other function but the animation is not playing fluently.
You have to run the code that is changing the position in the render() method, other wise it changes only once. Unless if you are changing the position elsewhere. Show the code that changes the position to get a better answer