libGDX font doesn't appear in Screen

Anything libgdx related goes here!

libGDX font doesn't appear in Screen

Postby TheRedstoneDiamond » Wed Sep 13, 2017 11:53 pm

Operating system: Windows 10
Architecture: x64
Computers OpenGL's Version: 4.4

Hello, I'm trying to insert a Score Column to my screen.
However, even when I followed the steps on the wiki the code compiles but there is no score or even text in the screen
Here is my code:
Code: Select all
package com.jjstudios.drop;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;
import com.badlogic.gdx.math.Rectangle;

import java.util.Iterator;

public class Drop extends Game {

   public OrthographicCamera camera;

   private Array<Rectangle> raindrops;
   private long lastDropTime;

   private Vector3 touchPos;
   private int rounded_touch_Pos;
   private int rounded_delta_Time;

   public int score;
   private CharSequence scoreConverter;
   private BitmapFont font;

   public Texture bucket_Image;
   public Texture drop_Image;
   public Sound drop_Sound;
   public Music rain;
   public Texture background;

   private Rectangle bucket;

   SpriteBatch batch;

   @Override
   public void create () {

      //Init Raindrop array
      raindrops = new Array<Rectangle>();
      spawnRaindrop();

      // Initializes Vector3f
      touchPos = new Vector3();

      // Creates drop and bucket's image
      bucket_Image = new Texture("bucket.png");
      drop_Image = new Texture("droplet.png");
      background = new Texture("background.jpg");

      // Initializes camera and sets viewports area
      camera = new OrthographicCamera();
      camera.setToOrtho(false, 1300, 650);

      //Creates sounds
      drop_Sound = (Gdx.audio.newSound(Gdx.files.internal("DropSound.wav")));
      rain = (Gdx.audio.newMusic(Gdx.files.internal("rain.mp3")));

      // Init game font
      font = new BitmapFont(Gdx.files.internal("default.fnt"), Gdx.files.internal("uiskin.png"), false);
      score = 0;
      scoreConverter = "Your Score" + score;

      // Buckets Properties
      bucket = new Rectangle();
      bucket.x = 1300 / 2;
      bucket.y = 100;
      bucket.width = 64;
      bucket.height = 64;

      batch = new SpriteBatch();

      rain.setLooping(true);
      rain.play();
   }

   public void drawScoreBoard()
   {
      // Adds score on the screen
      font.draw(batch, scoreConverter, 0, 0);
      font.setColor(Color.BLACK);
   }

   @Override
   public void render () {
      Gdx.gl.glClearColor(1, 0, 0, 1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

      batch.begin();

      drawScoreBoard();
      batch.draw(background, 0, 0);
      batch.draw(bucket_Image, bucket.x, bucket.y);
      for (Rectangle raindrop: raindrops)
      {
            batch.draw(drop_Image, raindrop.x, raindrop.y);
      }

      // Mouse or Tap Controls
      if (Gdx.input.isTouched())
      {
         touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
         camera.unproject(touchPos);
         rounded_touch_Pos = Math.round(touchPos.x);
         bucket.x = rounded_touch_Pos - 64 / 2;
      }

      // Keyboard Controls
      if (Gdx.input.isKeyPressed(Input.Keys.LEFT));
         {
            rounded_delta_Time = Math.round(Gdx.graphics.getDeltaTime());
            bucket.x -= 50;
      }
      if (Gdx.input.isKeyPressed(Input.Keys.RIGHT));
      {
         rounded_delta_Time = Math.round(Gdx.graphics.getDeltaTime());
         bucket.x += 50;
      }

      // Detects whether bucket goes out of bounds
      if (bucket.x < 0)
      {
         bucket.x = 0;
      }
      if (bucket.x > 1300 - 64)
      {
         bucket.x = 1300 - 64;
      }

      //Decides when to spawn raindrop
      if (TimeUtils.nanoTime() - lastDropTime > 1000000000)
      {
         spawnRaindrop();
      }

      // Makes the raindrops move
      Iterator<Rectangle> iter = raindrops.iterator();
      while(iter.hasNext())
      {
         Rectangle raindrop = iter.next();
         raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
         if (raindrop.y < 100)
         {
            iter.remove();
         }
         if(raindrop.overlaps(bucket)) {
            drop_Sound.play();
            score++;
            scoreConverter = "Your Score: " + score;
            iter.remove();
         }
      }

      camera.update();
      batch.end();
   }

   public void spawnRaindrop()
   {
        Rectangle raindrop = new Rectangle();
        raindrop.x = MathUtils.random(0, 800-64);
        raindrop.y = 650;
      raindrop.width = 64;
      raindrop.height = 64;
      raindrops.add(raindrop);
      lastDropTime = TimeUtils.nanoTime();
   }

   @Override
   public void dispose () {
      batch.dispose();
      bucket_Image.dispose();
      drop_Image.dispose();
      drop_Sound.dispose();
      rain.dispose();
      background.dispose();
   }
}

Any answers or suggestions would be gladly accepted and Thanks!!! :)
TheRedstoneDiamond
 
Posts: 5
Joined: Mon Jul 17, 2017 8:23 pm

Re: libGDX font doesn't appear in Screen

Postby Spoofon » Thu Sep 14, 2017 7:28 am

Hi try drawing the scores after you draw the background. At the moment the score will be underneath the background.
Hope that works.
Spoofon
 
Posts: 32
Joined: Wed Sep 19, 2012 9:00 pm

Re: libGDX font doesn't appear in Screen

Postby TheRedstoneDiamond » Thu Sep 14, 2017 9:58 pm

I did that but now some weird charterers are poping up in the screen
https://imgur.com/VCxk4T9]
TheRedstoneDiamond
 
Posts: 5
Joined: Mon Jul 17, 2017 8:23 pm

Re: libGDX font doesn't appear in Screen

Postby captainflyaway » Fri Sep 15, 2017 5:56 am

I do not really now what your issue is but I can tell you to use Scene2d.

Make a Stage the size of your view and add a Label to the stage. Now you can simply change everything you like. Use a table to make it work easily throughout different window sizes.

By label.setText(..)
captainflyaway
 
Posts: 104
Joined: Mon Feb 20, 2017 7:52 am

Re: libGDX font doesn't appear in Screen

Postby itsabhiaryan » Sat Sep 16, 2017 7:29 pm

    1. Draw your score above all your element like above background.
    2. You're initialising font in wrong way :

    Code: Select all
       font = new BitmapFont(Gdx.files.internal("default.fnt"), Gdx.files.internal("uiskin.png"), false);

    You're using uiskin.png in second argument that is not png part of your bitmap font so you're getting arbitrary view of your score.

Solution :

    So if .png file is in same folder use this constructor
    Code: Select all
    BitmapFont (FileHandle fontFile)

    Or If you've skin you can get font object from skin object.
itsabhiaryan
 
Posts: 4
Joined: Sat Nov 19, 2016 8:05 am
Location: New Delhi


Return to Libgdx

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Google Feedfetcher and 11 guests