Having an issue loading resource from assetmanager - libgdx

I am using assetmanager to laod a .pack file
//load assetmanager textures
Manager.load("Pong.pack", TextureAtlas.class); << line 34
Manager.finishLoading();
TextureAtlas atlas = Manager.get("Pong.pack");
the line with this << is the culprit from the log
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.Shadow.Pong.Game.Assets.init(Assets.java:34)
I pointed the working directory to the resource folder for the desktop module. So i dont know why it keeps saying nullpointerexception.
thanks in advance

try this
TextureAtlas atlas = Manager.get("Pong.pack", TextureAtlas.class);
having a wild guess because many information are missing.
I mean what exactly in Asset.java line 34.
Manager instantiated or not and many other things.
But i guess one i suggested seems to be the problem

I Realized that i didint call a new assetmanager for the parameter for the asstmanager call so thats why it was giving the nullpointer

Related

Cannot load a texture to AssetManager in libGDX

I seem to be unable to properly load a texture from the "assets" folder in my libgdx project in Eclipse.
My code is:
AssetManager am = new AssetManager();
am.load("football.png", Texture.class);
if(! am.isLoaded("football.png")) System.out.println("NOT loaded");
The "assets" folder is in the "/my-gdx-game-android", whereas the code above is called from the "/my-gdx-game-core/src/com/mygdx/game/MyGdxGame.java".
I am absolutely sure the texture is there. However, I am constantly getting "NOT loaded" message. Obviously, I am doing something wrong here. Could you please help me?
Problem solved. It turns out that AssetManager loads assets asynchronously. To get rid of the "NOT loaded" message, I had to put
am.finishLoading();
before
if(! am.isLoaded("football.png")) System.out.println("NOT loaded");
This method blocks the thread until all assets are loaded.

Do I have to always have an .atlas file when making a skin?

