2
\$\begingroup\$

Please excuse my lack of knowledge with LibGDX, as I have just started learning it. I am going through some simple tutorials, and in one of them, I draw a string onto the screen like so:

// the following code is in the main file in the core project folder:

// this is in the create() method:
private SpriteBatch batch;
batch = new SpriteBatch();

// this is in the render() method:
batch.draw(batch, "Hello world", 200, 200);

I am getting an error saying: The method draw(texture, float[], int, int) in the type SpriteBatch is not applicable for the arguments (SpriteBatch, int, int)

So, LibGDX wants a float array to draw instead of a string?

Thanks in advance.

\$\endgroup\$
2
\$\begingroup\$

You should follow the official libGDX setup instructions.

There's a section about Using "Libgdx Setup" where you execute a JAR file. This provides you with a GUI project builder, which will allow you to configure some options like:

  • Project types (Android, desktop, iPhone, etc.)
  • Advanced options (eg. IntelliJ project file)

Once you do this, you'll have working code which you can run.

You're misunderstanding the compiler error. It expects Texture, float[], int, int and you're passing in batch, "Hello world", 200, 200 which is SpriteBatch, String, int, int. That's not right.

\$\endgroup\$
2
\$\begingroup\$

According to the API documentation for SpriteBatch there is no method signature for 'draw' that expects a 'SpriteBatch, String, int, int' parameter list. Logically it also makes no sense to pass a reference to the SpriteBatch to itself in its draw call.

If you are attempting to draw a string ("Hello world") to the screen then you will need to use a BitmapFont alongside the SpriteBatch. A useful reference link to the wiki here. A link to the BitmapFontTest class demonstrating how to use the BitmapFont class here.

If you feel confused or lost on how a certain section of libGDX works I strongly recommend browsing through the tests directory of the GitHub repository here. It's likely you'll find a test case showing how to use it.

\$\endgroup\$
2
\$\begingroup\$

Well, I found my problem. I was using a bitmap font, and instead of using the SpriteBatch's .draw(), I was supposed to use the bitmap font's .draw(). Thanks for the help everyone.

\$\endgroup\$
2
\$\begingroup\$

What you are doing is mixing up a couple of different ways to make a draw call in LibGdx. This is the trouble with copy-pasting from tutorials, but I can tell exactly what is going on. There are two commons ways of making a draw call. One is from directly inside the render() method and is common in simple tutorials. The other way is to create a draw() method in another class like a Sprite that takes a spritebatch as an argument. You seem to be mixing these two styles.

You are trying to draw some type of object that knows how to draw strings, but you replaced that object reference with the spritebatch that you are supposed to be passing it. Replace the first batch in batch.draw(batch, "Hello world", 200, 200); with whatever that object is that you are trying to draw. It would help to be able to see more of your code.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.