LibGDX - trying to display a visible timer in my game - java

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().

Related

How to clear a JPanel

I am working on a 2d game using Swing. Before, I used to render my objects and player on a jPanel over the
panel.repaint();
method and would override the paint methode in the panel class. Then I learned about the concept of moving the render code to a Render class which looks like this:
public class Renderer{
public void render(Graphics g, Game game){
game.getObjects.forEach(gameObject -> g.drawImage(....);
}
}
With that code there is always a drawing on a drawing, ....
The problem with this is that I can't (or don't know how to) call the super method repaint() of the panel.
I would like to keep the Render class because the code is much more structured. Any advice on how to reset a jPanel?
I tried using panel.repaint() before calling the render method but I just got a blank screen.
public class Renderer {
public void render(Game game, Graphics graphics) {
Player player = game.getPlayer();
graphics.drawImage(player.getImage(), (int)player.getPosition().getX(), (int)player.getPosition().getY(), null);
}
}
public class Game{
private static Game instance;
private GamePanel gamePanel;
private Player player;
private Renderer renderer;
private boolean isRunning = true;
private final int MAX_FPS = 60;
private Game() {
initialize();
startGameLoop();
}
private void initialize() {
renderer = new Renderer();
player = Player.getInstance();
gamePanel = GamePanel.getInstance(this);
GameWindow.getInstance(gamePanel);
}
private void startGameLoop() {
double timePerCycle = 1_000_000_000 / MAX_FPS;
int updates = 0;
long lastInfo = System.currentTimeMillis();
long timeBefore = System.nanoTime();
while(isRunning) {
if(System.nanoTime() - timeBefore >= timePerCycle) {
timeBefore = System.nanoTime();
update();
render();
updates++;
}
if(System.currentTimeMillis() - lastInfo >= 1000) {
System.out.printf("UPS: %d\n", (updates / (( System.currentTimeMillis() - lastInfo) / 1000)));
lastInfo = System.currentTimeMillis();
updates = 0;
}
}
}
private void render() {
Graphics graphics = gamePanel.getGraphics();
renderer.render(this, graphics);
graphics.dispose();
}
To clear the panel you can employ a boolean in paintComponent and fill in the rectangle via g.fillRect(x,y, width, height).
Her is one possible example. Where boolean clearScreen is an instance field.
public void clear() {
clearScreen = true; // tested in paintComponent
repaint();
clearScreen = false;
}
Here are some other suggestions.
don't override paint for JPanel. Use paintComponent.
first statement should be super.paintComponent(g). This is what allows panel.setBackground() to work, among other things as it calls the overridden method to perform additional functionality.
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// your stuff here
}
Use a Swing Timer for controlling repaint cycles.
painting and event handling are done in the EventDispatch thread. So processing should be kept to a minimum. Any computations required for your game should be done outside that thread and when possible, only the actual invocation of the graphics methods should be done in the paintComponent method.
If done properly, subsequent calls to repaint() will not add to what is there. Each call must redraw everything including changes.
For more information check out How to paint
There are many examples of painting on this site. Search for them using [Swing] and [Graphics] tags. Here is one that employs some of the above. Also note that Swing components enable double buffering by default

LibGdx update with deltatime is not precise

I'm new in creating game.
I have several boxes that move from top to bottom of screen and i need to make 1 unit distance between them.
Every thing works fine except the distance is not precise and i think the deltatime cause this problem
This is how i move the boxes :
objX -= deltatime * speed;
Update
time += deltatime :if(time >= 3.0f) spawn
Im using libgdx
You can schedule your task in this way :
Array<Sprite> sprites;
Texture texture;
SpriteBatch batch;
float speed=35;
#Override
public void create() {
sprites=new Array<Sprite>();
texture=new Texture("badlogic.jpg");
batch=new SpriteBatch();
Timer.instance().scheduleTask(new Timer.Task() {
#Override
public void run() {
spawn();
}
},1,2);
}
private void spawn(){
Sprite sprite=new Sprite(texture);
sprite.setSize(50,50);
sprite.setPosition(Gdx.graphics.getWidth()/2,0);
sprites.add(sprite);
}
#Override
public void render() {
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
float delta=Gdx.graphics.getDeltaTime();
batch.begin();
for (Sprite sprite:sprites){
sprite.setY(sprite.getY()+delta*speed);
sprite.draw(batch);
}
batch.end();
}
After a lot of struggling i came up with an idea which is
instead of moving boxes and spawning them by deltatime , make boxes static and move camera over them!
So i just move camera here
public void render(float delta) {
posCameraDesired.y += 2000.0f * Gdx.graphics.getDeltaTime();
guiCam.position.lerp(posCameraDesired, 0.1f);
SemiRender();
}
and i have static Y that i'll update after each spawn
lastY = 10 + lastY + ((width / 4) / 2) * 3;
and some other logic to recycle them when they passed the viewport.
and every one is happy!

