LibGDX NullPointer尝试加载音乐时出现异常

时间:2016-10-30 01:07:41

标签: java nullpointerexception libgdx

我正在编写第10章学习Libgdx游戏开发一书,讨论如何为游戏添加音乐和声音。我试着谷歌搜索,看看是否有其他人遇到相同的空指针问题添加音乐,但没有运气。

基本上尝试从名为Assets的其他类加载音乐曲目以在我的主游戏中运行。

我看到资产已加载,它会在控制台上打印出来。所以我不认为这是一个没有找到文件的问题。

com.woodgdx.game.Assets: # of assets loaded: 9
com.woodgdx.game.Assets: asset: ../core/assets/sounds/jump_with_feather.wav
com.woodgdx.game.Assets: asset: ../core/assets/canyonbunny.pack.png
com.woodgdx.game.Assets: asset: ../core/assets/music/keith303_-_brand_new_highscore.mp3
com.woodgdx.game.Assets: asset: ../core/assets/sounds/live_lost.wav
com.woodgdx.game.Assets: asset: ../core/assets/canyonbunny.pack2.png
com.woodgdx.game.Assets: asset: ../core/assets/sounds/pickup_feather.wav
com.woodgdx.game.Assets: asset: ../core/assets/sounds/jump.wav
com.woodgdx.game.Assets: asset: ../core/assets/canyonbunny.pack.atlas
com.woodgdx.game.Assets: asset: ../core/assets/sounds/pickup_coin.wav

所以我不确定发生了什么......

错误本身就是......

Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.woodgdx.game.woodGdxGame.create(woodGdxGame.java:29)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:147)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)

主要课程

package com.woodgdx.game;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.woodgdx.game.Assets;
import com.woodgdx.game.screens.MenuScreen;
import com.woodgdx.game.util.AudioManager;
import com.woodgdx.game.util.GamePreferences;

/**
 * The main game file with the main
 * engine methods
 * @author xxxx
 */
public class woodGdxGame extends Game
{
    @Override
    public void create()
    {
        // Set Libgdx log level
        Gdx.app.setLogLevel(Application.LOG_DEBUG);
        // Load assets
        Assets.instance.init(new AssetManager());
        // Load preferences for audio settings and start playing music
        GamePreferences.instance.load();
        AudioManager.instance.play(Assets.instance.music.song01);
        // Start game at menu screen
        setScreen(new MenuScreen(this));
    }
}

此行发生错误..

AudioManager.instance.play(Assets.instance.music.song01);

我的Assets类包含此内部类。这本书有它的方式,当我把它作为自己的课时,我也遇到了同样的错误。

    /**
     * Music for game loaded
     * @author xxxx
     *
     */
    public class AssetMusic
    {
        //Our music object
        public final Music song01;

        /**
         * Retrieves the music file for the music
         * @param am
         */
        public AssetMusic(AssetManager am)
        {
            song01 = am.get("../core/assets/music/keith303_-_brand_new_highscore.mp3", Music.class);
        }
    }

然后这里是AudioManager类,它们在这里发挥了一点作用。

/**
 * Manages the playing and organization
 * of audio files
 * @author xxxx
 *
 */
public class AudioManager
{
    public static final AudioManager instance = new AudioManager();

    private Music playingMusic;

    // singleton: prevent instantiation from other classes
    private AudioManager()
    {
    }

    /**
     * Play sound with default vol, pitch, and pan (0)
     * @param sound
     */
    public void play(Sound sound)
    {
        play(sound, 1);
    }

    /**
     * Play sound with default pitch and pan (0) but different vol
     * @param sound
     * @param volume
     */
    public void play(Sound sound, float volume)
    {
        play(sound, volume, 1);
    }

    /**
     * Play sound with default pan (0) with different pitch and vol
     * @param sound
     * @param volume
     * @param pitch
     */
    public void play(Sound sound, float volume, float pitch)
    {
        play(sound, volume, pitch, 0);
    }

    /**
     * Plays sound with a vol, pitch, and pan activated
     * @param sound
     * @param volume
     * @param pitch
     * @param pan
     */
    public void play(Sound sound, float volume, float pitch, float pan)
    {
        //If sound is muted, dont play
        if (!GamePreferences.instance.sound)
            return;
        sound.play(GamePreferences.instance.volSound * volume, pitch, pan);
    }

    /**
     * Plays music on loop
     * @param music
     */
    public void play(Music music)
    {
        stopMusic();
        playingMusic = music;
        if (GamePreferences.instance.music)
        {
            music.setLooping(true);
            music.setVolume(GamePreferences.instance.volMusic);
            music.play();
        }
    }

    /**
     * Ends the music loop of doom
     */
    public void stopMusic()
    {
        if (playingMusic != null)
            playingMusic.stop();
    }

    /**
     * Checks settings for muted music, vol level
     */
    public void onSettingsUpdated()
    {
        //No music available
        if (playingMusic == null)
            return;
        //Sets volume
        playingMusic.setVolume(GamePreferences.instance.volMusic);
        //Checks if music is not muted
        if (GamePreferences.instance.music)
        {
            //If music isn't playing, play it
            if (!playingMusic.isPlaying())
                playingMusic.play();
        }
        //Mute music if selected so
        else
        {
            playingMusic.pause();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

忘记添加音乐和声音以进行Assets类的初始化。我认为这是文件的东西,但它没有初始化。

将此添加到Assets的init()方法

sounds = new AssetSounds(assetManager);
music = new AssetMusic(assetManager);