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();
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.
Why does this
player.sprite.draw(batch);
does not render my sprite, but
batch.draw(player.sprite.getTexture(), 0,0 );
does?
My complete render method:
public void render(float deltaTime) {
if (assets.assetManager.update()) {
loading = false;
player = new Cat(assets.assetManager.get("textures/Cat.png", Texture.class));
player.sprite.setPosition(0,0);
}
else
{
timeLoading += deltaTime;
System.out.println("Progress: " + assets.assetManager.getProgress() * 100);
}
if (!loading)
{
rayHandler.updateAndRender();
Gdx.gl.glClearColor(0.37f, 0.73f, 0.84f, 1f);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
processUserInput();
player.update(deltaTime);
orthogonalTiledMapRenderer.setView(orthographicCamera);
orthogonalTiledMapRenderer.render();
batch.begin();
if (debug)
renderDebugInfo();
player.sprite.draw(batch);
batch.draw(player.sprite.getTexture(), 0,0 );
batch.end();
batch.setProjectionMatrix(orthographicCamera.projection);
rayHandler.setCombinedMatrix(orthographicCamera);
orthographicCamera.update();
world.step(1, 4, 4);
}
}
I had not used the Constructor of Sprite, and set the Texture with Sprite.setTexture, but this somehow did not proper initialize the Sprite. Now with using the Constructor I can render it
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);
as the title says I'm trying to display a timer in my game that starts from 0 (and ideally, I want it to be on the top left of the screen)
I have the logic for the timer here:
public class Timer {
SpriteBatch batch;
private BitmapFont font;
private float deltaTime = 0;
CharSequence str;
public Timer() {
font = new BitmapFont();
batch = new SpriteBatch();
}
public void drawTime() {
deltaTime += Gdx.graphics.getDeltaTime();
str = Float.toString(deltaTime);
font.draw(batch, str, 0, 0);
}
}
I call this timer in my main class (Game) in the render() method like so:
public void render() {
player.update();
platform1.update();
platform2.update();
batch.begin();
Gdx.gl.glClearColor(135/255f, 206/255f, 235/255f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
flag.drawS(batch);
flag.draw(batch);
player.draw(batch);
platform1.draw(batch);
platform2.draw(batch);
timer.drawTime();
batch.end();
}
}
I get the error "SpriteBatch begin must be called before draw", so I tried moving the timer.drawTime() method in different places in render() but still no luck.
Anyone know what could be wrong? Any help is highly appreciated :)
You should not create SpriteBatch() inside your Timer object. SpriteBatch should be created once and used by multiple elements to draw themselves. Your Timer draw() method should look more like this:
public void drawTime(SpriteBatch batch) {
deltaTime += Gdx.graphics.getDeltaTime();
str = Float.toString(deltaTime);
font.draw(batch, str, 0, 0);
}
The specific error you are encountering is caused by the fact that you call batch.begin(); on a different SpriteBatch object then the one that gets used in drawTime().
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