Atlas does not render images in order[libGDX]

I am using the following class to render an atlas on screen:
public class AnimationDemo implements ApplicationListener {
private SpriteBatch batch;
private TextureAtlas textureAtlas;
private Animation animation;
private float elapsedTime = 0;
#Override
public void create() {
batch = new SpriteBatch();
textureAtlas = new TextureAtlas(Gdx.files.internal("data/packOne.atlas"));
animation = new Animation(1 / 1f, textureAtlas.getRegions());
}
#Override
public void dispose() {
batch.dispose();
textureAtlas.dispose();
}
#Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
//sprite.draw(batch);
elapsedTime += Gdx.graphics.getDeltaTime();
batch.draw(animation.getKeyFrame(elapsedTime, true), 0, 0);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
I am a beginner with libGDX, however with the above program my images are not rendered in order as random images appear. I was earlier using the following with the same . atlas file and it was working properly:
public class MyGdxGame implements ApplicationListener {
private SpriteBatch batch;
private TextureAtlas textureAtlas;
private Sprite sprite;
private int currentFrame = 1;
private String currentAtlasKey = new String("0001");
#Override
public void create() {
batch = new SpriteBatch();
textureAtlas = new TextureAtlas(Gdx.files.internal("data/packOne.atlas"));
TextureAtlas.AtlasRegion region = textureAtlas.findRegion("0001");
sprite = new Sprite(region);
sprite.setPosition(Gdx.graphics.getWidth() / 2 - sprite.getWidth() / 2, Gdx.graphics.getHeight() / 2 - sprite.getHeight() / 2);
sprite.scale(4.5f);
Timer.schedule(new Timer.Task() {
#Override
public void run() {
currentFrame++;
if (currentFrame > 393)
currentFrame = 1;
// ATTENTION! String.format() doesnt work under GWT for god knows why...
currentAtlasKey = String.format("%04d", currentFrame);
sprite.setRegion(textureAtlas.findRegion(currentAtlasKey));
}
}
, 0, 1 / 30.0f);
}
#Override
public void dispose() {
batch.dispose();
textureAtlas.dispose();
}
#Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
sprite.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
Any hints about what might be wrong here?
I am also trying to adapt my program with Screen Viewport any headings as in how to implement this would also be welcome.
Edit: The .atlas file is located here
Your atlas file isn't ordered. If you call the code below, it will be ordered.
regions.sort(new Comparator<AtlasRegion>() {
#Override
public int compare(AtlasRegion o1, AtlasRegion o2) {
return Integer.parseInt(o1.name) > Integer.parseInt(o2.name) ? 1 : -1;
}
});
But I'm still checking why your atlas regions isn't ordered.
you should create array with frames ordered alphabetically instead of using textureAtlas.getRegions() which just gives you an array without caring of order.
The example for atlas with regions named like: region1, region2 and so on would be:
AtlasRegion[] frames = new AtlasRegion[framesCount];
for(int i = 0; i < framesCount; i++)
{
frames[i] = atlas.findRegion("region" + i);
}
so you can adjust it to your regions names.
If you want to get all frames from textureAtlas you can also do it like this:
Array<String> names = new Array<String>();
for(AtlasRegion region : textureAtlas.getRegions())
{
names.add( region.name );
}
names.sort();
Array<AtlasRegion> frames = new Array<AtlasRegion>();
for(String s : names)
{
frames.add( textureAtlas.findRegion(s) );
}
and then after get frames array just create animation object:
animation = new Animation(1/1f, frames.items); //or just frames depending on which type frames is
TexturePacker will index the images for you as long as you follow the naming scheme set forth here https://github.com/libgdx/libgdx/wiki/Texture-packer#image-indexes.
so your frames would be named something like
anim1_001.png
anim1_002.png
...
anim1_100.png
and a separate animation would simply be
anim2_001.png
....
anim2_100.png
EDIT:
additionally you can get the regions only related to certain animations. So instead of
animation = new Animation(1 / 1f, textureAtlas.getRegions());
you could use (yes it's findRegions() not findRegion()):
animation1 = new Animation(1 / 1f, textureAtlas.findRegions("anim1"));
animation2 = new Animation(1 / 1f, textureAtlas.findRegions("anim2"));
EDIT2:
If you're are using a stage it is quite easy to implement a screen viewport. I do it like this, (stage is a field and this step is in the show/create method):
stage = new Stage(new ScreenViewport());
Then in the resize method:
stage.getViewport().update(width, height, true);
Without a stage it's only slightly more complex
camera = new WhateverCamera();
viewport = new ScreenViewport(camera);
Then in the resize method:
viewport.update(width, height, true);
Use whatever camera you want, WhateverCamera is a placeholder and can be OrthographicCamera or PerspectiveCamera.
The last argument true centers the camera, if you don't want to do this set it to false or leave it out, it assumes false.

Control sprite rotation (it's spinning like crazy)

So, I have created a texture, and then a sprite.
On my render() method, I am check for user input. If the user has touched/clicked, then I want my sprite to rotate 90 degrees ONCE.
Right now the rotation works. However, it rotates multiple times per click!
How can I make it rotate only once per touch? I have a feeling that I might have to use delta time, and that occurs because the render method is being called frequently, but I don't know how to fix it... Thanks!
public class MyGame extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
Sprite sprite;
#Override
public void create () {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
sprite = new Sprite(img);
}
#Override
public void render () {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
sprite.draw(batch);
batch.end();
if (Gdx.input.isTouched()) {
rotateRight();
}
}
private void rotateRight() {
sprite.setRotation(sprite.getRotation() - 90);
}
}
Right now you are polling input inside of your render method. Polling simply checks the status of the input (is it touched or not) and does not care for any actual "event" occurred.
Instead of this you need to look for input events via event handling as this will give you access to the actual event of the screen being touched or untouched. You can do this by implementing InputProcessor which will give you access to override a bunch of touch event methods so you can execute your rotate method only on the event of the touch.
Example:
public void update()
{
Gdx.input.setInputProcessor(new InputProcessor() {
#Override
public boolean TouchDown(int x, int y, int pointer, int button) {
if (button == Input.Buttons.LEFT) {
rotateRight();
return true;
}
return false
}
});
}
Don't forget to call your update() method from your render method. I put it in a separate function just for readability and not to clog up your rendering code.
You can also have your class implement InputProcessor and override the methods in your class so you do not have to do it inline as I did above.
if (Gdx.input.justTouched() && Gdx.input.isTouched()) {
rotateRight();
}

libgdx - moving an object

So, I'm farely new to programming this is my first game, and I have been working on it for a while, for one of my classes and I'm creating a pong game just changed it up a bit and I'm having trouble moving my paddle from the left to the center everytime I change the the percentage of the width of the screen to move it for some reason it elevates the paddle by all almost the same. Also everything is controlled by the width for some bizarre reason its almost like the field.height has no effect on moving it? I'm stuck right here and I'm so close to finishing it I'm trying to lower the paddle to the bottom center? any help would really be appreciated and hopefully this makes sense.
here is the field
#Override
public void create () {
field.set(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
fieldLeft = field.x;
fieldRight = field.x + field.width;
fieldBottom = field.y;
fieldTop = field.y + field.height;
and here is the paddle movement
private void resetRectangle(){
paddle1.move(field.x + (field.width * .5f, 0);
paddle2.move(0,0);
}
and the class
public class Paddle extends GameShare{
private ShapeRenderer paddleRenderer;
protected Paddle() {
super(100, 32);
}
public void paddleCreate(){
paddleRenderer = new ShapeRenderer();
}
public void paddleRender(float dt){
paddleRenderer.begin(ShapeType.Filled);
drawPaddle(dt);
paddleRenderer.end();
}
private void drawPaddle(float dt) {
paddleRenderer.rect(this.getX(), this.getX(), this.getWidth(), this.getHeight());
paddleRenderer.rect(this.getX(), this.getX(), this.getWidth(), this.getHeight());
}

Categories