I'm following this tutorial on libgdx and I've hit a bit of a snag. I just finished the 'loading the assets' section. When I tried to run it, instead of getting the rain sound and a pink background, like the tutorial claims, I get errors up the wazoo. Here's my Drop.java:
package com.badlogic.drop;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
public class Drop implements ApplicationListener {
Texture dropImage;
Texture bucketImage;
Sound dropSound;
Music rainMusic;
#Override
public void create() {
// load the images for the droplet and the bucket, 64x64 pixels each
dropImage = new Texture(Gdx.files.internal("droplet.png"));
bucketImage = new Texture(Gdx.files.internal("bucket.png"));
// load the drop sound effect and the rain background "music"
dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
// start the playback of the background music immediately
rainMusic.setLooping(true);
rainMusic.play();
}
#Override
public void dispose() {
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
and here are my errors. (Ones I think are important are bolded)
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: droplet.PNG
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:113)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: droplet.PNG
at com.badlogic.gdx.graphics.Pixmap.(Pixmap.java:140)
at com.badlogic.gdx.graphics.glutils.FileTextureData.prepare(FileTextureData.java:64)
at com.badlogic.gdx.graphics.Texture.load(Texture.java:175)
at com.badlogic.gdx.graphics.Texture.create(Texture.java:159)
at com.badlogic.gdx.graphics.Texture.(Texture.java:133)
at com.badlogic.gdx.graphics.Texture.(Texture.java:122)
at com.badlogic.drop.Drop.create(Drop.java:21)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:127)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:110)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: droplet.PNG (Internal)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:127)
at com.badlogic.gdx.files.FileHandle.length(FileHandle.java:580)
at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:215)
at com.badlogic.gdx.graphics.Pixmap.(Pixmap.java:137)
... 8 more
The weird thing is, I have all of the pngs and music files in my assets/data folders. They're all there, but the code isn't seeing them. Any idea what's causing this?
when you are using
Gdx.files.internal <- This only gets you to the assets folder. You have to direct it into any sub-directories you are wanting files from also.
and your files are in assets/data use
Gdx.files.internal("data/droplet.png")
Be sure to change all your other references also to the correct location.
Related
Alright, first and foremost, I am new to Java, but not to programming.
For a personnal project I decided that I should use Slick2D, a graphics library in java, and I am running into one problem I haven't been able to fix for now.
Considering the following:
I use Netbeans
I'm currently on linux
From what I understand, slick2d uses lwgjl. I have followed the instructions on the following page:
Setting up Slick2D for netbeans
My problem I think is giving my JVM the right options.
Currently the ones I set are:
-Djava.library.path=/home/karel/Téléchargements/slick/native/unix
Here is the current error output I get:
run:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl64 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at org.lwjgl.Sys$1.run(Sys.java:72)
at java.security.AccessController.doPrivileged(Native Method)
at org.lwjgl.Sys.doLoadLibrary(Sys.java:66)
at org.lwjgl.Sys.loadLibrary(Sys.java:87)
at org.lwjgl.Sys.<clinit>(Sys.java:117)
at org.lwjgl.opengl.Display.<clinit>(Display.java:135)
at org.newdawn.slick.AppGameContainer$1.run(AppGameContainer.java:39)
at java.security.AccessController.doPrivileged(Native Method)
at org.newdawn.slick.AppGameContainer.<clinit>(AppGameContainer.java:36)
at jcoinche.client.JcoincheClient.main(JcoincheClient.java:29)
/home/karel/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
My code is as follow (well, the part that needs a library, and doesn't work - it's all tutorial material, by the way. Nothing I made myself):
package jcoinche.client;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Scanner;
import static jcoinche.client.Game.gamename;
import static jcoinche.client.Game.xSize;
import static jcoinche.client.Game.ySize;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.SlickException;
public class JcoincheClient
{
public static void main(String[] args)
{
Socket socket;
AppGameContainer appgc;
try
{
appgc = new AppGameContainer(new Game(gamename));
appgc.setDisplayMode(xSize, ySize, false);
appgc.setTargetFrameRate(60);
appgc.start();
}
catch(SlickException e)
{
e.printStackTrace();
}
}
Game.java:
package jcoinche.client;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Game extends StateBasedGame
{
public static final String gamename = "MyGameName";
public static final int play = 0;
public static final int xSize = 400;
public static final int ySize = 300;
public Game(String gamename){
super(gamename);
this.addState(new Play());
}
public void initStatesList(GameContainer gc) throws SlickException{
this.getState(play).init(gc, this);
this.enterState(play);
}
}
Play.java:
package jcoinche.client;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class Play extends BasicGameState
{
public Play()
{
}
public void init(GameContainer gc, StateBasedGame sbg)
throws SlickException
{
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
throws SlickException
{
}
public void update(GameContainer gc, StateBasedGame sbg, int delta)
throws SlickException
{
}
public int getID()
{
return 0;
}
}
The error is telling you of the absence of a file named 'lwjgl64'. You are getting an UnsatisfiedLinkError, which according to the Java Documentation, is:
Thrown if the Java Virtual Machine cannot find an appropriate native-language definition of a method declared native.
Now, are you using Slick2D alone? Or do you have LWJGL and Slick2D? Slick2D is not standalone; you need LWJGL to run it. I suggest getting LWJGL from their site, and add it's native folder to the classpath.
From the Slick2D docs, they write this:
-Djava.library.path=<lwjgl-X.X path>/native/<linux|macosx|solaris|windows>
Source
There are a few answers on stack that may help you solve this issue as it is clear from the error that it is caused by a linking error to LWJGL64. One post with a number of solutions here offers a number of solutions, though targeted at the eclipse IDE specifically the general solution applies:
To quote from here #Ben Jackson
LWJGL needs the native components for your particular platform to be
in java.library.path. These are in the subdirectory native in the
LWJGL distribution and end in .so on Linux, OSX and Solaris and .dll
for windows.
Other potential examples solving the error:
Setting up natives
Alright, this is a bit embarassing, but I understood what my problem was and it was a simple misunderstanding of the link I gave (instructions on how to install and use slick2D on netbeans):
In fact, my problem was that, because of the lwgjl.jar and other similar files in the slick2D folder, I thought lwgjl was, in the end, included in the slick folder.
As such, for the last step, I gave to my JVM a path directing it to the slick library folder, while it was, this time, asking for lwgjl files, causing the errors shown above.
I changed that path to the lwgjl/native/linux folder, and everything is now working perfectly.
Thanks for your answers, they helped greatly.
I'm using Libgdx and AssetManager to load assets into memory. First I thought that something is wrong with my project so I made a new clean one, and the problem persists.
The Sound is not playing even if I use "IsLoaded" method and is not playing the first time (if you jump really quick into the game). The sound is small , like 40KB, and this happens only on Android (works on Desktop/iOS). The device I'm testing on Android is HTC One M7 with Lolipop. Here is the code of a clean project.
package com.mygdx.game;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.Audio;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Sound;
public class MyGdxGame extends ScreenAdapter {
private AssetManager assetManager;
#Override
public void show() {
super.show();
assetManager = new AssetManager();
assetManager.load("sound/jump.mp3", Sound.class);
}
Sound jump;
boolean isLoaded;
#Override
public void render(float delta) {
super.render(delta);
if(isLoaded) return;
assetManager.update();
if(assetManager.isLoaded("sound/jump.mp3", Sound.class)) {
assetManager.finishLoading();
jump = assetManager.get("sound/jump.mp3", Sound.class);
isLoaded = true;
System.err.println("LOADED");
jump.play();
}
}
}
In LOGCAT I'm getting "soundpool sample 1 not ready" error.
Any ideas?
NOTE: The sound is playing as normal if I give it a little time (If I don't press "play" right away).
The question is why does Libgdx thinks that the sound is loaded into memory even if its not.
Have you tried using .ogg instead?
On my libgdx projects i always used .ogg, since its license -free and well, it never gave me issues!
You should also be careful with the different Sound imports. I remember that you can sometimes import directly .ogg .mp3 classes from libgdx and that causes issues.
Also, i think the problem is that, even if the audio file was loaded, the assetmanager is still loading other resources.. so its busy doing another task.
I would either :
a) Play that sound without the assetmanager, since its just for the loading, right?
b) (This needs to be checked) But Maybe use a different assetmanager for that audio? Unsure how much resource would it waste by having 2 assetmanagers..
But, i would try that. Change to .ogg first, be sure the audio starts at exactly 0:00 and update to the last libgdx version.
I'm experiencing the same issue. I am using libgdx version 1.9.2. So far, I tried:
changing mp3 to ogg
using different asset manager for sounds
and it didn't help.
I replaced Sound object with Music object and it did help. But I'm not sure if this is the right approach, because I have a lot of short sounds in my app.
I know this is an old thread but I just ran into this same problem and I solved it like this..
private Sound sound;
private float soundVolume;
public void playSound(String path, float volume) {
sound = assetManager.get(path, Sound.class);
soundVolume = volume;
}
#Override
public void render () {
super.render();
// Because of some weird Android bug
if (sound != null) {
sound.play(soundVolume);
sound = null;
}
}
This is in my main game class and I can call the playSound() method from any other class.
It's probably caused by Gdx.audio.newSound() method is loading the sound by asynchronously instead asserting sound loading process to processor. I solved this by adding a "delay" to the playing sound process only for the first playing. That solved my problem.
private static void soundBugFixer(Sound sfx, float volume)
{
Timer x = new Timer();
x.scheduleTask(new Timer.Task() {
#Override
public void run() {
sfx.play(volume);
}
}, 0.1f );
}
public static void remove(float volume) {
if(sfxRemove == null)
{
sfxRemove = Gdx.audio.newSound(Gdx.files.internal("sfx/sfx_remove.wav"));
soundBugFixer(sfxRemove, volume);
}
else
{
sfxRemove.play(volume);
}
}
I am trying to get a Box2D PointLight to render on the screen, but upon rendering, it throws an exception. I have revised the API and the Box2D User Manual, as well as watched videos on the topic, but have yet to find a solution to my problem. Here is my code and the error.
package com.mygdx.test;
import box2dLight.PointLight;
import box2dLight.RayHandler;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.World;
public class Test extends ApplicationAdapter {
/** the camera **/
OrthographicCamera camera;
RayHandler rayHandler;
World world;
#Override
public void create() {
camera = new OrthographicCamera(48, 32);
camera.update();
world = new World(new Vector2(0, -10), true);
rayHandler = new RayHandler(world);
new PointLight(rayHandler, 32);
}
#Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
world.step(Gdx.graphics.getDeltaTime(), 8, 3);
rayHandler.setCombinedMatrix(camera.combined);
rayHandler.updateAndRender();
}
And the error:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.NoSuchMethodError: com.badlogic.gdx.graphics.glutils.FrameBuffer.getColorBufferTexture()Lcom/badlogic/gdx/graphics/Texture;
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:127)
Caused by: java.lang.NoSuchMethodError: com.badlogic.gdx.graphics.glutils.FrameBuffer.getColorBufferTexture()Lcom/badlogic/gdx/graphics/Texture;
at box2dLight.LightMap.gaussianBlur(LightMap.java:76)
at box2dLight.LightMap.render(LightMap.java:37)
at box2dLight.RayHandler.render(RayHandler.java:328)
at box2dLight.RayHandler.updateAndRender(RayHandler.java:262)
at com.mygdx.test.Test.render(Test.java:33)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
I had the same issue with a newly generated libgdx project, however updating the box2dlights version from 1.3 to 1.4 solved this error for me.
Have you tried using this method?
new PointLight(rayHandler, RAYS_NUM, new Color(1,1,1,1), lightDistance, x, y);
I have also noticed, you should do
world.step();
at the end of the render() method. Probably wont fix the problem, but i just noticed
I'm trying to run this code that is used to see if things were setup properly:
package simpleslickgame;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
public class SimpleSlickGame extends BasicGame
{
public SimpleSlickGame(String gamename)
{
super(gamename);
}
#Override
public void init(GameContainer gc) throws SlickException {}
#Override
public void update(GameContainer gc, int i) throws SlickException {}
#Override
public void render(GameContainer gc, Graphics g) throws SlickException
{
g.drawString("Howdy!", 10, 10);
}
public static void main(String[] args)
{
try
{
AppGameContainer appgc;
appgc = new AppGameContainer(new SimpleSlickGame("Simple Slick Game"));
appgc.setDisplayMode(640, 480, false);
appgc.start();
}
catch (SlickException ex)
{
Logger.getLogger(SimpleSlickGame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
All of the Googling I've done has led me to checking the natives over and over and I"m pretty sure I've done it right. When I go to the libraries in this project's build path, JRE System library and Slick2D both say Native library location: .../windows/x64. I've tried just .../windows, and I've tried it with the JRE not having a native location. I followed two different tutorials on how to do this and I keep getting errors trying to run this simple code. Any help?
basically what you need is to add slick.jar and lwjgl.jar to your build path and then right click your project >> open properties >> click on Java Build Path >> open Libraries tab >> expand JRE System Library >> click Native library location >> edit >> External Folder >> find ..\lwjgl-2.9.0\native\windows folder on your hard drive >> select it >> and you're done
g.drawString("Howdy!", 10, 10);
I would just move this string to some other location, cause it's showing over the FPS counter :)
EDIT:
you can use the natives from ..\slick\lib\natives-windows.jar.
open it with WinRAR or something and extract to a folder, then just set that folder to be your Native library location
I am following a java tiled game tutorial using libGDX and I met the following errors :
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: gaming creation/herbe16.png
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
at com.badlogic.gdx.graphics.glutils.FileTextureData.prepare(FileTextureData.java:64)
at com.badlogic.gdx.graphics.Texture.load(Texture.java:142)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:133)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:112)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:108)
at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:119)
at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:104)
at com.poussins.screens.Play.show(Play.java:35)
at com.badlogic.gdx.Game.setScreen(Game.java:62)
at com.poussins.Poussins.create(Poussins.java:11)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: gaming creation/herbe16.png (Internal)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:133)
at com.badlogic.gdx.files.FileHandle.length(FileHandle.java:563)
at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:218)
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)
... 12 more
here is the code i'm using in the Play class :
package com.poussins.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
public class Play implements Screen{
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private OrthographicCamera camera;
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.setView(camera);
renderer.render();
}
#Override
public void resize(int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.update();
}
#Override
public void show() {
map = new TmxMapLoader().load("maps/map-Herbe-Goudron.tmx");
renderer = new OrthogonalTiledMapRenderer(map);
camera = new OrthographicCamera();
}
#Override
public void hide() {
dispose();
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
map.dispose();
renderer.dispose();
}
}
In order to put the .tmx map into the Eclipse project, I just made a Copy (from the folder where the .tmx map is) and a paste in the Eclipse project named "Poussins-Desktop / assets / maps". I used the same actions (copy paste) to give the .png tiles to the same Eclipse project named "Poussins-Desktop / assets / maps" (can see it in the screenshots).
In the code, I am not sure with this following line :
map = new TmxMapLoader().load("maps/map-Herbe-Goudron.tmx");
because I don't know if the path to the .tmx is good, but it seem to respect what was said in the tutorial ...
In fact, in the error, it is saying that the file herbe16.png cannot be loaded. but this file is a tile from the whole .tmx map.
I am working on UBUNTU 13.10 system, Eclipse 3.8.1
I hope someone will help me and I thank you in advance for paying attention to my problem.
Okay, I don't know if this will help you..
I was just declaring the path for my files not minding caps letters (I'm using windows7).
Now, when I tested the code as a java application on my desktop it was going smoothly but when I tried launching it on an emulator or an android device it would crash.
Turns out that the emulator and device both need the exact path with capital letters.
This may have already been solved or the OP may have given up on it, but I came across this error too on the same tutorial. I solved the issue in my case. It may be possible that the OP was using a .png of a different bit depth (as was in my case). The .png that caused the error was 64 bit depth (you can check by right clicking on the .png -> PROPERTIES -> DETAILS). Using a .png of a bit depth of 32 allowed the loading to work perfectly fine. I used photoshop to create my .png tileset, and when creating the new file, set it to 8 bit (next to the color mode).