I'm currently trying to build a simple loading screen for my game and am trying to get a skin for a font.
When I try to get the the skin though, with skin = game.manager.get("bin/ui/loading.json", Skin.class);, this error occurs:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: File not found: bin\ui\loadingSkin.atlas (Internal)
at com.badlogic.gdx.assets.AssetManager.handleTaskError(AssetManager.java:540)
at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:356)
at com.badlogic.gdx.assets.AssetManager.finishLoading(AssetManager.java:377)
at com.Sidescroll.game.LoadingScreen.show(LoadingScreen.java:32)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at com.Sidescroll.game.Sidescroll.create(Sidescroll.java:20)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:143)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: bin\ui\loadingSkin.atlas (Internal)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136)
at com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.<init>(TextureAtlas.java:103)
at com.badlogic.gdx.assets.loaders.TextureAtlasLoader.getDependencies(TextureAtlasLoader.java:58)
at com.badlogic.gdx.assets.loaders.TextureAtlasLoader.getDependencies(TextureAtlasLoader.java:34)
at com.badlogic.gdx.assets.AssetLoadingTask.handleSyncLoader(AssetLoadingTask.java:98)
at com.badlogic.gdx.assets.AssetLoadingTask.update(AssetLoadingTask.java:87)
at com.badlogic.gdx.assets.AssetManager.updateTask(AssetManager.java:477)
at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:354)
... 6 more
In my current assets/ui folder are 5 files:
loading_0.png - BitmapFont image
loading.fnt - Font file
loading.pack - used TexturePacker to pack the single image
loading.png - TexturePacker image
loadingSkin.json - Skin.json, where BitmapFont and LabelStyle is described
The part where Im trying to use assetmanager:
//this is the beginning of the show method of my loadingScreen, nothing
//happened before
game.manager.load("bin/ui/loading.pack", TextureAtlas.class);
game.manager.finishLoading();
atlas = game.manager.get("bin/ui/loading.pack");
// do i need an atlas here ?
game.manager.load("bin/ui/loadingSkin.json", Skin.class);
game.manager.finishLoading();
skin = game.manager.get("bin/ui/loading.json", Skin.class);
Question is, do I always need an atlas? if yes, how do I create one, if not, why does the error occur?
EDIT: I'm using release 1.5.4 of libgdx and do not have android in my project
the Skin has many various contructors like:
Skin()
Skin(FileHandle skinFile)
Skin(FileHandle skinFile, TextureAtlas atlas)
Skin(TextureAtlas atlas)
If you are using the JSON version of constructor you are just passing the json configuration file to it. Skin needs to have some texture atlas so with this option by default it is looking for the .atlas file named exatcly like json file. It means that when you are using file name loadingSkin.json:
game.manager.load("bin/ui/loadingSkin.json", Skin.class);
it will look for the loadingSkin.atlas file and because there is nothing like this it causes the error.
Now you have two option:
create file named like your json but with atlas extension (.atlas is exactly like .pack - you can just change extension or create a file with .atlas instead of .pack in TexturePacker)
loading.pack -> loading.atlas
When changing names be aware of you texture .png file! (The .pack/.atlas file has its name inside - check with editor)
use another constructor of skin like:
Skin(FileHandle skinFile, TextureAtlas atlas)
You are passing filehandle to .json and TextureAtlas instance of the skin texture
TextureAtlas atlas = game.manager.get("bin/ui/loading.pack", TextureAtlas.class);
Skin skin = new Skin( Gdx.files.internal("bin/ui/loadingSkin.json"), atlas);
This line causes the error:
game.manager.load("bin/ui/loadingSkin.json", Skin.class);
When I looked into SkinLoader class' source I've seen that it loads the file by appending .atlas by default:
#Override
public Skin loadSync (AssetManager manager, String fileName, FileHandle file, SkinParameter parameter) {
String textureAtlasPath = file.pathWithoutExtension() + ".atlas";
...
So it seems like you need a TextureAtlas for your skin. You can create TextureAtlas by using https://libgdx-texturepacker-gui.googlecode.com/files/gdx-texturepacker-3.2.0.zip
You can create a TextureAtlas named loadingSkin.atlas and put it under bin\ui\loadingSkin.atlas. If you want to create an atlas with another name and tell the loader which TextureAtlas to use, you can pass SkinParameter to your load method:
SkinLoader.SkinParameter loadingSkinParameter = new SkinLoader.SkinParameter();
loadingSkinParameter.textureAtlasPath = "YOUR_TEXTURE_ATLAS_PATH";
game.manager.load("bin/ui/loadingSkin.json", Skin.class, loadingSkinParameter);

how to resolve stringIndexOutofBoundsException when bitmap is initialized

I am learning to develop a game using libgdix.I follow the book Learning LibGDX game development,second edition.
I am getting stuck on using Bitmapfont.
public class AssetsFonts
{
public final BitmapFont defaultSmall;
public final BitmapFont defaultNormal;
public final BitmapFont defaultBig;
public AssetsFonts()
{
defaultSmall = new BitmapFont(Gdx.files.internal("images/arial-15.fnt"),true);
defaultNormal = new BitmapFont(Gdx.files.internal("images/arial-15.fnt"),true);
defaultBig = new BitmapFont(Gdx.files.internal("images/arial-15.fnt"),true);
defaultSmall.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
defaultNormal.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
defaultBig.getRegion().getTexture().setFilter(TextureFilter.Linear,TextureFilter.Linear);
}
}
when i run this am getting the following error
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Error loading font file: images/arial-15.fnt
at com.badlogic.gdx.graphics.g2d.BitmapFont$BitmapFontData.load(BitmapFont.java:650)
at com.badlogic.gdx.graphics.g2d.BitmapFont$BitmapFontData.<init>(BitmapFont.java:465)
at com.badlogic.gdx.graphics.g2d.BitmapFont.<init>(BitmapFont.java:115)
at com.packtpub.libgdx.canyonbunny.game.Assets$AssetsFonts.<init>(Assets.java:125)
at com.packtpub.libgdx.canyonbunny.game.Assets.init(Assets.java:49)
at com.packtpub.libgdx.canyonbunny.CanyonBunnyMain.create(CanyonBunnyMain.java:22)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:143)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -7
at java.lang.String.substring(String.java:1918)
at com.badlogic.gdx.graphics.g2d.BitmapFont$BitmapFontData.load(BitmapFont.java:476)
... 7 more
The exception you've got is this one. Practically saying that something went wrong while loading the file. The inner exception shows what went wrong while loading:
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -7
at java.lang.String.substring(String.java:1918)
at com.badlogic.gdx.graphics.g2d.BitmapFont$BitmapFontData.load(BitmapFont.java:476)
Since there's no call to String#substring at line 476, this is an indication that you're using an older version of libgdx (which is to be expected, because a lot has changed to that file recently). So I'd suggest that you update to the latest nightly (or check the github history at the time of the version you're using), so you know which call is actually causing the error.
Either way, the fact that a substring call failed while loading your file is a good indication that the file is likely to be corrupt or doesnt meet the expected format in another way. Assuming you want to use the font that the libgdx tests uses, then you can download the correct version here and the required image here. Make sure to place the image in the same folder as the fnt file.
Your real problem is that it can't find the font file...
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Error loading font file: images/arial-15.fnt
Make sure the file is in the correct place, which is probably android/assets/images but depends on your project setup.
Also ensure that your run the desktop version with android/assets as the working folder.

LibGDX - Missing LabelStyle font error

I have a folder named UI and inside it, it contains a button.pack and a button.png (I created this with the GDX texture packer). Here is the line of code for the .pack.
atlas = new TextureAtlas(Gdx.files.internal("ui/button.pack"));
skin = new Skin(atlas);
But when I run the application it gives me this error:
Exception in thread "LWJGL Application" java.lang.IllegalArgumentException: Missing LabelStyle font.
Am I doing something wrong? What is the excact problem?

trouble getting flashdevelop to correctly identify sampling rate of a sound file

I'm trying to put a sound file into a flash program with the following code.
[Embed(source="../lib/DST-Aircord.mp3")]
static var sndtrck:Class; //used to represent the background music file
static var soundtrack:Sound; //used to handle the soundtrack file
soundtrack = new Sound();
soundtrack.load(sndtrck);
soundtrack.play();
I'm not sure that this is correct but it is throwing the following errors.
Error: Unsupported sampling rate (32000Hz)
Error: Unable to transcode ../lib/DST-Aircode.mp3.
Regarding the first error I checked in iTunes and the sampling rate is 44100Hz which I believe is what Flash needs to run. However FlashDevelop is incorrectly determining the sampling rate? I'm guessing that the second error is caused by the first although I'm not entirely sure what it means. Does anyone know what I might be missing in my attempt to embed sound into this SWF?
EDIT: changing the load function to use the path specified in the embed code yields the same error.
the load function of the Sound class only takes a URL of an external sound file. Im not sure what the class your putting into it is, but it probably throws the errors because a Class itself is not a sound file.
This is a link to the load() function of the Sound class, everything about it is there.
While writing this answer I realized that the question is pretty old. ;)
Anyhow, I ran into the same problem a while back and it could be solved
by this: Unsupported sampling flex/actionscript